Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,11 @@ void Value::CZString::swap(CZString& other) {
}

Value::CZString& Value::CZString::operator=(const CZString& other) {
cstr_ = other.cstr_;
index_ = other.index_;
// Copy-and-swap so a duplicate-policy buffer is deep-copied and the old one
// released once. The prior shallow copy aliased other.cstr_, double-freeing
// an owned string and leaking the overwritten one.
CZString temp(other);
swap(temp);

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 Inactive union member access

When a string-backed CZString is copy-assigned, the new call to swap(temp) unconditionally swaps index_ even though storage_ is the active union member, causing undefined behavior while transferring length and ownership metadata and enabling invalid comparisons, leaks, or invalid frees.

return *this;
}

Expand Down
13 changes: 13 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ void ValueTest::runCZStringTests() {
// This verifies we don't leak "other" (if fixed) and correctly take "param"
str5 = std::move(str3);
JSONTEST_ASSERT_STRING_EQUAL(str5.data(), "param");

// 7. Copy Assignment (String)
// Copy-assign one owning string over another. The source must remain valid
// (deep copy) and the destination's old buffer must be released once, with no
// double free of the shared buffer at scope exit.
Json::Value::CZString str6("alpha", 5,
Json::Value::CZString::duplicateOnCopy);
Json::Value::CZString str7((str6)); // owning "alpha"
Json::Value::CZString str8("beta", 4, Json::Value::CZString::duplicateOnCopy);
Json::Value::CZString str9((str8)); // owning "beta"
str9 = str7;
JSONTEST_ASSERT_STRING_EQUAL(str9.data(), "alpha");
JSONTEST_ASSERT_STRING_EQUAL(str7.data(), "alpha");
}

JSONTEST_FIXTURE_LOCAL(ValueTest, CZStringCoverage) { runCZStringTests(); }
Expand Down
Loading