From b60119790edee3968d3decb369cbcb4a3233ba33 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 14 Jan 2022 12:45:55 +0000 Subject: [PATCH] Fix wrong memcmp usage in mg_url_port() --- mongoose.c | 9 +++++---- src/url.c | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mongoose.c b/mongoose.c index 9ee2eed3..7e612608 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4145,10 +4145,11 @@ const char *mg_url_uri(const char *url) { unsigned short mg_url_port(const char *url) { struct url u = urlparse(url); unsigned short port = 0; - if (memcmp(url, "http:", 5) == 0 || memcmp(url, "ws:", 3) == 0) port = 80; - if (memcmp(url, "wss:", 4) == 0 || memcmp(url, "https:", 6) == 0) port = 443; - if (memcmp(url, "mqtt:", 5) == 0) port = 1883; - if (memcmp(url, "mqtts:", 6) == 0) port = 8883; + if (strncmp(url, "http:", 5) == 0 || strncmp(url, "ws:", 3) == 0) port = 80; + if (strncmp(url, "wss:", 4) == 0 || strncmp(url, "https:", 6) == 0) + port = 443; + if (strncmp(url, "mqtt:", 5) == 0) port = 1883; + if (strncmp(url, "mqtts:", 6) == 0) port = 8883; if (u.port) port = (unsigned short) atoi(url + u.port); return port; } diff --git a/src/url.c b/src/url.c index c2064a59..22575dab 100644 --- a/src/url.c +++ b/src/url.c @@ -56,10 +56,11 @@ const char *mg_url_uri(const char *url) { unsigned short mg_url_port(const char *url) { struct url u = urlparse(url); unsigned short port = 0; - if (memcmp(url, "http:", 5) == 0 || memcmp(url, "ws:", 3) == 0) port = 80; - if (memcmp(url, "wss:", 4) == 0 || memcmp(url, "https:", 6) == 0) port = 443; - if (memcmp(url, "mqtt:", 5) == 0) port = 1883; - if (memcmp(url, "mqtts:", 6) == 0) port = 8883; + if (strncmp(url, "http:", 5) == 0 || strncmp(url, "ws:", 3) == 0) port = 80; + if (strncmp(url, "wss:", 4) == 0 || strncmp(url, "https:", 6) == 0) + port = 443; + if (strncmp(url, "mqtt:", 5) == 0) port = 1883; + if (strncmp(url, "mqtts:", 6) == 0) port = 8883; if (u.port) port = (unsigned short) atoi(url + u.port); return port; }