Skip to content

Reject wrapped and overflowing --uri database numbers - #761

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/725-uri-db-number
Oct 17, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/725-uri-db-number

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #748, which left the --uri database number out of scope.

Problem

parse_uri() reads the database number in redis://host:port/N with (int) strtol() and checks only *endptr and db < 0. Values outside int therefore wrap through the cast. On master 7e107ff, --show-config shows:

--uri path master this PR
/4294967296 select-db = 0 invalid database number '4294967296'
/4294967297 select-db = 1 rejected
/6442450943 select-db = 2147483647 rejected
/-99999999999999999999999, /-18446744073709551615, /-18446744071562067969 select-db = 0 (LONG_MIN cast to int) rejected
/-4294967295, /-4294967296 select-db = 1, 0 rejected
/2147483648, /-1, /abc, /1x rejected rejected (unchanged)

Root cause

The value is narrowed to int before any range check: the same #725 pattern that #748 fixed for --select-db.

Fix

The number now goes through parse_unsigned(db_start, INT_MAX, &db), the same rule as --select-db:

What is unchanged:

  • An empty path (redis://host:port/) still selects no database, so a --select-db value is kept.
  • The userinfo split from Redact the password in --json-out-file and --show-config #735 (uri_userinfo_end()) is untouched, including the known '@'-in-path limits pinned by test_config_password_redaction.py. In particular, a URI with credentials ending in .../1@ is still reported as invalid database number '1@'. Without credentials, the '@' still ends a "userinfo", as pinned by the known-limit test.

Tests

New test_uri_database_number in tests/test_cli_validation_count_flags.py. It runs --uri=redis://127.0.0.1:1/<N> --show-config; parsing happens before the connect to closed port 1.

  • Accepted: 0, 7, +3, " 5", -0, -00 and 2147483647, each checked as select-db = N. With --select-db=3, an empty path keeps select-db = 3.
  • Rejected with the message: -1, -2147483648, 2147483648, 4294967296, 4294967297, 6442450943, overflow (both signs), -4294967295 and -4294967296 (which master wraps to db 1 and 0), -18446744073709551615 and -18446744071562067969 (which a strtoull() parse without the sign check would negate to db 1 and 2147483647), abc, 1x, "1 ", +-1, - 1, ++1, " " and 0x10.

RLTest --env oss:

suite origin/master 0ea5c6f this PR (b5702de)
test_uri_database_number fails (8 values accepted: 4294967296, 4294967297, 6442450943, -4294967295, -4294967296, -18446744073709551615, -18446744071562067969, -99999999999999999999999) passes, 3 of 3 runs
test_cli_validation_count_flags.py — 29/29
test_config_password_redaction.py — 20/20
test_cli_validation_ports.py — 8/8
the 9 test_uri_* tests in tests_oss_simple_flow.py — 9/9 pass

Release note: --uri database numbers above 2147483647, or negative values other than -0, are now rejected instead of wrapping to another database.

Refs #725

Affected versions

2.2.0 through 2.5.2, and master. The (int) strtol() parse was added together with --uri in 68fb953 (#311), whose first tag is 2.2.0, and the code is unchanged since. This was reproduced on master 7e107ff. The released versions come from history (git tag --contains) and were not run.

🤖 Generated with Claude Code


Note

Low Risk
CLI-only URI parsing change with stricter validation; aligns with existing --select-db behavior and adds tests.

Overview
URI database parsing now matches --select-db validation instead of casting with (int) strtol(), which could silently wrap large values (e.g. 4294967297 → db 1).

parse_uri() uses parse_unsigned(..., INT_MAX, ...) so leading whitespace, +, and -0/-00 still work, while negatives (other than -0), overflow, trailing junk, and values above INT_MAX fail with error: invalid database number '...'. Empty path after / behavior is unchanged.

Adds test_uri_database_number covering accepted paths and the rejection cases above via --show-config without needing a live Redis.

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

fcostaoliveira and others added 2 commits October 17, 2026 03:19
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]>
… the comment

Review of #761:
- Reject list adds -4294967295 and -4294967296, which (int) strtol()
  wrapped into db 1 and 0, and -18446744073709551615 and
  -18446744071562067969, which strtoull() without the sign check would
  negate into db 1 and 2147483647.
- The empty-path case now runs with --select-db=3 and checks that
  "redis://127.0.0.1:1/" keeps it, so resetting it to 0 is caught.
- The comment states the rule instead of the history.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit afbb514 into master Oct 17, 2026
129 checks passed
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.

1 participant