Skip to content

Fix ASan alloc-dealloc mismatch in nothrow new test hooks - #103

Merged
brofield merged 2 commits into
masterfrom
cursor/asan-nothrow-new-8865
Oct 4, 2026
Merged

brofield merged 2 commits into
masterfrom
cursor/asan-nothrow-new-8865

Conversation

@brofield

@brofield brofield commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

The Linux allocation-failure hooks in tests/ts-regressions.cpp replaced nothrow operator new / new[] with __wrap_malloc. That is required: SimpleIni allocates with new (std::nothrow), which libstdc++ implements in a DSO, so --wrap=malloc never sees those calls.

Returning malloc memory from operator new made AddressSanitizer abort before any test body. testing::TestSuite::Run() calls std::stable_sort, libstdc++ get_temporary_buffer uses ::operator new(n, std::nothrow), and the matching free is sized ::operator delete. ASan reports malloc vs operator delete.

Keep the same fail-after-N / fail-small policy for both __wrap_malloc and the nothrow new replacements. On success, nothrow new now delegates to throwing ::operator new so delete[] matches. Do not call __wrap_malloc from operator new, or the budget would decrement twice.

Also cover every SI_NOMEM allocation site that the default Linux build can reach:

  • First-load LoadData parse buffer (new SI_CHAR[uLen+1])
  • LoadFile read buffer (new char[uSize+1])
  • LoadFile read buffer succeeds, then LoadData parse buffer fails
  • CopyString via SetValue (new returns nullptr)
  • Incremental LoadData file-comment CopyString failure
  • Incremental LoadData AddEntry / CopyString failure (existing leak test no longer starts with a file comment, so it actually hits AddEntry)

Not covered, on purpose: ICU UCharPathToUtf8 (not in the default build) and std::map bad_alloc (not turned into SI_NOMEM).

Validation

  • Official Debug suite: 290 tests pass (GCC 13.3, C++17)
  • ASan+UBSan with default alloc_dealloc_mismatch=1 and detect_leaks=1: 290 tests pass
  • LoadDataRegression and LoadFileRegression OOM cases pass in both builds
Open in Web Open in Cursor 

cursoragent and others added 2 commits October 4, 2026 07:02
The LoadData OOM tests still fail SimpleIni's new (nothrow) through a
shared policy, but the success path now uses throwing ::operator new so
ASan no longer reports malloc vs operator delete on GoogleTest/libstdc++
temporary buffers.

Co-authored-by: Brodie Thiesfield <brofield@users.noreply.github.com>
Add tests for first-load parse-buffer failure, LoadFile read-buffer
failure, LoadFile-then-LoadData parse-buffer failure, SetValue
CopyString new failure, and file-comment CopyString cleanup. The
existing incremental leak test no longer starts with a file comment,
so it actually fails in AddEntry.

Co-authored-by: Brodie Thiesfield <brofield@users.noreply.github.com>
@brofield
brofield marked this pull request as ready for review October 4, 2026 07:12
@brofield
brofield merged commit ba82f70 into master Oct 4, 2026
14 checks passed
@brofield
brofield deleted the cursor/asan-nothrow-new-8865 branch October 4, 2026 11:49
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