builtin: guard array push len panics#27335
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d9951abbb1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| if a.len >= max_int { | ||
| panic('array.push: len bigger than max_int') | ||
| $if !no_bounds_checking { |
There was a problem hiding this comment.
Keep the max_int push guard enabled
When compiling with -no-bounds-checking and appending to an array whose len is already max_int (for example a very large byte array or one constructed through unsafe/C interop), this wrapper removes the only guard before required := a.len + 1; that addition then overflows before the capacity check and the subsequent write uses the old max_int offset, causing UB/memory corruption instead of the previous panic. -no-bounds-checking disables index checks, but other capacity-overflow guards such as push_many/insert_many remain unconditional, so the max length guard should stay outside the $if (or be replaced with an overflow-safe computation); the same applies to push_noscan.
Useful? React with 👍 / 👎.
Fixes #27334.
What changed
array.pushandarray.push_noscannegative-length sanity checks with$if !no_bounds_checking.max_intlength guards unconditional sorequired := a.len + 1cannot overflow when bounds checking is disabled.-prod -no-bounds-checkinggenerated C omits the negative-length panic paths while retaining the max-length overflow guards for both regular and noscan array pushes.Why
The negative-length checks are bounds-checking sanity paths and can be compiled out consistently with existing array access checks. The
max_intchecks are capacity-overflow guards, so they remain active even with-no-bounds-checking.Validation
./v -g -keepc -o ./vnew cmd/v./vnew test vlib/builtin/array_test.v./vnew -silent vlib/v/gen/c/coutput_test.v