Repository navigation
Conversation
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.
[Medium impact] The change affects shared Json::Value string and object-key operations at extreme length limits. Fix Findings
|
| const size_t keyLength = static_cast<size_t>(end - begin); | ||
| if (!isValidKeyLength(keyLength)) | ||
| return false; |
There was a problem hiding this comment.
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.
| 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))), |
There was a problem hiding this comment.
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.
A string of 4 GiB or more is stored truncated with no error (#1329):
Valuecasts the length tounsignedbefore the "length too big for prefixing" check runs, so the check only sees the low 32 bits. Strings of 2-4 GiB already throwLogicError; 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.
OurReaderrejects those while parsing, butdemand(),operator[],find()andremoveMember()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.