Bug Description
dynarray_push() increments length before calling set(), causing it to write to the wrong index.
Location
src/modify.c lines 122-123
Current Code
array->length++;
isOk = dynarray_set(array, array->length, value); // Uses length as index!
Problem
- Increments
length first (e.g., 0 → 1)
- Then calls
set(array, length, value) which tries to set index 1
- Arrays are 0-indexed, so first element should be at index 0
- Skips the first position and writes to wrong indices
Expected Behavior
Should set at current length, then increment.
Proposed Fix
isOk = dynarray_set(array, array->length, value);
if (isOk) {
array->length++;
}
return isOk;
Impact
- Severity: High
- First element never gets written
- All elements written to wrong positions
- Combined with set() bugs, causes complete failure
Reproduction
DynArray *arr = dynarray_create(sizeof(int), 10);
int val1 = 10, val2 = 20;
dynarray_push(arr, &val1);
printf("Length after first push: %zu\n", dynarray_length(arr)); // Shows 1
printf("Should have written to index 0, but wrote to index 1\n");
Bug Description
dynarray_push()incrementslengthbefore callingset(), causing it to write to the wrong index.Location
src/modify.clines 122-123Current Code
Problem
lengthfirst (e.g., 0 → 1)set(array, length, value)which tries to set index 1Expected Behavior
Should set at current length, then increment.
Proposed Fix
Impact
Reproduction