Skip to content

Commit 252a17a

Browse files
authored
Merge branch 'open-source-parsers:master' into master
2 parents b86c108 + 93acc7b commit 252a17a

4 files changed

Lines changed: 113 additions & 19 deletions

File tree

‎src/lib_json/json_reader.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ class OurReader {
988988
// CharReaderTest/parseCommentsAfterValueScansLinearly). thread_local so it
989989
// never races during concurrent parsing; the increment is negligible and only
990990
// runs while parsing comments. Not part of the supported public API.
991+
JSON_API size_t& newlineScanByteCountForTesting();
991992
JSON_API size_t& newlineScanByteCountForTesting() {
992993
static thread_local size_t count = 0;
993994
return count;

‎src/lib_json/json_value.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ void Value::CZString::swap(CZString& other) {
320320
}
321321

322322
Value::CZString& Value::CZString::operator=(const CZString& other) {
323-
cstr_ = other.cstr_;
324-
index_ = other.index_;
323+
// Copy-and-swap so a duplicate-policy buffer is deep-copied and the old one
324+
// released once. The prior shallow copy aliased other.cstr_, double-freeing
325+
// an owned string and leaking the overwritten one.
326+
CZString temp(other);
327+
swap(temp);
325328
return *this;
326329
}
327330

@@ -1307,7 +1310,13 @@ void Value::removeMember(const char* key) {
13071310
CZString actualKey(key, unsigned(strlen(key)), CZString::noDuplication);
13081311
value_.map_->erase(actualKey);
13091312
}
1310-
void Value::removeMember(const String& key) { removeMember(key.c_str()); }
1313+
void Value::removeMember(const String& key) {
1314+
JSON_ASSERT_MESSAGE(
1315+
type() == nullValue || type() == objectValue,
1316+
"in Json::Value::removeMember(): requires objectValue, but found "
1317+
<< valueTypeToString(type()));
1318+
removeMember(key, nullptr);
1319+
}
13111320

13121321
bool Value::removeIndex(ArrayIndex index, Value* removed) {
13131322
if (type() != arrayValue) {

‎src/test_lib_json/fuzz.cpp‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "fuzz.h"
77

88
#include <cstdint>
9+
#include <cstdlib>
910
#include <json/config.h>
1011
#include <json/json.h>
1112
#include <memory>
@@ -25,17 +26,29 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
2526
data += sizeof(uint32_t);
2627
size -= sizeof(uint32_t);
2728

28-
builder.settings_["failIfExtra"] = hash_settings & (1 << 0);
29-
builder.settings_["allowComments_"] = hash_settings & (1 << 1);
30-
builder.settings_["strictRoot_"] = hash_settings & (1 << 2);
31-
builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3);
32-
builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4);
33-
builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5);
34-
builder.settings_["failIfExtra_"] = hash_settings & (1 << 6);
35-
builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7);
36-
builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8);
37-
builder.settings_["collectComments"] = hash_settings & (1 << 9);
38-
builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10);
29+
builder.settings_["failIfExtra"] = (hash_settings & (1 << 0)) != 0;
30+
builder.settings_["allowComments"] = (hash_settings & (1 << 1)) != 0;
31+
builder.settings_["strictRoot"] = (hash_settings & (1 << 2)) != 0;
32+
builder.settings_["allowDroppedNullPlaceholders"] =
33+
(hash_settings & (1 << 3)) != 0;
34+
builder.settings_["allowNumericKeys"] = (hash_settings & (1 << 4)) != 0;
35+
builder.settings_["allowSingleQuotes"] = (hash_settings & (1 << 5)) != 0;
36+
builder.settings_["skipBom"] = (hash_settings & (1 << 6)) != 0;
37+
builder.settings_["rejectDupKeys"] = (hash_settings & (1 << 7)) != 0;
38+
builder.settings_["allowSpecialFloats"] = (hash_settings & (1 << 8)) != 0;
39+
builder.settings_["collectComments"] = (hash_settings & (1 << 9)) != 0;
40+
builder.settings_["allowTrailingCommas"] = (hash_settings & (1 << 10)) != 0;
41+
// Bit 11 selects a small stack limit taken from the upper 16 bits, so that
42+
// the depth-limit error paths are reachable without huge inputs.
43+
if (hash_settings & (1 << 11)) {
44+
builder.settings_["stackLimit"] = (hash_settings >> 16) & 0x3FF;
45+
}
46+
47+
// An unknown key is silently ignored by newCharReader(), so a typo here
48+
// would quietly stop the fuzzer from exercising that setting.
49+
if (!builder.validate(nullptr)) {
50+
std::abort();
51+
}
3952

4053
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
4154

‎src/test_lib_json/main.cpp‎

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ void ValueTest::runCZStringTests() {
208208
// This verifies we don't leak "other" (if fixed) and correctly take "param"
209209
str5 = std::move(str3);
210210
JSONTEST_ASSERT_STRING_EQUAL(str5.data(), "param");
211+
212+
// 7. Copy Assignment (String)
213+
// Copy-assign one owning string over another. The source must remain valid
214+
// (deep copy) and the destination's old buffer must be released once, with no
215+
// double free of the shared buffer at scope exit.
216+
Json::Value::CZString str6("alpha", 5,
217+
Json::Value::CZString::duplicateOnCopy);
218+
Json::Value::CZString str7((str6)); // owning "alpha"
219+
Json::Value::CZString str8("beta", 4, Json::Value::CZString::duplicateOnCopy);
220+
Json::Value::CZString str9((str8)); // owning "beta"
221+
str9 = str7;
222+
JSONTEST_ASSERT_STRING_EQUAL(str9.data(), "alpha");
223+
JSONTEST_ASSERT_STRING_EQUAL(str7.data(), "alpha");
211224
}
212225

213226
JSONTEST_FIXTURE_LOCAL(ValueTest, CZStringCoverage) { runCZStringTests(); }
@@ -1933,6 +1946,9 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) {
19331946
JSONTEST_ASSERT_THROWS(intVal.removeMember("test"));
19341947
JSONTEST_ASSERT_THROWS(strVal.removeMember("test"));
19351948
JSONTEST_ASSERT_THROWS(arrVal.removeMember("test"));
1949+
JSONTEST_ASSERT_THROWS(intVal.removeMember(Json::String("test")));
1950+
JSONTEST_ASSERT_THROWS(strVal.removeMember(Json::String("test")));
1951+
JSONTEST_ASSERT_THROWS(arrVal.removeMember(Json::String("test")));
19361952

19371953
JSONTEST_ASSERT_THROWS(intVal.getMemberNames());
19381954
JSONTEST_ASSERT_THROWS(strVal.getMemberNames());
@@ -2173,6 +2189,44 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, zeroesInKeys) {
21732189
}
21742190
}
21752191

2192+
JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberWithEmbeddedNull) {
2193+
const Json::String keys[] = {Json::String("\0ab", 3), Json::String("a\0b", 3),
2194+
Json::String("ab\0", 3)};
2195+
for (const auto& key : keys) {
2196+
const Json::String prefix(key.c_str());
2197+
Json::Value root;
2198+
root[prefix] = "keep";
2199+
root[key] = "remove";
2200+
2201+
root.removeMember(key);
2202+
JSONTEST_ASSERT(!root.isMember(key));
2203+
JSONTEST_ASSERT(root.isMember(prefix));
2204+
JSONTEST_ASSERT_STRING_EQUAL("keep", root.get(prefix, "").asString());
2205+
JSONTEST_ASSERT_EQUAL(1U, root.size());
2206+
2207+
root.removeMember(key);
2208+
JSONTEST_ASSERT(root.isMember(prefix));
2209+
JSONTEST_ASSERT_EQUAL(1U, root.size());
2210+
}
2211+
}
2212+
2213+
JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberStringOnNullAndEmptyKey) {
2214+
Json::Value root;
2215+
root.removeMember("missing");
2216+
JSONTEST_ASSERT(root.isNull());
2217+
root.removeMember(Json::String("a\0b", 3));
2218+
JSONTEST_ASSERT(root.isNull());
2219+
2220+
root[""] = "remove";
2221+
root.removeMember(Json::String());
2222+
JSONTEST_ASSERT(root.isObject());
2223+
JSONTEST_ASSERT(root.empty());
2224+
2225+
root["ordinary"] = "remove";
2226+
root.removeMember("ordinary");
2227+
JSONTEST_ASSERT(root.empty());
2228+
}
2229+
21762230
JSONTEST_FIXTURE_LOCAL(ValueTest, specialFloats) {
21772231
Json::StreamWriterBuilder b;
21782232
b.settings_["useSpecialFloats"] = true;
@@ -4350,11 +4404,28 @@ struct FuzzTest : JsonTest::TestCase {};
43504404
// Build and run the fuzz test without any fuzzer, so that it's guaranteed not
43514405
// go out of date, even if it's never run as an actual fuzz test.
43524406
JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
4353-
const std::string example = "{}";
4354-
JSONTEST_ASSERT_EQUAL(
4355-
0,
4356-
LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(example.c_str()),
4357-
example.size()));
4407+
// The first four bytes select the reader settings (little-endian).
4408+
const auto withSettings = [](uint32_t settings, const std::string& doc) {
4409+
std::string input;
4410+
for (int i = 0; i < 4; ++i) {
4411+
input += static_cast<char>((settings >> (8 * i)) & 0xFF);
4412+
}
4413+
return input + doc;
4414+
};
4415+
const std::string examples[] = {
4416+
"{}",
4417+
withSettings(0, "{\"a\":[1,2]}"),
4418+
// Every boolean setting on, with a stack limit of 16.
4419+
withSettings(0x0010FFFF, "{\"a\":[1,2,],} // comment"),
4420+
// A stack limit of 1, so the nested arrays exceed it.
4421+
withSettings(0x00010800, "[[[[1]]]]"),
4422+
};
4423+
for (const auto& example : examples) {
4424+
JSONTEST_ASSERT_EQUAL(
4425+
0,
4426+
LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(example.data()),
4427+
example.size()));
4428+
}
43584429
}
43594430

43604431
struct ParseWithStructuredErrorsTest : JsonTest::TestCase {

0 commit comments

Comments
 (0)