Consider port numbers with zero digits as empty, not 0 (fixes #3)

This commit is contained in:
2025-12-07 15:51:25 +01:00
parent 0182ee2beb
commit 880236a2b0
2 changed files with 44 additions and 23 deletions
+20
View File
@@ -202,6 +202,10 @@ static int json_to_url(cJSON *json, URL *url)
url->fragment.len--; url->fragment.len--;
} }
if (port.len == 0) {
url->no_port = 1;
url->port = 0;
} else {
char tmp[8]; char tmp[8];
if (port.len >= (int) sizeof(tmp)) { if (port.len >= (int) sizeof(tmp)) {
test_printf(" JSON object's port field is longer than expected\n"); test_printf(" JSON object's port field is longer than expected\n");
@@ -210,7 +214,9 @@ static int json_to_url(cJSON *json, URL *url)
memcpy(tmp, port.ptr, port.len); memcpy(tmp, port.ptr, port.len);
tmp[port.len] = 0; tmp[port.len] = 0;
url->no_port = 0;
url->port = atoi(tmp); url->port = atoi(tmp);
}
return 0; return 0;
} }
@@ -269,6 +275,20 @@ static int compare_urls(URL parsed, URL expected)
ok = false; ok = false;
} }
if (parsed.no_port != expected.no_port) {
if (ok) test_printf("\n");
if (parsed.no_port) {
test_printf(" port mismatch\n");
test_printf(" parsed (empty)\n");
test_printf(" expected [%d]\n", expected.port);
} else {
test_printf(" port mismatch\n");
test_printf(" parsed [%d]\n", parsed.port);
test_printf(" expected (empty)\n");
}
ok = false;
}
if (parsed.port != expected.port) { if (parsed.port != expected.port) {
if (ok) test_printf("\n"); if (ok) test_printf("\n");
test_printf(" port mismatch\n"); test_printf(" port mismatch\n");
+5 -4
View File
@@ -580,6 +580,9 @@ int url_parse(char *src, int len, int *pcur, URL *out, int flags)
out->no_port = 1; out->no_port = 1;
out->port = 0; out->port = 0;
if (cur < len && src[cur] == ':') { if (cur < len && src[cur] == ':') {
cur++; // Consume the ':'
if (cur < len && is_digit(src[cur])) {
// The WHATWG standard forbids port numbers with the // The WHATWG standard forbids port numbers with the
// "file" protocol. // "file" protocol.
@@ -588,17 +591,15 @@ int url_parse(char *src, int len, int *pcur, URL *out, int flags)
return -1; return -1;
} }
cur++; // Consume the ':'
out->no_port = 0; out->no_port = 0;
do {
while (cur < len && is_digit(src[cur])) {
int n = src[cur] - '0'; int n = src[cur] - '0';
cur++; cur++;
if (out->port > (UINT16_MAX - n) / 10) if (out->port > (UINT16_MAX - n) / 10)
return -1; // Overflow return -1; // Overflow
out->port = out->port * 10 + n; out->port = out->port * 10 + n;
} while (cur < len && is_digit(src[cur]));
} }
} }