From: Catherine Tower Date: Wed, 24 Aug 2022 01:39:33 +0000 (-0700) Subject: Ignore whitespace around components of host-and-port specification X-Git-Tag: v1.12.90~15 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F1521%2Fhead;p=tigervnc.git Ignore whitespace around components of host-and-port specification This is to make the code more tolerant of typos when entering a hostname --- diff --git a/common/rfb/Hostname.h b/common/rfb/Hostname.h index bd68dc02..341f69e1 100644 --- a/common/rfb/Hostname.h +++ b/common/rfb/Hostname.h @@ -20,12 +20,24 @@ #define __RFB_HOSTNAME_H__ #include +#include #include #include #include namespace rfb { + static bool isAllSpace(const char *string) { + if (string == NULL) + return false; + while(*string != '\0') { + if (! isspace(*string)) + return false; + string++; + } + return true; + } + static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) { const char* hostStart; const char* hostEnd; @@ -34,6 +46,10 @@ namespace rfb { if (hi == NULL) throw rdr::Exception("NULL host specified"); + // Trim leading whitespace + while(isspace(*hi)) + hi++; + assert(host); assert(port); @@ -44,7 +60,7 @@ namespace rfb { throw rdr::Exception("unmatched [ in host"); portStart = hostEnd + 1; - if (*portStart == '\0') + if (isAllSpace(portStart)) portStart = NULL; } else { hostStart = &hi[0]; @@ -65,6 +81,10 @@ namespace rfb { } } + // Back up past trailing space + while(isspace(*(hostEnd - 1)) && hostEnd > hostStart) + hostEnd--; + if (hostStart == hostEnd) *host = strDup("localhost"); else { @@ -87,7 +107,7 @@ namespace rfb { *port = strtol(portStart + 1, &end, 10); else *port = strtol(portStart + 2, &end, 10); - if (*end != '\0') + if (*end != '\0' && ! isAllSpace(end)) throw rdr::Exception("invalid port specified"); if ((portStart[1] != ':') && (*port < 100)) diff --git a/tests/unit/hostport.cxx b/tests/unit/hostport.cxx index af60643a..6ca5df8f 100644 --- a/tests/unit/hostport.cxx +++ b/tests/unit/hostport.cxx @@ -80,5 +80,17 @@ int main(int argc, char** argv) doTest("[2001:1234::20:1]:100", "2001:1234::20:1", 100); doTest("[2001:1234::20:1]:5901", "2001:1234::20:1", 5901); + doTest(" 1.2.3.4 ", "1.2.3.4", 5900); + doTest(" 1.2.3.4:5901 ", "1.2.3.4", 5901); + doTest(" 1.2.3.4: 5901 ", "1.2.3.4", 5901); + doTest(" 1.2.3.4 :5901 ", "1.2.3.4", 5901); + doTest(" [1.2.3.4]:5902 ", "1.2.3.4", 5902); + doTest(" :5903 ", "localhost", 5903); + doTest(" ::4 ", "localhost", 4); + doTest(" [::1] ", "::1", 5900); + doTest(" 2001:1234::20:1 ", "2001:1234::20:1", 5900); + doTest(" [2001:1234::20:1] ", "2001:1234::20:1", 5900); + doTest(" [2001:1234::20:1]:5905 ", "2001:1234::20:1", 5905); + return 0; }