Skip to content

fix(core): make numeric formatting and parsing culture-invariant - #663

Merged
currantw merged 5 commits into
valkey-io:mainfrom
currantw:651-numeric-invariant-culture
Nov 20, 2026
Merged

currantw merged 5 commits into
valkey-io:mainfrom
currantw:651-numeric-invariant-culture

Conversation

@currantw

@currantw currantw commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Make the C# client culture-invariant. The server expects numbers in a fixed, culture-independent format (e.g. -1, 13.361389), but the client formatted arguments and parsed replies using CultureInfo.CurrentCulture. As a result, applications running under some non-English cultures could send arguments that the server rejects. After this change, the client formats every argument and parses every reply with the invariant culture, and analyzers prevent regressions.

Issue Link

Closes #651.

Features and Behaviour Changes

  • Culture-invariant arguments: numbers are always sent in invariant form:

    • Under sv-SE, fa-IR, and similar cultures, negative integers were sent with a Unicode minus sign (U+2212) and rejected by the server (e.g. INCRBY key -5).
    • Under tr-TR, BITFIELD ... OVERFLOW FAIL was sent as OVERFLOW FAİL and rejected by the server. The subcommand argument was built with OverflowType.Fail.ToString().ToUpper(), and in Turkish, uppercase i is the dotted capital İ (U+0130), not ASCII I. It now uses ToUpperInvariant(). WRAP and SAT contain no i, so they were not affected.
  • Culture-invariant reply parsing: replies are parsed with the invariant culture.

  • Unchanged display output: GeoPosition.ToString() and SortedSetEntry.ToString() still use the current culture, matching StackExchange.Redis.

  • Regression prevention: task lint now fails on culture-sensitive formatting, parsing, and casing:

    • MA0011: IFormatProvider is missing.
    • MA0075: do not use implicit culture-sensitive ToString.
    • MA0076: do not use implicit culture-sensitive ToString in interpolated strings.

    These rules come from Meziantou.Analyzer, which is added as a new development-only analyzer dependency.

Implementation

  • Culture-invariant fixes:
    • Call sites: pass CultureInfo.InvariantCulture at every place that formats or parses a number.
    • Where an argument is already a GlideString, use the invariant ToGlideString() extension instead.
    • Reply helpers: parse replies with the existing ToLong/ToDouble/TryToLong/TryToDouble helpers rather than x.ToString().
  • Analyzer configuration (.editorconfig, all C# files):
    • Meziantou is used instead of the built-in CA1305 because MA0011 covers everything CA1305 does that matters here, plus TryParse without a format provider and culture-sensitive casing (ToUpper/ToLower). CA1305 missed both the MEMORY STATS TryParse and the BITFIELD ToUpper bugs. MA0075 and MA0076 also catch culture-sensitive string concatenation and interpolation, which CA1305 doesn't check.
    • Meziantou is added with MeziantouAnalysisMode=None, so only rules opted into in .editorconfig run. This mirrors the Roslynator and StyleCop setup. The remaining rules are listed under a TODO for task(lint): enable Meziantou analyzers #662.
    • MA0076 is disabled for tests, where interpolation is mostly non-negative integers in test keys and messages.
  • Tests run under the invariant culture: xunit.runner.json sets "culture": "invariant", so results don't depend on the developer's machine culture.
  • Minor refactors in touched code:
    • Added a Request.ToString(RequestType, args) helper for commands with string replies.
    • LolwutOptions.ToArgs() now returns GlideString[].
    • Removed #pragma warning disable from ConfigurationOptions.cs and fixed the violations:
      • Added braces to single-line if statements,
      • Renamed the internal fields _trustedIssuers, _clientCertificate, and _clientKey.

Limitations

None

Testing

  • Added GlideStringTests.ToGlideString_NonAsciiNegativeSign_UsesInvariantCulture, which runs under sv-SE.
  • Fixed 10 culture-sensitive call sites in test code and test utilities.

Related Issues

Checklist

  • This Pull Request is related to one issue.
  • Commit message has a detailed description of what changed and why.
  • Tests are added or updated and all checks pass.
  • CHANGELOG.md, README.md, DEVELOPER.md, and other documentation files are updated. No documentation changes needed; CHANGELOG.md is updated at release time.
  • Destination branch is correct - main or release
  • Create merge commit if merging release branch into main, squash otherwise.

The server expects numbers in a fixed, culture-independent format, but
the client formatted arguments and parsed replies using the current
culture. Under cultures such as sv-SE (U+2212 minus sign) or tr-TR
(dotted capital I), the client could send arguments that the server
rejects.

- Format arguments and parse replies with the invariant culture.
- Enable Meziantou MA0011/MA0075/MA0076 as errors to prevent
  regressions; track the remaining Meziantou rules in valkey-io#662.
- Run tests under the invariant culture via xunit.runner.json.
- Keep GeoPosition and SortedSetEntry ToString() culture-sensitive
  for StackExchange.Redis compatibility.
- Add a ToString(RequestType, args) helper for string replies.

Closes valkey-io#651

Signed-off-by: currantw <taylor.curran@improving.com>
@currantw currantw self-assigned this Nov 19, 2026
@currantw currantw added the core Core library (`sources/Valkey.Glide/`) label Nov 19, 2026
Signed-off-by: currantw <taylor.curran@improving.com>
Signed-off-by: currantw <taylor.curran@improving.com>

@leslie-bq leslie-bq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, non-blocking, LGTM.

Signed-off-by: currantw <taylor.curran@improving.com>
@currantw
currantw merged commit 50fc87a into valkey-io:main Nov 20, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core library (`sources/Valkey.Glide/`)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): numeric command arguments and reply parsing use the current culture

3 participants