account for alignment padding in monotonic_resource::do_allocate - #1191
account for alignment padding in monotonic_resource::do_allocate#1191Ramya-9353 wants to merge 1 commit into
Conversation
A fresh block obtained from the upstream resource is only aligned to alignof(block), so an over-aligned request can need up to align - alignof(block) bytes of padding on top of n. do_allocate sized the block from n alone, so whenever n + padding exceeded next_size_ the std::align on the new block failed and a null pointer was returned (or BOOST_ASSERT fired). Include the worst-case padding when choosing the block size and reject requests whose padded size would overflow with bad_alloc.
|
An automated preview of the documentation is available at https://1191.json.prtest2.cppalliance.org/libs/json/doc/html/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-08-21 06:39:09 UTC |
|
GCOVR code coverage report https://1191.json.prtest2.cppalliance.org/gcovr/index.html Build time: 2026-08-21 06:55:12 UTC |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1191 +/- ##
========================================
Coverage 93.71% 93.72%
========================================
Files 85 85
Lines 8980 8984 +4
========================================
+ Hits 8416 8420 +4
Misses 564 564
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
|
|

Repro:
monotonic_resource mr; mr.allocate(1024, 64);returns a null pointer in release builds and tripsBOOST_ASSERT(p)in debug (UBSan: "applying non-zero offset 1024 to null pointer" at monotonic_resource.ipp:145). The same happens throughpmr::polymorphic_allocator<T>for anyTwithalignof(T) > alignof(max_align_t)once a pmr vector grows to a power-of-two byte size.Cause:
do_allocatesizes a fresh block fromnalone, but the block payload is onlyalignof(block)aligned, sostd::alignon the new block has no room for the padding an over-aligned request needs whenevern + padding > next_size_.Fix: include the worst-case padding (
align - alignof(block)) when choosing the block size, and reject a padded size that would overflow withbad_alloc, asstatic_resourcealready does. The added test allocates 1024 bytes at alignments from2 * max_alignto 4096 from fresh resources; it fails 7 assertions before the change and passes after.