Skip to content

Reject wrapped, overflowing and empty values on the remaining numeric options (#725) - #748

Merged
fcostaoliveira merged 8 commits into
masterfrom
fix/725-remaining-numeric-options
Oct 17, 2026
Merged

fcostaoliveira merged 8 commits into
masterfrom
fix/725-remaining-numeric-options

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Behaviour: same relaxed rule as #721. Leading whitespace and one + are still accepted (--select-db " 2", --key-minimum=+2), as strtoul() always accepted them (#212). Any negative value, empty input, trailing junk, ERANGE and values above the field's type are rejected with exit 2. -0 and -00 are accepted as 0, since strtoul() never wrapped them. --max-retries also accepts one leading -, because -1 means unlimited.

Release note (also in README, "Using memtier_benchmark"):

  • Breaking: --max-retries= (empty, for example an unset $VAR) used to mean 0 = retries disabled and is now an error. Values above 2147483647 are rejected. Before, 4294967295 became -1 (unlimited) and 4294967297 became 1.
  • Breaking: empty values are also rejected for --data-offset, --select-db, --thread-conn-start-min-jitter-micros, --thread-conn-start-max-jitter-micros, --scan-incremental-max-iterations, --replica-clients and --replicas-per-shard, where they used to mean 0. Pass 0 explicitly. -0 keeps meaning 0 on every option that goes through parse_unsigned(), including the options from Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721, which had rejected it on unreleased master only. Empty values are the only previously accepted, in-range input that is now rejected.
  • -1 (any negative except -0), overflow and values above the field's type are now rejected on all 15 options listed below, instead of wrapping or truncating.
  • --multi-key-get is capped at 16777214, the largest value whose preallocated per-client key buffer (256 * (N + 1) bytes) stays below 4 GiB. It is a memory sanity limit: the sizes are size_t now, and the cap sits where keylist's old 32-bit size wrapped, so values that used to work keep working. Memory is left to the user: about 272 bytes per key per client, as --help now says. Before, INT_MAX was accepted and reached about 31 GB RSS before it was killed.
  • --data-offset above 536870911 ((1 << 29) - 1) is rejected at parse time with "must be <= 536870911", instead of later with "data-offset too long".
  • --pipeline, -d and --reconnect-interval now print "must be a positive integer" for -1, empty values and trailing junk; they used to print "must be greater than zero". --rate-limiting errors now say "rate-limiting must be ..." instead of "rate must be ...".

Problem

#721 left 15 options on the old (T) strtoul()/strtoull()/strtol() pattern, which checked only *endptr: --data-offset, --key-minimum, --key-maximum, --thread-conn-start-{min,max}-jitter-micros, --multi-key-get, --select-db, --rate-limiting, --scan-incremental-max-iterations, --clients-start, --clients-step, --step-duration, --replica-clients, --replicas-per-shard and --max-retries. Examples:

  • --data-offset=-1 became 4294967295 and --key-minimum=-1 became 18446744073709551615.
  • --multi-key-get, --select-db and --rate-limiting set to 4294967297 all became 1.
  • --key-maximum=99999999999999999999999 saturated to ULLONG_MAX.

Root cause

These config_parse_args() cases had no sign or leading-digit check, no errno/ERANGE check and no range check before the narrowing cast. --max-retries used strtol() with only parsed < -1 and then cast to int.

Fix

  • Unsigned options now use parse_unsigned(s, max, &v, &too_large) from Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721, with max set to the field's type:

    • ULLONG_MAX for --key-minimum and --key-maximum;
    • INT_MAX for the int field --select-db;
    • 16777214 (MEMTIER_MULTI_KEY_GET_MAX) for --multi-key-get. keylist(multi_key_get + 1) preallocates 16 + 256 bytes per entry, and the unsigned 256 * entries buffer size wraps above 16777215 entries;
    • 536870911 (MEMTIER_DATA_OFFSET_MAX) for --data-offset, the limit main() used to check after parsing;
    • UINT_MAX for the rest.

    I checked each option for sentinels: the only one is 0, and its handling is unchanged. 0 is still rejected for the key range, --multi-key-get, --rate-limiting and the staircase options, and still accepted where --help documents it (--data-offset, the jitters, --select-db, --scan-incremental-max-iterations 0 = follow to the end, --replica-clients 0 = inherit, --replicas-per-shard 0 = all).

  • --max-retries uses a new signed counterpart, parse_signed(s, -1, INT_MAX, &v, &too_large). It follows the same rules as parse_unsigned(): leading whitespace, at most one sign, then a required digit. Unlike parse_unsigned(), which accepts - only for zero, any negative value in range is valid here. It resets errno and rejects junk, ERANGE and values outside the range.

  • Messages:

    • Options where 0 is rejected go through count_option_error(). As the follow-up on Remaining numeric options accept negative, overflowing and wrapping values (unchecked strtoul/strtoull/strtol) #725 suggested, this now includes --pipeline, -d and --reconnect-interval. So -1, 5x and "" give "must be a positive integer", 0 gives "must be greater than zero", and a value that is too large gives "must be <= MAX". count_option_error() takes an optional unit, which keeps -d's "<= 536870912 bytes (512 MiB)".
    • Options where 0 is valid say "must be a non-negative integer" or "must be <= MAX".
    • --max-retries keeps its "integer >= -1 (-1 means unlimited)" message and adds "must be <= 2147483647".
    • tests/test_cli_validation_size.py now expects "positive integer" for --pipeline=-1 and --data-size=-1.
  • Errno-leak tests: they used --key-maximum=99999999999999999999999 to leave errno == ERANGE behind, and that value is now rejected. They now use --key-median=1e-310, which is accepted, while strtod() sets ERANGE for the subnormal result. C leaves that up to the implementation, so the guard in count_flags checks both that the value is still accepted and, through ctypes, that this host's libc sets ERANGE for it. The same test now also runs --max-retries after the stale errno, which covers parse_signed().

  • SIGFPE at the maxima (also on master): --rate-limiting above UINT_MAX - 49 made (request_rate + 49) / 50 wrap to 0, and the next division by it crashed. A --thread-conn-start-*-jitter-micros range of UINT_MAX made rand() % (range + 1) become rand() % 0. Both now do that math in 64 bits.

  • --replica-clients and --replicas-per-shard messages drop the -- prefix, like every other option.

  • Full 64-bit key range (also on master): --key-maximum=18446744073709551615 with the default --key-minimum 0 made random_range() compute rn % 0 (SIGFPE with R:R). gaussian_distribution_range() rejected every sample against max + 1 == 0, so G:G hung and ignored SIGTERM. Now a wrapped span means "any rn", the Gaussian bound is compared in double, and Gaussian and Zipf results are clamped to the maximum, so no double of 2^64 or more is converted. S:S and P:P were checked and do not wrap. get_random() yields 62 bits, so R:R over more than 2^62 keys is not uniform, as before; the comment now says so.

  • MGET keylist growth hang (also on master): keylist::add_key() doubled an unsigned int buffer size, which wraps to 0 past 2 GiB, and the growth loop never ended. It is reachable under the cap, for example with --multi-key-get=8388607 and ~260-byte keys. The size is now size_t and grows through keylist_grow_size() (new keylist_growth.h), which stops doubling before size_t overflows. The new unit test tests/unit/keylist_growth_test.cpp (make check) covers the 2^31 step and the SIZE_MAX edge without allocating; a mutant with an unsigned int size hangs it. A static_assert in keylist keeps m_buffer_size at least size_t wide; reverting the member to unsigned int passed every runtime test, and a real --multi-key-get=8388607 run with 250-byte keys then aborted. That run now exits 0 at ~4.5 GB RSS. A failed key-list allocation prints "out of memory building MGET key list" and exits 1 instead of asserting.

Tests

New tests in tests/test_cli_validation_count_flags.py, already in the sanitizer workflow lists:

test checks
test_remaining_options_reject_wrapped_values 17 options × the #721 vectors ("\n-18446744073709551000", "\n-1", "\v-1", -1, " -5", "- 5", "+-5", "++5", "+ 5", "", " ", 5x, "5 ", overflow) → exit 2 + the option's message
test_remaining_options_reject_values_above_their_type max+1, 4294967297 and overflow → "must be <= MAX" (UINT_MAX / INT_MAX / ULLONG_MAX / cap)
test_remaining_options_messages "positive integer" / "greater than zero" / "non-negative integer" and the -d unit text
test_remaining_options_accept_valid_values 1, the type maximum, " 5", "\t5", "\n5", "+5", " +5", and 0 where valid
test_max_retries_rejects_out_of_range_and_malformed -2, "\n-2", -4294967295, signed overflow both ways, "- 1", "+-1", "-+1", "--1", "++1", "-", "+", "", " ", 5x, -1x; and 2147483648, 4294967295, 4294967297 → "<= 2147483647"
test_negative_zero_is_zero -0, -00, " -0", "\n-0" accepted where 0 is valid and stored as 0; "must be greater than zero" where it is not
test_max_retries_accepts_valid_values -1, " -1", "\t-1", 0, +0, -0, 1, " +5", 2147483647
test_rate_limiting_max_does_not_divide_by_zero --rate-limiting=4294967247 and =4294967295 against a closed port: no SIGFPE
test_jitter_max_range_does_not_divide_by_zero jitter 0..4294967295, -c 2: no SIGFPE
test_multi_key_get_cap 2000000 and 16777214 are accepted; 16777215, 16777216 and 2147483647 are rejected with "must be <= 16777214"
test_data_offset_cap 536870911 is accepted; 536870912 and 4294967295 are rejected with "must be <= 536870911"
test_remaining_options_store_their_maximum --show-config (which now also prints the staircase, scan and replica fields) shows each option's full maximum, all above 65535, so a narrowed or dropped store is caught
test_key_maximum_full_range_runs a real -n 3 against redis with --key-maximum=18446744073709551615 and R:R, G:G, Z:Z, S:S, P:P exits 0 (master: R:R SIGFPE, G:G hangs)
test_remaining_options_store_the_parsed_value --show-config shows the given value: max_retries = -1 (sentinel kept), = 2147483647, = 7 for +7, key_maximum = 18446744073709551615, select-db = 2147483647, rate_limit = 9 for " 9", ...

RLTest --env oss, port 23500:

suite origin/master 38dfbaa this PR (316559b)
test_cli_validation_count_flags.py 11/28 failed (including the SIGFPE and full-key-range tests) 28/28 passed (--tls last checked at 9d17d4c: 27/27)
make check (unit tests, new keylist_growth_test) n/a 11/11 passed
test_cli_validation_size.py (updated -1 messages) 2/8 failed 8/8 passed
ports, reconnect_backoff, retry_backoff, sampler, mget_validation, cli_fuzz_phase1 pass pass

test_remaining_options_store_the_parsed_value also passes on master. It guards the accepted forms and sentinels, not the bug.

Mutation check: with errno = 0 removed from parse_unsigned() and parse_backoff_factor(), all four errno-leak tests (count_flags, ports, reconnect_backoff, retry_backoff) fail with the new --key-median=1e-310 source. With errno = 0 removed from parse_signed(), test_unsigned_options_ignore_errno_from_earlier_options fails (--max-retries=5 → "must be <= 2147483647").

Fixes #725

Follow-ups (not in this PR)

  • The connection jitter is drawn with rand(), which only reaches RAND_MAX (2^31 - 1 on glibc). A jitter range above that never sleeps near its top. This is unchanged here; the 64-bit modulo only removes the SIGFPE.
  • The --uri database number (redis://host:port/N) still goes through (int) strtol. It will move to the --select-db rule after Redact the password in --json-out-file and --show-config #735.

Affected versions

Every release through 2.5.2, and master (from the issue, git log -S then git tag --contains):

Reproduced on master 193f6a8 (the issue's exit-code table is unchanged after #721). The older versions come from history and were not run.

🤖 Generated with Claude Code


Note

Medium Risk
Wide CLI parsing changes include intentional breaking behavior for empty and out-of-range values; runtime fixes touch key generation and large MGET allocations, though coverage is extensive.

Overview
Hardens numeric CLI parsing (#725) by routing the remaining count/size options through shared parse_unsigned() / new parse_signed() (for --max-retries), with clearer errors, caps (--multi-key-get ≤ 16777214, --data-offset at parse time), and documented breaking behavior for empty values and overflow instead of silent wrap/truncate. -0 is still accepted as zero.

Fixes integer-overflow crashes and hangs that showed up at extreme settings: 64-bit math for rate-limiting and connection-start jitter (% 0), safe full-range key sampling in obj_gen (Gaussian/Zipf/random), and MGET keylist buffer growth on size_t via keylist_grow_size() with OOM handling instead of asserts/infinite doubling.

Docs and tests update README/manpage/--help, extend --show-config/JSON for staircase and replica fields, add keylist_growth_test, and greatly expand test_cli_validation_count_flags.py (including live Redis runs for max key range and SIGFPE guards).

Reviewed by Cursor Bugbot for commit 316559b. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I went through most of it and only have a few comments.

  1. The PR description and some commit messages no longer match the code. After the last commit (316559b), parse_unsigned() accepts -0/-00 again, and the README says so. The description still says "Any - ... rejected" near the top, and its first Breaking bullet says "empty values, and -0, are now rejected" and that "On these options -0 ... is now rejected too". Since the release note is meant to be copied from here, I think it's worth fixing before merge. Also, the first commit message says INT_MAX for --multi-key-get and a251ecb says the cap is 1000000; both were later replaced by 16777214. If this isn't squashed, the history will contradict itself.

  2. Compatibility of empty values. You applied the Fix issue #211. Allow key_stddev and key_median options to be set values #212 reasoning (keep command lines that used to work working) to -0, which I like. The only breaking change left is that empty values now error out. I think that's defensible: an empty string that silently means 0 can't be told apart from bad input. The one I'd look at twice is --max-retries=. An unset $VAR used to mean "retries disabled", and now the run fails. That's loud rather than silent, so probably fine, but did you consider keeping empty as 0 just for options where 0 is valid? If not, the README wording is good as is.

  3. Scope. Besides the parser changes, this now fixes three runtime bugs that are also on master: the --rate-limiting and jitter SIGFPEs, the full 64-bit key range SIGFPE/hang in random_range() and the Gaussian/Zipf paths, and the keylist::add_key() growth hang. Each has its own test, so I'm not asking for a split. But the obj_gen.cpp and protocol.cpp changes touch key generation and MGET building, not just CLI parsing, so they may be worth a second look from whoever owns that code.

  4. obj_gen.cpp: I checked the Gaussian clamp. For ranges that don't wrap, val >= (double) max → max gives the same result truncation gave for [max, max+1), and it also fixes the val == max + 1 edge case. The extra span == 0 branch in random_range() is well predicted, so I don't think it matters on the hot path.

  5. Nitpicking: in client.cpp the 64-bit modulo fixes the SIGFPE, but rand() only goes up to RAND_MAX. So with a jitter range above about 2^31 the sleep never reaches the top of the range. This was already true before; maybe worth a word in the comment, since the PR now advertises UINT_MAX as valid there.

The --show-config lines and the conditional scan_incremental_max_iterations JSON key are additions only, so I don't see a compatibility problem there. The new tests, and the mutation checks in the description, cover the actual failing cases, which is the bar #364 set.

The code looks solid to me. I'd mainly want the description/release note updated for the -0 change before this merges.

(would file this as a comment, not a formal block)

@fcostaoliveira
fcostaoliveira force-pushed the fix/725-remaining-numeric-options branch from 86b872f to 87f814f Compare October 16, 2026 21:06
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…igned errno

Review of #748:
- --rate-limiting above UINT_MAX - 49 made (request_rate + 49) / 50 wrap
  to 0 and request_rate / 0 raise SIGFPE. The PR advertises UINT_MAX as
  valid, so do the rounding in 64 bits.
- A --thread-conn-start-*-jitter-micros range of UINT_MAX made
  jitter_range + 1 wrap to 0 and rand() % 0 raise SIGFPE. Same fix.
- The errno-leak test now also runs --max-retries after the stale ERANGE;
  removing errno = 0 from parse_signed() fails it.
- C leaves ERANGE for a subnormal strtod() result implementation-defined,
  so the guard now checks, through ctypes, that this libc sets it for
  1e-310. 1e999 (where ERANGE is required) was not used because
  --key-median accepting inf is itself a bug that will be fixed.
- Messages for --replica-clients/--replicas-per-shard drop the "--"
  prefix, like the other options.
- README: "-0" is now rejected on the options where 0 is valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Both points are addressed in 87f814f.

Errno source. You're right: C leaves ERANGE for a subnormal strtod() result up to the implementation. I kept --key-median=1e-310, and the guard in test_unsigned_options_ignore_errno_from_earlier_options now also calls this libc's strtod("1e-310") through ctypes and fails loudly if it does not set ERANGE. So on a libc that doesn't set it, the errno-leak tests can no longer pass without testing anything.

I didn't switch to 1e999. It is accepted today only because --key-median (and --key-zipf-exp) accept inf/nan: <= 0 is false for both. That is a separate bug to fix, and once it's fixed the errno source would break again. The same test now also runs --max-retries=5 and --max-retries=-1 after the stale ERANGE. Removing errno = 0 from parse_signed() makes it fail; I checked that.

-- prefix. Fixed. The messages are now replica-clients must be ... and replicas-per-shard must be ..., like the other options.

The same push also fixes two SIGFPEs at the newly advertised maxima. Both also happen on master:

  • --rate-limiting above UINT_MAX - 49: (request_rate + 49) / 50 wrapped to 0, and the next division by it crashed.
  • A jitter range of UINT_MAX: rand() % (range + 1) became rand() % 0.

Both now do that math in 64 bits, and there are regression tests for each.

🤖 Generated with Claude Code

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Both are addressed in 183f9d6.

--multi-key-get maximum. You're right, INT_MAX was not safe. Every client preallocates keylist(multi_key_get + 1): 16 bytes of key_entry plus 256 bytes of key buffer per entry. The unsigned int 256 * m_keys_size wraps above 16777215 entries. Well before that, the startup allocation runs out of memory: on master, --multi-key-get=2147483647 -c1 -t1 reached about 31 GB RSS and was killed before it connected.

I capped it at MEMTIER_MULTI_KEY_GET_MAX = 1000000. That is about 272 MB per client, comfortably below both the wrap and the multi_key_get + 1 signed overflow. Larger values now fail with error: multi-key-get must be <= 1000000..

  • --help, the man page and the README release note say so.
  • test_multi_key_get_cap checks that 1000000 is accepted and that 1000001, 16777215 and 2147483647 are rejected.
  • test_remaining_options_store_the_parsed_value runs --multi-key-get=1000000 against a closed port and checks multi_key_get = 1000000. That run allocates the keylist, so the cap value itself is exercised.

-0. Yes, it's intentional. For a signed option - is a legal sign, and strtoll("-0") is just 0 with no wrap. parse_unsigned() rejects every - because strtoull() negates. There's now a comment saying this next to parse_signed().

🤖 Generated with Claude Code

fcostaoliveira pushed a commit that referenced this pull request Oct 16, 2026
…et at their real bounds

Review of #748:
- --key-maximum=18446744073709551615 with the default --key-minimum 0 is
  a full 64-bit range. random_range() computed rn % (max - min + 1),
  which wraps to % 0 (SIGFPE with R:R), and gaussian_distribution_range()
  rejected every sample against max + 1 == 0, so G:G hung (it ignores
  SIGTERM). Treat a wrapped span as "any rn", compare against max + 1 in
  double, and clamp the result so a double >= 2^64 is never converted.
  Sequential and parallel patterns were checked and do not wrap.
- --multi-key-get is capped at 16777214, the largest value for which
  keylist's unsigned 256 * (N + 1) buffer size does not wrap, instead
  of 1000000: values that used to work keep working (#212). --help and
  the man page note the ~272 bytes per key per client.
- --data-offset is capped at (1 << 29) - 1 in the parser, so its
  "must be <= N" is true; the later "data-offset too long" check in
  main() is gone.
- --rate-limiting errors say "rate-limiting must be ...".
- README: the message change for --pipeline, -d and
  --reconnect-interval.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Answers to the three points, all in 8301d5a.

1. --key-maximum=18446744073709551615 with the default --key-minimum. Confirmed, and there were two bugs, not one. With 0..ULLONG_MAX:

  • random_range() computed rn % 0, so R:R died with SIGFPE (rc 136).
  • gaussian_distribution_range() compared each sample with max + 1, which is 0 in unsigned long long. Every sample was rejected, so G:G spun forever. It ignored SIGTERM and needed SIGKILL.

I made the arithmetic safe instead of capping the range:

  • A wrapped span means the range covers every value, so any rn is in range.
  • The Gaussian bound is compared in double, and the result is clamped so a value of 2^64 or more is never converted to unsigned long long.

S:S and P:P were already fine; I checked both. test_key_maximum_full_range_runs runs a real -n 3 against redis with R:R, G:G, S:S and P:P (2 clients for P:P). All four exit 0. On the previous head, R:R exits 136 and G:G hangs.

2. --multi-key-get cap. Agreed, a 1000000 cap would break command lines that work today. The cap is now the correctness bound, 16777214. keylist(multi_key_get + 1) allocates 256 * entries bytes in an unsigned int, which wraps at 16777216 entries, so 16777215 + 1 is the first value that breaks. Memory is left to the user, and --help and the man page now say about 272 bytes per key per client. The test checks that 2000000 and 16777214 are accepted and that 16777215, 16777216 and 2147483647 fail with "must be <= 16777214".

3. Helper refactor. I left it out on purpose. The block is three lines per option, and keeping each option's error call next to its own bound makes the diff easy to check one option at a time. I'm happy to fold it in if a maintainer prefers.

Also in this push:

  • --data-offset is capped in the parser at (1 << 29) - 1, the limit main() used to check later as "data-offset too long", so "must be <= 536870911" is true.
  • --rate-limiting errors now say "rate-limiting must be ...".

🤖 Generated with Claude Code

fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…igned errno

Review of #748:
- --rate-limiting above UINT_MAX - 49 made (request_rate + 49) / 50 wrap
  to 0 and request_rate / 0 raise SIGFPE. The PR advertises UINT_MAX as
  valid, so do the rounding in 64 bits.
- A --thread-conn-start-*-jitter-micros range of UINT_MAX made
  jitter_range + 1 wrap to 0 and rand() % 0 raise SIGFPE. Same fix.
- The errno-leak test now also runs --max-retries after the stale ERANGE;
  removing errno = 0 from parse_signed() fails it.
- C leaves ERANGE for a subnormal strtod() result implementation-defined,
  so the guard now checks, through ctypes, that this libc sets it for
  1e-310. 1e999 (where ERANGE is required) was not used because
  --key-median accepting inf is itself a bug that will be fixed.
- Messages for --replica-clients/--replicas-per-shard drop the "--"
  prefix, like the other options.
- README: "-0" is now rejected on the options where 0 is valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/725-remaining-numeric-options branch from e7f852c to 17b7081 Compare October 16, 2026 22:40
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…et at their real bounds

Review of #748:
- --key-maximum=18446744073709551615 with the default --key-minimum 0 is
  a full 64-bit range. random_range() computed rn % (max - min + 1),
  which wraps to % 0 (SIGFPE with R:R), and gaussian_distribution_range()
  rejected every sample against max + 1 == 0, so G:G hung (it ignores
  SIGTERM). Treat a wrapped span as "any rn", compare against max + 1 in
  double, and clamp the result so a double >= 2^64 is never converted.
  Sequential and parallel patterns were checked and do not wrap.
- --multi-key-get is capped at 16777214, the largest value for which
  keylist's unsigned 256 * (N + 1) buffer size does not wrap, instead
  of 1000000: values that used to work keep working (#212). --help and
  the man page note the ~272 bytes per key per client.
- --data-offset is capped at (1 << 29) - 1 in the parser, so its
  "must be <= N" is true; the later "data-offset too long" check in
  main() is gone.
- --rate-limiting errors say "rate-limiting must be ...".
- README: the message change for --pipeline, -d and
  --reconnect-interval.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
Review of #748:
- keylist::add_key() doubled an unsigned int buffer size, which wraps to
  0 past 2 GiB and spins the growth loop forever. With the
  --multi-key-get cap at 16777214 that is reachable, e.g.
  --multi-key-get=8388607 with ~260-byte keys. The buffer size is now
  size_t and grows through keylist_grow_size(), which stops doubling
  before size_t overflows. tests/unit/keylist_growth_test.cpp covers the
  2^31 step and the SIZE_MAX edge without allocating; a real run would
  need several GB per client. The cap comment and release note now say
  exactly what 16777214 guarantees: the preallocated buffer stays below
  4 GiB.
- Zipf samples are clamped to the range maximum, as Gaussian ones are:
  at --key-maximum=ULLONG_MAX a sample can be the double 2^64, whose
  conversion to unsigned long long is undefined. The full-range run test
  now also covers Z:Z.
- random_range() documents that get_random() yields 62 bits, so spans
  above 2^62 are not uniform (unchanged behaviour).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Replying point by point to the review. Everything is in 17b7081, and the branch is rebased on a7881e0.

1. Zipf with the full 64-bit range. Yes, the same undefined conversion applied there. Every Zipf sample now goes through zipf_key(), which clamps to m_key_zipf_max the same way the Gaussian path does, so the double 2^64 is never converted. test_key_maximum_full_range_runs now also runs Z:Z, alongside R:R, G:G, S:S and P:P. It does a real -n 3 against redis with --key-maximum=18446744073709551615, and every pattern exits 0, including under --tls.

2. random_range() comment. Agreed, it shouldn't suggest uniform coverage. The comment now says that get_random() yields 62 bits (get_random_max()), so the result is only uniform for spans up to 2^62. Above that it never exceeds r_min + 2^62 - 1, and spans that don't divide 2^62 lean toward the low end, as they always did. span == 0 returning rn only avoids rn % 0.

3. Empty --max-retries= and -0. Rejecting them is a deliberate choice, not done just to match #721:

  • On master, strtol("") gave 0 with *endptr == '\0', so --max-retries= (for example an unset $RETRIES) silently meant 0 = retries disabled. That is the opposite of the default (-1, unlimited), and nothing told the user. Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721 rejected --max-reconnect-attempts= for the same reason: there, empty meant "unlimited".
  • An empty value is the typical sign of a broken script variable, so a clear error is safer than quietly changing retry behaviour.
  • Fix issue #211. Allow key_stddev and key_median options to be set values #212 was about keeping valid spellings working. Those still work here: leading whitespace, +N, and -1 for --max-retries.
  • -0 is accepted by --max-retries, where - is a legal sign. It is rejected only on the unsigned options, by the same rule parse_unsigned() already applies to the options from Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721: any - there is a negative value that strtoull() would wrap.
  • Both are in the README release note. If maintainers prefer an empty --max-retries= to mean the default, that's a one-line change; I didn't make it because it would silently change behaviour instead of reporting the problem.

4. Authorship. Fixed. Those commits came from a stray local git identity. I rewrote the branch so every commit's author and committer is fcostaoliveira <[email protected]>, checked with git log --format='%ae %ce'.

5. Message change for --pipeline, -d, --reconnect-interval. It's intentional, it follows the follow-up on #725, and the README lists it.

The same push also fixes a hang the --multi-key-get cap made reachable. keylist::add_key() doubled an unsigned int buffer size, which wraps to 0 past 2 GiB, and the growth loop then never ends. For example, --multi-key-get=8388607 with ~260-byte keys hangs.

  • The size is now size_t and grows through keylist_grow_size(), which stops doubling before size_t would overflow.
  • A new unit test, tests/unit/keylist_growth_test.cpp, runs in make check. It covers the 2^31 step and the SIZE_MAX edge without allocating. A mutant that keeps the size in unsigned int hangs it. (Correction: this applies to the helper's local size variable only. Reverting the keylist::m_buffer_size member to unsigned int passed every test; 9d17d4c adds a static_assert for that. See below.)
  • A real-run test would need several GB per client, so there isn't one.
  • The cap comment and release note now say exactly what 16777214 guarantees: the preallocated per-client buffer stays below 4 GiB.

🤖 Generated with Claude Code

fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
…not grow

Review of #748:
- Reverting keylist::m_buffer_size to unsigned int passed every test, so
  a static_assert in keylist now requires it to be at least size_t
  wide; the helper's unit test only covers the growth arithmetic.
- A failed malloc/realloc of the MGET key list (GBs with a large
  --multi-key-get and long keys) prints "out of memory building MGET key
  list" and exits 1 instead of asserting.
- The --multi-key-get cap comment says what it is now: a memory sanity
  limit that keeps the preallocated key buffer under 4 GiB.
- The JSON configuration also records scan_incremental_max_iterations,
  like --show-config.
- Test docstrings: the Z:Z case only checks that the full range runs
  (its clamp is defensive), and the jitter test passes on the timeout.
  Blank lines fixed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the re-review. Everything is in 9d17d4c.

Correction to my last reply. I said that keeping the size in unsigned int hangs the new unit test. That is true only for the helper's local variable. Reverting the actual member, keylist::m_buffer_size, back to unsigned int passed make check and all the MGET RLTests. A real run with -n 1 --ratio 0:8388607 --multi-key-get=8388607, a 250-character key prefix and --key-maximum=100000000 then aborted. keylist now has static_assert(sizeof(m_buffer_size) >= sizeof(size_t), ...), so that revert fails to compile (checked). The same run exits 0 at ~4.5 GB RSS. I've also corrected the earlier comment.

  • Compat (empty --max-retries=, -0). Both are now called out explicitly in the release-note section of the PR description, not only in the README paragraph. I agree failing loudly is right for --max-retries=: empty used to mean "retries disabled", the opposite of the default. -0 is rejected only on the unsigned options, consistent with Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721.
  • --multi-key-get cap. You're right that it is now a memory sanity limit, not an overflow guard. The sizes are size_t, and the key_entry array was already sized in size_t (m_keys_size * sizeof(key_entry)), so nothing wraps above 16777214. The cap keeps the preallocated key buffer just under 4 GiB, about 4.3 GiB per client with the index. It sits where the old 32-bit size wrapped, so every value that ever worked still does. The MEMTIER_MULTI_KEY_GET_MAX comment and the README now say that plainly.
  • Scope. Acknowledged. The key-generation fixes (random_range(), Gaussian, Zipf, keylist growth) are separable, and a second look from whoever owns key generation is welcome. I kept them here because the new advertised maxima (--key-maximum=ULLONG_MAX, the --multi-key-get cap) are what made them reachable.
  • JSON config. Of the new --show-config fields, the JSON configuration already had replica_clients, replicas_per_shard and (in staircase mode) clients_start/clients_step/step_duration. It was missing scan_incremental_max_iterations, which is now added. The text dump only appends at the end, as you noted.
  • Jitter test. The docstring now says that hitting the 5 s timeout is the expected pass path, since no smaller range has a 2^32 span.
  • Blank lines. Fixed.

Also in this push:

  • A failed malloc/realloc of the MGET key list now prints "error: out of memory building MGET key list (reduce --multi-key-get or the key size)." and exits 1, instead of asserting.
  • The Z:Z full-range case is documented as checking only that the range runs. The Zipf clamp is defensive: reaching a 2^64 sample needs p within ~1e-16 of 1, which no test can force.

🤖 Generated with Claude Code

fcostaoliveira and others added 8 commits October 17, 2026 00:37
… options

#721 moved 13 options onto parse_unsigned(), but 15 more still did
(T) strtoul()/strtoull()/strtol() and checked only *endptr (#725). So
"-1" wrapped (--data-offset=-1 became 4294967295, --key-minimum=-1
became ULLONG_MAX), values above the field's type were truncated by the
cast (--multi-key-get, --select-db, --rate-limiting=4294967297 became
1), overflow saturated, and an empty value meant 0. --max-retries used
strtol() with only a "< -1" check, so 4294967295 became -1 (unlimited)
and 4294967297 became 1.

Unsigned options now use parse_unsigned() with the field's maximum:
ULLONG_MAX for --key-minimum/--key-maximum, INT_MAX for the int fields
--multi-key-get and --select-db, UINT_MAX for the rest. None of them has
a sentinel other than 0, whose handling is unchanged. --max-retries uses
a new signed counterpart, parse_signed(), over [-1, INT_MAX]; it
accepts the same relaxed forms (leading whitespace, one sign).

Messages: options where 0 is rejected (including --pipeline, -d and
--reconnect-interval, as noted on #725) now go through
count_option_error(), so "-1" and "5x" say "must be a positive integer",
0 says "must be greater than zero" and too-large values name the limit.
Options where 0 is valid say "must be a non-negative integer" or
"must be <= MAX".

The errno-leak tests used --key-maximum=99999999999999999999999 to leave
errno == ERANGE behind; that is now rejected, so they use
--key-median=1e-310 (accepted, strtod() sets ERANGE for the subnormal
result). Removing errno = 0 from parse_unsigned() and
parse_backoff_factor() still fails all four of them.

Release note: empty values are now rejected for --max-retries (used to
mean 0, retries disabled), --data-offset, --select-db,
--thread-conn-start-{min,max}-jitter-micros,
--scan-incremental-max-iterations, --replica-clients and
--replicas-per-shard (used to mean 0); --max-retries above 2147483647
is rejected instead of wrapping.

Fixes #725

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…igned errno

Review of #748:
- --rate-limiting above UINT_MAX - 49 made (request_rate + 49) / 50 wrap
  to 0 and request_rate / 0 raise SIGFPE. The PR advertises UINT_MAX as
  valid, so do the rounding in 64 bits.
- A --thread-conn-start-*-jitter-micros range of UINT_MAX made
  jitter_range + 1 wrap to 0 and rand() % 0 raise SIGFPE. Same fix.
- The errno-leak test now also runs --max-retries after the stale ERANGE;
  removing errno = 0 from parse_signed() fails it.
- C leaves ERANGE for a subnormal strtod() result implementation-defined,
  so the guard now checks, through ctypes, that this libc sets it for
  1e-310. 1e999 (where ERANGE is required) was not used because
  --key-median accepting inf is itself a bug that will be fixed.
- Messages for --replica-clients/--replicas-per-shard drop the "--"
  prefix, like the other options.
- README: "-0" is now rejected on the options where 0 is valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The PR advertised INT_MAX for --multi-key-get, but every client
preallocates keylist(multi_key_get + 1): 16 bytes of key_entry plus a
256-byte key buffer per entry. The unsigned 256 * entries buffer size
wraps above 16777215 entries, and well before that the startup
allocation exhausts memory (INT_MAX reached ~31 GB RSS on one client
before being killed). Cap it at 1000000 (~272 MB per client) with
"multi-key-get must be <= 1000000.", and say so in --help, the man page
and the README release note.

Also comment why parse_signed() accepts "-0" while parse_unsigned()
rejects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…et at their real bounds

Review of #748:
- --key-maximum=18446744073709551615 with the default --key-minimum 0 is
  a full 64-bit range. random_range() computed rn % (max - min + 1),
  which wraps to % 0 (SIGFPE with R:R), and gaussian_distribution_range()
  rejected every sample against max + 1 == 0, so G:G hung (it ignores
  SIGTERM). Treat a wrapped span as "any rn", compare against max + 1 in
  double, and clamp the result so a double >= 2^64 is never converted.
  Sequential and parallel patterns were checked and do not wrap.
- --multi-key-get is capped at 16777214, the largest value for which
  keylist's unsigned 256 * (N + 1) buffer size does not wrap, instead
  of 1000000: values that used to work keep working (#212). --help and
  the man page note the ~272 bytes per key per client.
- --data-offset is capped at (1 << 29) - 1 in the parser, so its
  "must be <= N" is true; the later "data-offset too long" check in
  main() is gone.
- --rate-limiting errors say "rate-limiting must be ...".
- README: the message change for --pipeline, -d and
  --reconnect-interval.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ts maximum

- test_key_maximum_full_range_runs ran memtier against the env's server
  without TLS args, so it failed with "Connection reset by peer" in every
  TLS CI cell. It now builds the command through addTLSArgs(). The other
  real-run tests in the file use a closed port and need no server.
- --show-config now also prints clients_start, clients_step,
  step_duration, scan_incremental_max_iterations, replica_clients and
  replicas_per_shard, and test_remaining_options_store_their_maximum
  checks that each option stores its full maximum (all above 65535), so
  a store narrowed to a smaller type or dropped is caught.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #748:
- keylist::add_key() doubled an unsigned int buffer size, which wraps to
  0 past 2 GiB and spins the growth loop forever. With the
  --multi-key-get cap at 16777214 that is reachable, e.g.
  --multi-key-get=8388607 with ~260-byte keys. The buffer size is now
  size_t and grows through keylist_grow_size(), which stops doubling
  before size_t overflows. tests/unit/keylist_growth_test.cpp covers the
  2^31 step and the SIZE_MAX edge without allocating; a real run would
  need several GB per client. The cap comment and release note now say
  exactly what 16777214 guarantees: the preallocated buffer stays below
  4 GiB.
- Zipf samples are clamped to the range maximum, as Gaussian ones are:
  at --key-maximum=ULLONG_MAX a sample can be the double 2^64, whose
  conversion to unsigned long long is undefined. The full-range run test
  now also covers Z:Z.
- random_range() documents that get_random() yields 62 bits, so spans
  above 2^62 are not uniform (unchanged behaviour).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…not grow

Review of #748:
- Reverting keylist::m_buffer_size to unsigned int passed every test, so
  a static_assert in keylist now requires it to be at least size_t
  wide; the helper's unit test only covers the growth arithmetic.
- A failed malloc/realloc of the MGET key list (GBs with a large
  --multi-key-get and long keys) prints "out of memory building MGET key
  list" and exits 1 instead of asserting.
- The --multi-key-get cap comment says what it is now: a memory sanity
  limit that keeps the preallocated key buffer under 4 GiB.
- The JSON configuration also records scan_incremental_max_iterations,
  like --show-config.
- Test docstrings: the Z:Z case only checks that the full range runs
  (its clamp is defensive), and the jitter test passes on the timeout.
  Blank lines fixed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… JSON only with scanning

Review of #748:
- strtoul("-0") never wrapped, so "-0" and "-00" always meant what was
  typed. Following #212 (keep working command lines working),
  parse_unsigned() now accepts one '-' when the value is zero and still
  rejects every real negative ("-1", "-0x", "+-0", negative overflow).
  This applies to every option on the helper, including #721's (which
  had rejected "-0" on unreleased master only). 0 keeps its meaning
  per option: accepted where valid, "must be greater than zero" where
  not; port options still reject 0. The only breaking change left is
  empty values.
- config_print_to_json() writes scan_incremental_max_iterations only
  with --scan-incremental-iteration, like the staircase keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/725-remaining-numeric-options branch from 9d17d4c to 316559b Compare October 16, 2026 23:43
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Point by point on the 23:04 review. Everything is in 316559b, rebased on 38dfbaa.

  1. -0: agreed, it is now accepted. strtoul("-0") never wrapped, so by the Fix issue #211. Allow key_stddev and key_median options to be set values #212 rule those command lines should keep working.

  2. Scope. This lands as a single squash commit. Each runtime fix has its own regression test and its own section in the PR description:

    • the rate-limit/jitter SIGFPEs;
    • the full 64-bit key range in random_range()/Gaussian/Zipf;
    • the keylist growth hang.

    I'm happy to split the obj_gen.cpp/protocol.cpp part into its own PR if a maintainer prefers that for bisecting or backporting.

  3. scan_incremental_max_iterations in JSON. Now written only when --scan-incremental-iteration is on, matching the staircase keys.

  4. static_assert. It's deliberate. The type declared right above it is what it protects: reverting m_buffer_size to unsigned int passed every runtime test (unit and RLTest), and only a real multi-GB run caught it. A comment can't fail the build; the static_assert does.

🤖 Generated with Claude Code

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Point by point on the 23:44 review. This round only changes the PR description; there is no code push.

  1. Description vs. code. Fixed.
    • The top summary now says that any negative value is rejected and that -0/-00 are accepted as 0.
    • The Breaking bullets cover only the empty value: --max-retries=, and the options where 0 is valid. The stale "-0 is now rejected" sentence is gone.
    • The --max-retries bullet now explains how parse_signed() differs from parse_unsigned().
    • The cap is 16777214 everywhere.
    • The PR lands as one squash commit with a fresh message, so the intermediate commits that mention INT_MAX / 1000000 for --multi-key-get won't be in the history.
  2. Empty --max-retries=. It stays rejected, on purpose. An unset $VAR used to turn silently into "retries disabled", the opposite of the default (-1, unlimited); now the run fails loudly at startup. As you say, an empty string that means 0 can't be told apart from a broken script. I applied the same rule to the options where 0 is valid instead of keeping empty as 0 there, for consistency with Reject wrapped, overflowing and empty values on the 13 options in #705/#695 #721 (--max-reconnect-attempts= used to mean unlimited). The README states this.
  3. Scope. Acknowledged. The obj_gen.cpp / protocol.cpp fixes deserve a second look from whoever owns key generation and MGET building. Each one has its own regression test and its own section in the description.
  4. Thanks for checking the Gaussian clamp and the span == 0 branch.
  5. rand() / RAND_MAX jitter ceiling. Right. With a jitter range above about 2^31, the sleep never reaches the top of the range. That was already the case before this PR; the 64-bit modulo only removes the SIGFPE. I've listed it under "Follow-ups" in the description rather than changing behaviour here.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 215fbac into master Oct 17, 2026
147 of 148 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 17, 2026
parse_uri() read the database number of redis://host:port/N with
(int) strtol() and only checked db < 0, so the cast wrapped values
outside int: /4294967296 selected db 0, /4294967297 db 1, /6442450943
db 2147483647, and the saturated -99999999999999999999999 became db 0.
That is the #725 class #748 fixed for --select-db.

The number now goes through parse_unsigned() with an INT_MAX bound, the
same rule as --select-db: leading whitespace, one '+' and "-0" are
accepted; any other negative, overflow, junk and values above INT_MAX
are rejected with the existing "invalid database number '...'" error.
An empty path ("redis://host:port/") still selects no database, and
#735's userinfo split (including the known '@'-in-path limits pinned by
test_config_password_redaction.py) is unchanged.

Refs #725

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remaining numeric options accept negative, overflowing and wrapping values (unchecked strtoul/strtoull/strtol)

1 participant