@@ -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
213226JSONTEST_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 (" \0 ab" , 3 ), Json::String (" a\0 b" , 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\0 b" , 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+
21762230JSONTEST_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.
43524406JSONTEST_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
43604431struct ParseWithStructuredErrorsTest : JsonTest::TestCase {
0 commit comments