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
+28 -8
View File
@@ -202,15 +202,21 @@ static int json_to_url(cJSON *json, URL *url)
url->fragment.len--;
}
char tmp[8];
if (port.len >= (int) sizeof(tmp)) {
test_printf(" JSON object's port field is longer than expected\n");
return -1;
}
memcpy(tmp, port.ptr, port.len);
tmp[port.len] = 0;
if (port.len == 0) {
url->no_port = 1;
url->port = 0;
} else {
char tmp[8];
if (port.len >= (int) sizeof(tmp)) {
test_printf(" JSON object's port field is longer than expected\n");
return -1;
}
memcpy(tmp, port.ptr, port.len);
tmp[port.len] = 0;
url->port = atoi(tmp);
url->no_port = 0;
url->port = atoi(tmp);
}
return 0;
}
@@ -269,6 +275,20 @@ static int compare_urls(URL parsed, URL expected)
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 (ok) test_printf("\n");
test_printf(" port mismatch\n");
+16 -15
View File
@@ -580,25 +580,26 @@ int url_parse(char *src, int len, int *pcur, URL *out, int flags)
out->no_port = 1;
out->port = 0;
if (cur < len && src[cur] == ':') {
// The WHATWG standard forbids port numbers with the
// "file" protocol.
if ((flags & URL_FLAG_RFC3986) == 0) {
if (streqcase(out->scheme, S("file")))
return -1;
}
cur++; // Consume the ':'
out->no_port = 0;
while (cur < len && is_digit(src[cur])) {
if (cur < len && is_digit(src[cur])) {
int n = src[cur] - '0';
cur++;
// The WHATWG standard forbids port numbers with the
// "file" protocol.
if ((flags & URL_FLAG_RFC3986) == 0) {
if (streqcase(out->scheme, S("file")))
return -1;
}
if (out->port > (UINT16_MAX - n) / 10)
return -1; // Overflow
out->port = out->port * 10 + n;
out->no_port = 0;
do {
int n = src[cur] - '0';
cur++;
if (out->port > (UINT16_MAX - n) / 10)
return -1; // Overflow
out->port = out->port * 10 + n;
} while (cur < len && is_digit(src[cur]));
}
}