Skip to content

[BUG] dynarray_set() increments length on failure #2

Description

@seesee010

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

  1. Increments length when operation fails
  2. Causes length corruption
  3. 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions