Skip to content

fix: reject string values and object keys too long to store - #1733

Open
b84330808 wants to merge 1 commit into
open-source-parsers:masterfrom
b84330808:fix-long-string-key-truncation
Open

b84330808 wants to merge 1 commit into
open-source-parsers:masterfrom
b84330808:fix-long-string-key-truncation

Conversation

@b84330808

Copy link
Copy Markdown

A string of 4 GiB or more is stored truncated with no error (#1329): Value casts the length to unsigned before the "length too big for prefixing" check runs, so the check only sees the low 32 bits. Strings of 2-4 GiB already throw LogicError; this makes longer ones do the same.

Object keys have the same problem in the 30-bit length field. A key of 1 GiB or more is stored as its first (length mod 2^30) bytes and can alias a short key. OurReader rejects those while parsing, but demand(), operator[], find() and removeMember() don't. Creating such a key now throws; looking one up returns not found.

Long strings still aren't supported (that would change the stored layout), but they no longer lose data silently. The new test uses an untouched allocation, since lengths are checked before anything is read.

Value casts string lengths to unsigned before the existing "length too
big for prefixing" check, so a string of 4 GiB or more wraps and is
stored as its first (length mod 2^32) bytes with no error (open-source-parsers#1329).
Take the length as size_t so the check sees the real value.

Object keys have the same problem in a 30-bit field: a key of 1 GiB or
more is stored as its first (length mod 2^30) bytes and can alias a
short key. OurReader already rejects those while parsing, but demand(),
operator[], find() and removeMember() don't. Throw when creating such a
key, and treat it as absent when looking one up.
@greptile-apps

greptile-apps Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects shared Json::Value string and object-key operations at extreme length limits.

Fix removeMember(const char*) before merging; oversized keys can still delete unrelated members.

Findings

  1. P1 Wrong member gets deleted ▶
  2. P2 Key tests silently skip ▶

Summary

The PR checks full string and object-key lengths before narrowing them.

  • String values longer than the prefix limit now fail instead of being shortened.
  • Object keys too long for storage now fail on insert and miss on lookup or removal.

Reviews (1) · Last reviewed commit: "fix: reject string values and object key..." · Reviewed by Greptile

Comment on lines +1298 to +1300
const size_t keyLength = static_cast<size_t>(end - begin);
if (!isValidKeyLength(keyLength))
return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Wrong member gets deleted

The new check misses removeMember(const char*). That overload still builds a CZString directly from strlen(key). For a null-terminated key of length 2^30 + 1 starting with "x", CZString stores length 1, so root.removeMember(key) can delete root["x"].

Route that overload through the checked overload while keeping its existing type check.

Comment on lines +2195 to +2198
if (stringLength > std::numeric_limits<size_t>::max())
return;
std::unique_ptr<char, decltype(&std::free)> buffer(
static_cast<char*>(std::malloc(static_cast<size_t>(stringLength))),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Key tests silently skip

The early return for stringLength skips the key checks too. On a 32-bit build, the 4 GiB string cannot fit, but the 1 GiB key length can. If the larger allocation fails, the key checks also skip even when a smaller allocation could succeed.

Split the string and key checks so each can run independently.

This branch has not been deployed

No deployments
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