Bug Description
When dynarray_set() fails because index is out of bounds, it incorrectly increments the length before returning false.
Location
src/access.c lines 28-30
Current Code
if (index >= array->capacity) {
array->length++; // ← Why increment on FAILURE?!
return false;
}
Problem
- Increments
length when operation fails
- Causes length corruption
- Makes
push() break because it relies on accurate length
Expected Behavior
Should return false WITHOUT modifying array state on failure.
Proposed Fix
if (index >= array->capacity) {
return false; // Just fail, don't corrupt state
}
Impact
- Severity: High
- Causes incorrect length tracking
- Breaks
dynarray_push() functionality
- Violates principle: failed operations shouldn't modify state
Reproduction
DynArray *arr = dynarray_create(sizeof(int), 2);
int val = 10;
dynarray_set(arr, 5, &val); // Should fail, but corrupts length
printf("Length: %zu\n", dynarray_length(arr)); // Shows 1 instead of 0!
Bug Description
When
dynarray_set()fails because index is out of bounds, it incorrectly increments the length before returning false.Location
src/access.clines 28-30Current Code
Problem
lengthwhen operation failspush()break because it relies on accurate lengthExpected Behavior
Should return false WITHOUT modifying array state on failure.
Proposed Fix
Impact
dynarray_push()functionalityReproduction