Skip to content

Fix #1199: correct ownership transfer in json::detail::r_string move operations (prevent leaks and invalid frees)#1200

Open
jwolak wants to merge 1 commit into
CrowCpp:masterfrom
jwolak:fix/issue-1199-rstring-ownership
Open

Fix #1199: correct ownership transfer in json::detail::r_string move operations (prevent leaks and invalid frees)#1200
jwolak wants to merge 1 commit into
CrowCpp:masterfrom
jwolak:fix/issue-1199-rstring-ownership

Conversation

@jwolak

@jwolak jwolak commented Jul 17, 2026

Copy link
Copy Markdown

This PR fixes a memory-management issue in json::detail::r_string caused by incorrect move semantics.

Problem
r_string move operations did not safely transfer ownership in all cases, which could lead to:

  • memory leaks during move assignment when the destination already owned a buffer,
  • incorrect behavior on self-move assignment,
  • potential invalid memory handling when move construction fell back to copy-like behavior.

Root Cause

  • The move constructor delegated without std::move, effectively using copy-assignment behavior.
  • Move assignment did not release the destination’s previously owned buffer.
  • No self-assignment/self-move guard was present.

Changes

In json.h:

  1. Updated move constructor to use std::move(r) so move assignment is actually invoked.
  2. Updated move assignment to:
    • return early on this == &r,
    • release currently owned destination memory before taking ownership from source,
    • transfer pointers/ownership flag and clear source ownership afterward.
  3. Added a this == &r guard to copy assignment for consistency/safety.

In test_json.cpp:

  1. Added a helper that creates an owned r_string from a copied buffer.
  2. Added regression tests for When a crow::json::rvalue is reused and repeatedly assigned from crow::json::load(...), heap usage grows roughly linearly with the number of assignments. #1199 covering:
    • move constructor ownership transfer,
    • move assignment ownership transfer,
    • self-move assignment as a no-op.

Result
- Ownership is now transferred correctly and safely in r_string.
- Leak/double-free risk paths in move-related scenarios are eliminated.
- New regression tests protect against future breakage.

Compatibility / Impact
This is an internal JSON memory-safety fix and does not change the public API. It improves runtime safety and stability.This PR fixes a memory-management issue in json::detail::r_string caused by incorrect move semantics.

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.

1 participant