Skip to content

Escape string values written to the JSON output - #555

Merged
fcostaoliveira merged 1 commit into
redis:masterfrom
darth-modder:feat/json-escape-strings
Oct 12, 2026
Merged

fcostaoliveira merged 1 commit into
redis:masterfrom
darth-modder:feat/json-escape-strings

Conversation

@darth-modder

@darth-modder darth-modder commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Why

json_handler::write_obj writes "%s" values straight through vfprintf, with no escaping. A configuration value containing ", \ or a control character therefore makes mb.json invalid. For example, on Linux:

--key-prefix='we"ird\pre'   →   ,"key_prefix": "we"ird\pre"   →   json.load: Expecting ',' delimiter

Any Windows path (C:\Users\...) hits the same thing, which is how I found it while porting memtier to Windows (#553). As suggested in the review of #553, I've split it out into this small, platform-neutral PR.

What

  • write_obj escapes the value when the format is exactly "\"%s\"": " → \", \ → \\, and other control characters → \u00XX. That is the same encoding the Prometheus run-label helper already used. Every string call site uses that exact format today, and a comment at the gate notes that any other quoted format is not escaped.
  • The Prometheus run-label helper no longer pre-escapes its value, since write_obj now does it. Otherwise labels would be escaped twice. test_v18_mb_json_round_trip covers this.
  • A new test, test_json_configuration_escapes_string_values, runs with a --key-prefix containing a quote, a backslash and control characters, and checks that mb.json parses and the value round-trips.

Output only changes for values that previously produced invalid JSON. Everything else is byte-identical.

Testing

  • Linux (Ubuntu 24.04 container):
    • test_json_output_integrity.py and test_cli_validation_prometheus.py pass (28/28).
    • Run against master's binary, the new test fails with JSONDecodeError: Expecting ',' delimiter, so it discriminates.
  • Side by side with master on the same fixed workloads, only configuration.key_prefix differs, and only for values containing those characters. Before, master's file failed json.load; with this change it parses and the value round-trips.
  • No new compiler warnings, and clang-format is clean.

Note

Low Risk
Output-format fix limited to JSON serialization; behavior changes only for values that previously produced unparsable JSON.

Overview
Fixes invalid mb.json when configuration strings contain quotes, backslashes, or control characters by escaping those values at write time instead of emitting them verbatim.

json_handler::write_obj now uses a write_json_string_body helper when the format is exactly "\"%s\"", producing standard JSON escapes (\", \\, \u00XX). Prometheus run labels no longer pre-escape in prometheus_run_labels_str, since centralizing escaping avoids double-encoding. A new integrity test runs with a hostile --key-prefix and asserts the configuration section parses and round-trips.

Output is unchanged for values that were already valid JSON; only previously broken documents differ.

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

json_handler::write_obj printed quoted string values with a raw vfprintf, so
a string containing '"', '\' or a control character produced a document that
json.load() rejects. Examples are a path with backslashes (such as a Windows
--out-file or --json-out-file) and, on any platform,
--key-prefix='we"ird\pre'.

write_obj now escapes such values as \", \ and \u00XX (the encoding the
Prometheus run-label helper already used) when the format is exactly a
quoted %s. That helper no longer pre-escapes, otherwise its value would be
escaped twice. Output for strings without those characters is unchanged, and
all other formats still go through vfprintf as before.

Adds a regression test that round-trips a quote/backslash key prefix and the
out_file path through json.load().
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

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

Thanks for splitting this out of #553, it's much easier to review on its own. I think the fix is right, and it's in the right place. Escaping once in write_obj and dropping the pre-escaping in the Prometheus helper is a smaller and cleaner result than adding another per-call-site helper. I checked the current call sites, and every %s use of write_obj (in memtier_benchmark.cpp and run_stats.cpp) passes exactly "\"%s\"", so gating on that format covers everything today. Output only changes for values that used to produce invalid JSON, so I don't see a compatibility concern here. Also good to see the new test shown failing on master's binary, which matches what was done for #364.

A few small comments:

  • The PR description says the new test uses "a quote, a backslash and control characters", but the prefix in test_json_configuration_escapes_string_values is 'q"b\\s:', which has no control characters. Was that intentional? The \u00XX branch in write_json_string_body looks untested by this test, unless test_v18_mb_json_round_trip already sends a control character through the run labels. If it's easy to pass one through --key-prefix (a tab, say), that would cover the whole helper.
  • I wonder if the exact-format gate will catch someone out later. Someone adding "\"%s:%d\"" would silently get unescaped output, and the comment you added is the only warning. Did you consider a dedicated write_string_obj(name, value) instead? Not a blocker, since the current callers are all covered.
  • Nitpicking: the commit message says \", \ and \u00XX. I guess the second one should be \\.

The core change looks solid to me. I'd just like the control-character case actually exercised by a test before this merges.

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

@darth-modder
darth-modder force-pushed the feat/json-escape-strings branch from f4b95c1 to 48f7817 Compare October 9, 2026 22:56

@fcostaoliveira fcostaoliveira 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.

Thanks — clean, well-tested fix. CI green.

@fcostaoliveira
fcostaoliveira merged commit 3d710d5 into redis:master Oct 12, 2026
119 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.

2 participants