@@ -377,6 +377,37 @@ TEST(arena_total_through_reset) {
377377 PASS ();
378378}
379379
380+ TEST (arena_reset_block_size_invariant ) {
381+ /* BUG DETECTOR: after reset, block_size should match blocks[0]'s actual size.
382+ * arena_grow() updates block_size to the new (larger) block's size.
383+ * arena_reset() frees the grown blocks but does NOT restore block_size.
384+ * Result: arena thinks blocks[0] has more capacity than it actually does.
385+ * This is a heap-buffer-overflow waiting to happen. */
386+ CBMArena a ;
387+ cbm_arena_init_sized (& a , 128 );
388+ size_t original_block_size = a .block_size ;
389+ ASSERT_EQ (original_block_size , 128 );
390+ ASSERT_EQ (a .block_sizes [0 ], 128 );
391+
392+ /* Force growth: block_size will be updated to new (larger) value */
393+ cbm_arena_alloc (& a , 100 );
394+ cbm_arena_alloc (& a , 100 ); /* triggers grow */
395+ ASSERT_GTE (a .nblocks , 2 );
396+ ASSERT_GT (a .block_size , 128 ); /* block_size grew */
397+
398+ /* Reset: frees grown blocks, keeps blocks[0] (which is 128 bytes) */
399+ cbm_arena_reset (& a );
400+ ASSERT_EQ (a .nblocks , 1 );
401+
402+ /* CRITICAL CHECK: block_size must match blocks[0]'s actual capacity.
403+ * If block_size > block_sizes[0], the arena will allow allocations
404+ * that overflow blocks[0]. */
405+ ASSERT_EQ (a .block_size , a .block_sizes [0 ]);
406+
407+ cbm_arena_destroy (& a );
408+ PASS ();
409+ }
410+
380411TEST (arena_strndup_zero_len ) {
381412 /* strndup with len=0 — should return empty string */
382413 CBMArena a ;
@@ -419,5 +450,6 @@ SUITE(arena) {
419450 RUN_TEST (arena_calloc_zero );
420451 RUN_TEST (arena_many_large_allocs_block_growth );
421452 RUN_TEST (arena_total_through_reset );
453+ RUN_TEST (arena_reset_block_size_invariant );
422454 RUN_TEST (arena_strndup_zero_len );
423455}
0 commit comments