Skip to content

Reclaim owned right operand of string concat (leak fix)#24

Closed
loreste wants to merge 2 commits into
mainfrom
fix/concat-owned-operand-leak
Closed

Reclaim owned right operand of string concat (leak fix)#24
loreste wants to merge 2 commits into
mainfrom
fix/concat-owned-operand-leak

Conversation

@loreste

@loreste loreste commented Jul 24, 2026

Copy link
Copy Markdown
Owner

Fixes a pre-existing per-call leak: an owned string temporary used as the right operand of + (e.g. "row-" + int_to_string(n)) was copied by mako_str_concat but never freed (mako_str_concat_own only consumes the left operand). One buffer leaked per concat, accumulating in long-running string builders.

The right operand is now freed after the concat when it is an unambiguously owned temporary, gated by expr_is_scope_drop_safe (excludes literal views, index, and method borrows — no double-free). int_to_string / format_int recognized as owned.

Verification (Linux): concat churn leak-free under LeakSanitizer (was 499 leaks); full suite green under AddressSanitizer (380/0, no double-free/UAF); both backends pass new concat_owned_operand_test.mko. Follows #23 (same leak family).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a memory leak that could occur when concatenating strings with temporary values produced by integer conversion or formatting functions.
    • Improved cleanup during chained string concatenation while preserving safe handling of shared or borrowed values.
  • Tests
    • Added coverage for temporary string ownership, helper-returned values, chained concatenation, and result correctness.
  • Documentation
    • Updated the changelog with details of the memory-safety improvement.

An owned string temporary used as the right operand of `+` (e.g.
`"row-" + int_to_string(n)`) was copied by mako_str_concat but never
freed — one buffer leaked per concat, accumulating in a long-running
builder. The right operand is now freed after the concat when it is an
unambiguously owned temporary (gated by expr_is_scope_drop_safe, which
excludes literal views / index / method borrows, so no double-free).
int_to_string and format_int results are recognized as owned.

Verified on Linux: concat churn leak-free under LeakSanitizer, full
suite green under AddressSanitizer (no double-free/UAF). New
concat_owned_operand_test.mko.
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@loreste, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 29 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4a45e420-528d-4f81-8de5-f87c4979d6c3

📥 Commits

Reviewing files that changed from the base of the PR and between a011a40 and 9d9c82b.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • src/codegen/mod.rs
📝 Walkthrough

Walkthrough

String concatenation code generation now detects safe owned right-hand operands, frees their temporary buffers after concatenation, and recognizes integer-string helpers in leak handling. New example tests cover direct, helper-returned, chained, and value-correct concatenations.

Changes

String concatenation ownership

Layer / File(s) Summary
Ownership-aware concat generation
src/codegen/mod.rs
int_to_string and format_int are included in leak handling, and owned right-hand MakoString operands are copied to temporaries and freed after mako_str_concat.
Concat ownership validation
examples/testing/concat_owned_operand_test.mko, CHANGELOG.md
Tests cover repeated temporary concatenations, returned builders, chained expressions, and "n=42" value correctness; the changelog documents the fix.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CodeGenerator
  participant GeneratedConcat
  participant RightOperandBuffer
  CodeGenerator->>GeneratedConcat: Emit ownership-aware concat code
  GeneratedConcat->>RightOperandBuffer: Bind owned right operand
  GeneratedConcat->>RightOperandBuffer: Free buffer after mako_str_concat
Loading

Suggested reviewers: attahn, loreste32

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: reclaiming owned right-hand string operands during concatenation to fix a leak.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/concat-owned-operand-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
examples/testing/concat_owned_operand_test.mko (1)

25-30: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Strengthen the stress-test value assertions.

TestConcatReturnedBuilder only checks that the aggregate length is positive, so an incorrect non-empty label result can pass. TestConcatChain should use exact equality instead of substring containment to catch extra output.

Also applies to: 35-36

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/testing/concat_owned_operand_test.mko` around lines 25 - 30,
Strengthen assertions in TestConcatReturnedBuilder and TestConcatChain: replace
the aggregate length positivity check with equality against the exact expected
total for labels 0 through 63, and replace substring containment in
TestConcatChain with exact string equality against the expected concatenated
result.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@examples/testing/concat_owned_operand_test.mko`:
- Around line 25-30: Strengthen assertions in TestConcatReturnedBuilder and
TestConcatChain: replace the aggregate length positivity check with equality
against the exact expected total for labels 0 through 63, and replace substring
containment in TestConcatChain with exact string equality against the expected
concatenated result.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: eba9f0d2-527c-4171-b487-84b0ffb565b1

📥 Commits

Reviewing files that changed from the base of the PR and between c1bdbdb and a011a40.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • examples/testing/concat_owned_operand_test.mko
  • src/codegen/mod.rs

Recognize base64_encode / base32_encode / base64_decode / bytes_to_hex /
csv_join_row / auth_bearer / auth_basic_header as owned producers so
their results free at scope exit / after concat instead of leaking one
buffer per call. Their output is freshly computed (never a borrowed view
of an argument), so this is double-free-safe. string(x) stays excluded —
it is polymorphic (identity for string input).

Verified on Linux: base64_encode drops from 2 to 1 leaked allocation per
call (result reclaimed; the residual is an internal builtin-impl leak,
tracked separately); full suite green under AddressSanitizer (380/0, no
double-free/UAF).
@loreste

loreste commented Jul 24, 2026

Copy link
Copy Markdown
Owner Author

Superseded by the rebase onto the networking/Diameter main; the fixes are re-applied and re-validated in the new PR.

@loreste loreste closed this Jul 24, 2026
@loreste
loreste deleted the fix/concat-owned-operand-leak branch July 24, 2026 18:29
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.

2 participants