Skip to content

Commit 0dc5fdf

Browse files
authored
uri: fix off-by-one in port bounds check (#140)
* uri: fix off-by-one in port bounds check sc_uri_create() rejected ports > 65536 instead of > 65535, so the invalid port 65536 was accepted as valid. Valid TCP/UDP ports are 0-65535 (16-bit range). * Lock URI port boundary behavior with regression coverage The off-by-one fix needs an executable test for both the maximum valid port and the first invalid port, so future changes cannot silently reopen the boundary. Constraint: The maintainer requested a regression test on PR #140 Rejected: Add a standalone test binary | The existing uri test target already covers the parser and keeps the change focused Confidence: high Scope-risk: narrow Reversibility: clean Directive: Keep 65535 and 65536 as adjacent explicit boundary cases Tested: Old origin/master fails at the 65536 assertion; fixed tree passes Linux GCC with ASan/UBSan, MinGW GCC 8, and official CMake/CTest Not-tested: Zig Windows workflow remains blocked by its upstream 153-byte download Related: #140 (comment)
1 parent f522219 commit 0dc5fdf

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

uri/sc_uri.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct sc_uri *sc_uri_create(const char *str)
9494

9595
errno = 0;
9696
val = strtoul(ptr + 1, &parse_end, 10);
97-
if (errno != 0 || val > 65536) {
97+
if (errno != 0 || val > 65535) {
9898
return NULL;
9999
}
100100

uri/uri_test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,22 @@ void test13(void)
246246
const char *f = "foo://user:password@example.com:-1/over/there?x#3";
247247
const char *f2 = "foo://user:password@example.com:100000/over/"
248248
"there?x#3";
249+
const char *f3 = "foo://example.com:65535";
250+
const char *f4 = "foo://example.com:65536";
249251

250252
uri = sc_uri_create(f);
251253
assert(uri == NULL);
252254

253255
uri = sc_uri_create(f2);
254256
assert(uri == NULL);
257+
258+
uri = sc_uri_create(f3);
259+
assert(uri != NULL);
260+
assert(strcmp(uri->port, "65535") == 0);
261+
sc_uri_destroy(uri);
262+
263+
uri = sc_uri_create(f4);
264+
assert(uri == NULL);
255265
}
256266

257267
#ifdef SC_HAVE_WRAP

0 commit comments

Comments
 (0)