Skip to content

fix double-free in CZString copy-assignment operator - #1706

Open
SABITHSAHEB wants to merge 3 commits into
open-source-parsers:masterfrom
SABITHSAHEB:czstring-copy-assign-double-free
Open

fix double-free in CZString copy-assignment operator#1706
SABITHSAHEB wants to merge 3 commits into
open-source-parsers:masterfrom
SABITHSAHEB:czstring-copy-assign-double-free

Conversation

@SABITHSAHEB

Copy link
Copy Markdown
Contributor

CZString is the key type behind Json::Value's object and array maps, and a key that owns its buffer (the duplicate policy) frees that buffer in its destructor. The copy-assignment operator only shallow-copied cstr_ and the packed index_/policy word, so after a = b both keys pointed at b's allocation while a's old buffer was never released. On scope exit both destructors free the same pointer, which AddressSanitizer reports as a double free, and the overwritten buffer leaks. I ran into it while filling out the CZString assignment coverage from #1654, which exercised the copy and move constructors and the move-assignment but not this operator. The move-assignment already releases then transfers correctly, so I made copy-assignment do the safe thing with copy-and-swap: the temporary deep-copies the source and takes our old buffer to be freed exactly once. Added the missing case to runCZStringTests, which double-frees under ASan before the change and passes after.

@SABITHSAHEB

Copy link
Copy Markdown
Contributor Author

any update?

@greptile-apps

greptile-apps Bot commented Aug 20, 2026

Copy link
Copy Markdown

Greptile Summary

The PR replaces shallow CZString copy assignment with copy-and-swap to deep-copy owned key buffers and adds a focused ownership regression test.

  • Constructs a copied temporary before replacing the destination key.
  • Exercises copy assignment between two owning string-backed keys.

Confidence Score: 4/5

The PR is not yet safe to merge because string-backed copy assignment still reaches undefined behavior in CZString::swap.

The new assignment path calls a swap implementation that unconditionally accesses index_ even when storage_ is the active union member, so the previously reported defect remains outstanding.

Files Needing Attention: src/lib_json/json_value.cpp

Important Files Changed

Filename Overview
src/lib_json/json_value.cpp Reworks CZString copy assignment to use copy-and-swap, but the previously reported inactive-union-member access remains in the invoked swap implementation.
src/test_lib_json/main.cpp Adds focused regression coverage confirming that both source and destination retain the copied string after assignment.

Reviews (2): Last reviewed commit: "Merge branch 'master' into czstring-copy..." | Re-trigger Greptile

Comment thread src/lib_json/json_value.cpp
@coveralls

coveralls commented Aug 20, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 32808338607

Coverage increased (+0.1%) to 90.022%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 2 of 2 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2741
Covered Lines: 2622
Line Coverage: 95.66%
Relevant Branches: 2641
Covered Branches: 2223
Branch Coverage: 84.17%
Branches in Coverage %: Yes
Coverage Strength: 23819.35 hits per line

💛 - Coveralls

@Alhammadi3

Alhammadi3 commented Aug 26, 2026 via email

Copy link
Copy Markdown

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.

4 participants