Skip to content

Cherry-Pick: Fix memnew_placement with char * arguments (godotengine/godot#112033) - #1417

Open
morgothaufheroin88 wants to merge 2 commits into
Redot-Engine:masterfrom
morgothaufheroin88:fix/memnew-placement-char
Open

morgothaufheroin88 wants to merge 2 commits into
Redot-Engine:masterfrom
morgothaufheroin88:fix/memnew-placement-char

Conversation

@morgothaufheroin88

@morgothaufheroin88 morgothaufheroin88 commented Sep 13, 2026

Copy link
Copy Markdown

Cherry-pick of godotengine/godot#112033 (upstream commit 65bf51600652bddc8faea0f7f6a1ead0e28da81b, merged 2025-10-25), plus a regression test.

memnew_placement(ptr, T(...)) expands to ::new (ptr) T(...). When ptr is a char *, overload resolution prefers operator new(size_t, const char *p_description) (the "new with description" overload in core/os/memory.h) over the standard operator new(size_t, void *), because char *const char * is a better conversion than char *void *. The object is then constructed in a freshly allocated block and the intended storage is never written.

Concretely, LocalVector<char>::push_back(), insert(), resize_initialized() and memnew_arr_placement() on char storage silently produce garbage and leak. Nothing in the current tree hits this (all LocalVector<char> users go through resize() + memcpy), but I ran into it immediately while writing a follow-up FileAccess::get_line() PR, and upstream hit it as data loss (godotengine/godot#112022).

The upstream fix adds an operator new(size_t, char *) overload that forwards to placement new. It is applied verbatim with the original author and (cherry picked from ...) trailer; the test is a separate commit.

Testing

  • New test [LocalVector] Push Back char. fails on master (CHECK( vector[0] == 'a' )0 == 97) and passes with the fix.
  • Full unit-test suite: 1343/1343 test cases pass on the fixed build.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed placement construction for character-buffer destinations, preventing incorrect overload selection.
    • Improved reliability when adding and inserting elements in local character vectors.
  • Tests

    • Added regression coverage for character vector insertion, resizing, and zero-initialized elements.

Ivorforce and others added 2 commits September 13, 2026 20:14
… the description version, allocating on the heap instead of ni the given memory.

(cherry picked from commit 65bf51600652bddc8faea0f7f6a1ead0e28da81b)
Covers the memnew_placement() overload-resolution bug fixed in the
previous commit (godotengine/godot#112033): with a char* placement
address the description overload of operator new was picked, so
push_back()/insert()/resize_initialized() on a LocalVector<char> wrote
into a freshly allocated block and left the vector's storage untouched.

The test fails on the unfixed code (elements read back as 0) and passes
with the fix.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@morgothaufheroin88
morgothaufheroin88 requested review from a team September 13, 2026 17:15
@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 9246d8b8-31d6-4497-8e98-b87a7ae2ff2d

📥 Commits

Reviewing files that changed from the base of the PR and between 2ab24bc and 3e6231b.

📒 Files selected for processing (2)
  • core/os/memory.h
  • tests/core/templates/test_local_vector.h

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


Walkthrough

The change adds a char * placement-new overload that forwards to the existing void * overload. A LocalVector<char> test covers insertion and initialized resizing.

Changes

Placement-new overload fix

Layer / File(s) Summary
Add overload and validate LocalVector<char> behavior
core/os/memory.h, tests/core/templates/test_local_vector.h
Adds operator new(size_t, char *) and forwards its destination to the existing placement-new overload. Adds a regression test for LocalVector<char> insertion, resizing, element order, and zero initialization.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: mcdubhghlas, generalprotectionfault

Merge Risk: ⚪ Minimal · up to 3e623

The placement-new fix has appropriate regression coverage and no remaining merge-blocking risk was identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the cherry-pick and the main fix for memnew_placement calls with char * arguments.
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.
  • Fix all pre-merge checks with AI
✨ 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.

@JoltedJon JoltedJon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested and see that it's fixed with this PR. Thanks for bringing this in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants