Skip to content

[BUG] dynarray_insert() has same double capacity increase bug as push() #6

Description

@seesee010

Bug Description

dynarray_insert() has the same double capacity increase bug as dynarray_push().

Location

src/modify.c lines 164-170

Current Code

if (array->length >= array->capacity) {
    if (!dynarray_inc(array, array->capacity)) {
        return false;
    }
}

array->capacity *= 2;  // ← Doubles capacity AGAIN after inc() already did!

Problem

Same as bug #4 in dynarray_push():

  1. dynarray_inc() already increases capacity
  2. Then multiplication happens again
  3. Results in exponential memory waste

Expected Behavior

Should only resize once.

Proposed Fix

if (array->length >= array->capacity) {
    size_t new_capacity = array->capacity * 2;
    if (!dynarray_setSize(array, new_capacity)) {
        return false;
    }
}
// Remove the extra capacity *= 2 line

Related Issue

This is the same root cause as the push() capacity bug

Impact

  • Severity: Medium
  • Memory waste
  • Less critical than push() because insert() is used less frequently

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