Repository navigation
Reject out-of-range and wrapping port values (-p, --statsd-port, --graphite-port, --uri, --read-server) - #699
Conversation
config_parse_args() narrowed strtoul()'s result to unsigned short before range-checking it, so the "> 65535" test could never be true (gcc -Wtype-limits: "comparison is always false"). Any value above 65535 that did not wrap to exactly 0 was silently accepted modulo 65536: -p 65537 connected to port 1 and -p 88536 to port 23000. strtoul() also negates a leading '-', so -p -65535 meant port 1. --statsd-port and --graphite-port had the same cast and only rejected 0. Parse all three through one parse_port() helper that rejects empty input, a leading '-', trailing junk and ERANGE, and range-checks the unsigned long to [1,65535] before narrowing, as --prometheus-port already does. Valid inputs and the existing error messages are unchanged. Add tests/test_cli_validation_ports.py (uses --version, no server needed) and run it in the asan/tsan/ubsan TLS (cli) jobs. Fixes #697 Co-Authored-By: Claude Opus 5.5 <[email protected]>
strtoul() skips any leading whitespace and accepts a sign, and optarg_is_negative() only skips spaces and tabs, so "\n-18446744073709551615" still wrapped to port 1. parse_port() now requires the value to start with a digit, which also rejects "+6379" and " 6379" (plain decimal only, as documented). Tests cover the wrapping negatives, with and without leading whitespace, and the stale-errno case for --graphite-port. Co-Authored-By: Claude Opus 5.5 <[email protected]>
08c7400 to
a2a6b5b
Compare
|
Follow-up from review, now rebased on master:
|
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this. I went through it and the fix itself looks right to me. All five port inputs now go through a single One question on compatibility, since that's the part this project has historically been careful about. In PR#212 the less-breaking fix was chosen specifically because the stricter one would make previously-working command lines error out. Here, Nitpicking: The core change looks solid to me. I'd just like the leading-whitespace question answered before this merges. |
Both parsers checked the strtoul() result for [1,65535] but accepted a leading '-', which strtoul() negates modulo 2^64, so --uri=redis://host:-18446744073709551615 and --read-server=host:-18446744073709551615 connected to port 1. They now share parse_port() with -p, --statsd-port and --graphite-port. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The leading-digit check already guarantees strtoul() consumes at least one character, so endptr == s cannot be true. Also note in the stale errno test why --max-reconnect-attempts is used to leave ERANGE set. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Re the automated review of a2a6b5b:
|
The comment still described the leading '-' check and listed only three options. parse_port() now sits above its first caller, so the forward declaration is gone. Also clarify why the --uri test needs no server. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Re the automated review of cced292, fixed in the new head:
Local: |
|
Re the automated review posted at 11:12:57, which crossed with my reply at 11:12:02:
|
13 numeric options used strtoul()/strtoull() and a narrowing cast with no range or format check (#705, #695). Negative values wrapped to huge ones ("-1", or "\n-18446744073709551000" which slipped past the old leading-'-' check), values above the option's type were silently truncated (-c 4294967296 became 0), overflow was ignored, and an empty value meant 0, e.g. --max-reconnect-attempts= meaning "unlimited". All of them now go through one parser, parse_unsigned(), and parse_port() is built on it: - leading whitespace and a single '+' are accepted, as strtoul() always accepted them, so existing command lines keep working (the reasoning of #212); - any '-', an empty value, trailing junk, overflow and values above the option's maximum are rejected with exit 2; - the "unset" sentinels are excluded (-n <= ULLONG_MAX-1, --connection-stage-timeout <= UINT_MAX-1). Messages for -n/-c/-t/--test-time: "must be a positive integer" for malformed input, "must be greater than zero" for 0, "must be <= MAX" when too large. Port options (-p, --uri, --read-server, the prometheus port) follow the same rule, which restores what releases before the unreleased #699 accepted. Release note: an empty --max-reconnect-attempts= is now an error; pass 0 for unlimited. Tests cover every bypass vector per option, the accepted relaxed forms, the sentinels, the messages and stale-errno handling; they fail on master. The remaining unchecked options are tracked in #725. Fixes #705 Fixes #695 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Decision:
-p/--port,--statsd-port,--graphite-port, the--read-server HOST:PORTport and the--uriport now accept only digits, as a number in[1,65535]. Values above 65535, negative values, empty strings and trailing junk are rejected with the existing error messages. 0 is still rejected (it already was for all of them).Small compatibility change: a value with a leading sign or whitespace (
+6379," 6379","\t6379") used to be accepted and is now rejected. The value must start with a digit becausestrtoul()skips any leading whitespace (including\nand\v) and accepts a sign, so a check for a leading-alone could be bypassed:"\n-18446744073709551615"wrapped to port 1. Leading zeros (06379) are still accepted.--prometheus-portis unchanged here (0 means an ephemeral port); it has a related leading-whitespace hole tracked in #705.Problem
Values above 65535 that did not wrap to exactly 0 were silently accepted modulo 65536.
-p 65537connected to port 1, and-p 88536ran a normal benchmark against a Redis on port 23000. Negative values were accepted too:-p -65535meant port 1.--statsd-portand--graphite-porthad the same problem.--uriand--read-serverrange-checked correctly but still accepted a negative value that wraps into range:--uri=redis://host:-18446744073709551615connected to port 1. (#697 said these two were not affected; review found they were.)Root cause
config_parse_args()caststrtoul()'s result tounsigned shortbefore the range check:So only multiples of 65536 were rejected, because they wrap to 0.
strtoul()negates a leading-, so negative input wrapped as well.--statsd-portand--graphite-portused the same cast and only checked== 0.Fix
I added one small
parse_port()helper, next toparse_backoff_factor(), and all five port inputs use it. It follows the--prometheus-portpattern:ERANGE(it resetserrnofirst, so overflow from an earlier option cannot leak in)unsigned longto[1,65535]before narrowingThe
-Wtype-limitswarning at the old-pline goes away: building with-O0 -Wtype-limitsgives one warning inmemtier_benchmark.cppon master and none with this patch.Tests
New file
tests/test_cli_validation_ports.py. It uses--version, which exits right after parsing, so it needs no server. It covers:--port,--statsd-portand--graphite-port:65536 65537 88536 131072 70000 99999999999999999999999 -1 -65535 -0 " -1" 0 "" " " abc 1x "6379 " 0x50, the wrapping negatives-18446744073709551615and-18446744073709545916(also with\n/\vin front), and+6379 " 6379" "\t6379"--read-serverand--uriports: out of range, 0, a wrapping negative and a leading+; valid--read-serverspecs (including[::1]:6379) are accepted-p 65537|88536|-655351 6379 65535strtoul(the staleerrnocase)The file is added to the asan/tsan/ubsan "Single Endpoint: TCP TLS (cli)" and "OSS TCP TLS v1.3 (cli)" selections, next to the other
test_cli_validation_*files.Local results,
--env existing-env:make check7/7Direct matrix (
--<opt>=<v> --version, rc). The three options give the same result:+6379," 6379"and"\n-18446744073709551615": rc 0 on master, rc 2 with this PR.A real run with
-p <valid port>still completes normally.Fixes #697
Affected versions
1.0.0 through 2.5.2 (latest) and master.
-p/--port: the cast-then-compare goes back to 833a8df "First memtier_benchmark version". The first tag that contains it is 1.0.0.--statsd-port/--graphite-port: introduced by 3623256 "Add statsd metrics output ability (Add statsd metrics output ability #334)". The first tag that contains it is 2.3.0.-p, and 2.3.0-2.5.1 for the statsd/graphite ports.🤖 Generated with Claude Code
Note
Medium Risk
Changes argument parsing for all main connection/metrics ports; behavior shifts for previously accepted malformed values, but valid ports should behave the same.
Overview
Tightens CLI port parsing so
-p/--port,--statsd-port,--graphite-port,--urihost ports, and--read-serverHOST:PORTonly accept a plain decimal in 1–65535, checked on theunsigned longbefore casting tounsigned short.A shared
parse_port()helper replaces per-optionstrtoul+ early cast logic that let values like65537or wrapping negatives connect to the wrong port (#697). Inputs must start with a digit (rejecting signs, leading whitespace, and junk); 0 stays invalid.--prometheus-portis unchanged.Adds
tests/test_cli_validation_ports.py(mostly via--version) and wires it into the asan/tsan/ubsan “TCP TLS (cli)” matrix jobs.Reviewed by Cursor Bugbot for commit 9436446. Bugbot is set up for automated code reviews on this repo. Configure here.