Fix out-of-bounds write in Vector initializer_list constructor#113
Open
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
Open
Fix out-of-bounds write in Vector initializer_list constructor#113CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
Conversation
The initializer_list constructor (enabled for N >= 5) has a second loop intended to zero-fill elements beyond those provided. The loop condition was `i < numValues` but should be `i < N`: - When numValues > N, the loop writes past the end of the fixed-size mTuple array (buffer overflow). - When numValues < N, the loop never executes, leaving elements [numValues..N-1] uninitialized instead of zeroed as documented. Changing the bound to N fixes both problems. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
initializer_listconstructor inVector.h(line 82, enabled forN >= 5) has a loop bound bug that causes two problems:1. Buffer overflow when more than N initializers are provided:
The second loop at line 82 uses
i < numValuesas its bound. WhennumValues > N, the first loop correctly copies onlymin(numValues, N)elements, but the second loop then continues writing zeros tomTuple[N],mTuple[N+1], ...,mTuple[numValues-1]— all past the end of the fixed-sizestd::array<Real, N>.2. Missing zero-fill when fewer than N initializers are provided:
The comment on line 66 documents the intent: "At most N elements are copied from the initializer list, setting any remaining elements to zero." However, when
numValues < N, the second loop conditioni < numValuesis immediately false (sincei == numValuesafter the first loop), so elements[numValues..N-1]are left uninitialized.Fix
One-character change:
numValues→Non line 82.This fixes both problems: the loop now stops at N (preventing overflow) and runs from
numValuestoN(zeroing remaining elements).Test program
The test below demonstrates both bugs against the unfixed code and passes cleanly after the fix.
To build: place both files in a directory, run
cmake . -B build && cmake --build build, then./build/test_issue_1_1.Output BEFORE fix:
Output AFTER fix:
CMakeLists.txt
test_issue_1_1.cpp
🤖 Generated with Claude Code