Skip to content

Commit 614cca0

Browse files
Copilotsyoyo
andcommitted
Address code review: fix sizeof guard, improve comments
Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
1 parent 4bb4e2b commit 614cca0

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

tests/tester.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,8 +4153,8 @@ void test_loadobjopt_nan_inf_values() {
41534153

41544154
void test_arena_adapter_overflow_guard() {
41554155
// Verify that arena_adapter::allocate rejects SIZE_MAX/sizeof(T) overflow.
4156-
// We can't actually allocate SIZE_MAX bytes, but we can verify the function
4157-
// returns nullptr (when exceptions are disabled, the default).
4156+
// When TINYOBJLOADER_ENABLE_EXCEPTION is not defined, the allocator returns
4157+
// nullptr on overflow. When exceptions are enabled, it throws std::bad_alloc.
41584158
tinyobj::ArenaAllocator arena;
41594159
tinyobj::arena_adapter<double> adapter(&arena);
41604160
// Request an allocation that would overflow size_t when multiplied by

tiny_obj_loader.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ class arena_adapter {
827827
: arena_(other.arena()) {}
828828

829829
T *allocate(size_t n) {
830-
if (sizeof(T) > 1 && n > SIZE_MAX / sizeof(T)) {
830+
if (n > SIZE_MAX / sizeof(T)) {
831831
#ifdef TINYOBJLOADER_ENABLE_EXCEPTION
832832
throw std::bad_alloc();
833833
#else
@@ -9856,8 +9856,9 @@ void *ArenaAllocator::allocate(size_t bytes, size_t alignment) {
98569856
size_t space = b->capacity;
98579857
void *ptr = b->data;
98589858
if (!std::align(alignment, bytes, ptr, space)) {
9859-
// Should never happen: block capacity >= bytes + alignment, but guard
9860-
// against implementation quirks.
9859+
// Defensive guard: the block was allocated with capacity >= bytes + alignment,
9860+
// so alignment should always succeed. This handles edge cases where the
9861+
// capacity was insufficient due to unusual alignment requirements.
98619862
#ifdef TINYOBJLOADER_ENABLE_EXCEPTION
98629863
throw std::bad_alloc();
98639864
#else

0 commit comments

Comments
 (0)