Skip to content

NimBLEAttValue.cpp - preserve capacity after realloc failure#430

Open
chmorgan wants to merge 1 commit into
h2zero:masterfrom
chmorgan:nimbleattrvalue_realloc
Open

NimBLEAttValue.cpp - preserve capacity after realloc failure#430
chmorgan wants to merge 1 commit into
h2zero:masterfrom
chmorgan:nimbleattrvalue_realloc

Conversation

@chmorgan

@chmorgan chmorgan commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

NimBLEAttValue::append advanced m_capacity immediately after calling realloc. If realloc failed, the original buffer remained allocated but the object advertised a larger capacity than it actually owned. A later append could then skip reallocation and copy past the end of the allocation.

Track whether the append actually needs growth and only update m_capacity after the realloc result passes the null check. Appends that fit the existing allocation leave capacity unchanged; failed growth preserves the old capacity invariant.

Summary by CodeRabbit

  • Bug Fixes
    • Improved data append behavior to better handle memory allocation failures.
    • Prevented internal capacity values from being updated when an expansion attempt fails, keeping state consistent and avoiding incorrect size reporting after errors.

NimBLEAttValue::append advanced m_capacity immediately after calling realloc.
If realloc failed, the original buffer remained allocated but the object advertised a larger capacity
than it actually owned. A later append could then skip reallocation and copy past the end of the allocation.

Track whether the append actually needs growth and only update m_capacity after the realloc result
passes the null check. Appends that fit the existing allocation leave capacity unchanged;
failed growth preserves the old capacity invariant.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eac25d12-9551-4012-a88f-78f3b2984975

📥 Commits

Reviewing files that changed from the base of the PR and between 657583a and ede097a.

📒 Files selected for processing (1)
  • src/NimBLEAttValue.cpp

📝 Walkthrough

Walkthrough

Arr, this here PR patches up the NimBLEAttValue::append method, matey! It adds a grow flag to guide when reallocation be needed, and shifts the m_capacity update to only happen after realloc be a success, avoidin' a false capacity count when the reallocation fails.

Changes

AttValue Append Reallocation Fix

Layer / File(s) Summary
Guard capacity update on realloc success
src/NimBLEAttValue.cpp
Introduces a grow condition to control reallocation, and moves the m_capacity update so it only fires after realloc succeeds, keepin' the ship's hold accurate even when allocation fails.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Poem

Yarrr, the buffer grows with care,
No false capacity in the air,
realloc succeeds, then we record,
A tighter ship, this rabbit's aboard! 🏴‍☠️🐇
Set sail, the code be shipshape now!

🚥 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 Arrr, the title clearly describes the main fix in NimBLEAttValue.cpp.
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

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.

@chmorgan chmorgan force-pushed the nimbleattrvalue_realloc branch from efe7a72 to ede097a Compare July 6, 2026 01:35
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