Skip to content

Set the capacity of a Vector made with initWithElements - #44

Merged
jdolan merged 1 commit into
mainfrom
fix/vector-init-with-elements-capacity
Oct 12, 2026
Merged

jdolan merged 1 commit into
mainfrom
fix/vector-init-with-elements-capacity

Conversation

@jdolan

@jdolan jdolan commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Vector::initWithElements set count but left capacity at 0. The first add after it then saw count != capacity, did not grow the array, and wrote past its end. copy had the same defect, because it allocates by capacity and copies by count.

The fix sets capacity to count. The initWithElements test now adds to the vector and copies it. Without the fix, the test fails with vector->capacity == 0.

Quetoo found this when it trimmed its bots' link vectors (jdolan/quetoo#1108).

🤖 Generated with Claude Code

Vector::initWithElements set the count but left the capacity at zero, so
the first add after it saw count != capacity, did not grow the array, and
wrote past its end. Copy did the same, since it allocates by capacity and
copies by count. Quetoo found this when it trimmed its bots' link
vectors.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 23:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix restores Vector invariants and includes appropriate regression coverage.

0 open findings

What changed in this PR

Initializes Vector capacity correctly for supplied elements, preventing out-of-bounds writes and invalid copies.

Changes:

  • Sets capacity equal to the initial element count.
  • Adds regression coverage for growth and copying.
File Description
Sources/​Objectively/​Vector.c Initializes capacity in initWithElements.
Tests/​Objectively/​Vector.c Tests capacity, subsequent additions, and copying.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jdolan
jdolan merged commit 54d510a into main Oct 12, 2026
5 checks passed
@jdolan
jdolan deleted the fix/vector-init-with-elements-capacity branch October 12, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants