BUG: fix CPU feature env diagnostic buffer overruns - #31905
Merged
Conversation
Avoid the trailing-space-and-trim pattern for nonexistent CPU features in npy__cpu_check_env. A maximum-length invalid feature name could write the terminating NUL one byte past the end of `nexist`.
ngoldbaum
approved these changes
Jul 9, 2026
Member
There was a problem hiding this comment.
Just two very minor nits, otherwise this looks good to me. The increased stack size doesn't matter I think.
I'll probably merge this soon but let's ping @seiko2plus since he originally wrote this code.
Use the maximum accepted environment variable length for the `notsupp` diagnostic buffer in npy__cpu_check_env as repeated unsupported features could overflow the buffer.
Use %zu when formatting var_len, which is a size_t.
stratakis
force-pushed
the
buffers_more_buffers
branch
from
July 9, 2026 19:50
716118d to
6a52575
Compare
Contributor
Author
|
Rebased to address the comments. |
Member
|
Thanks @stratakis! |
charris
pushed a commit
that referenced
this pull request
Jul 10, 2026
charris
added a commit
that referenced
this pull request
Jul 10, 2026
BUG: fix CPU feature env diagnostic buffer overruns (#31905)
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.
PR summary
This is a follow up to gh-30877.
I've had on the back on my mind to verify and check some other issues I saw in the file and finally managed to get to it.
The original PR fixed a buffer overrun in CPU baseline validation. This PR fixes two similar issues in
npy__cpu_check_env.The two issues fixed:
nexiststack buffernotsuppdiagnostic buffer, which was sized fromNPY_WITH_CPU_DISPATCHrather than from the accepted environment variable length.Both fixes avoid the trailing-space-and-trim pattern when building the diagnostic strings.
Noting though that the fix for the
notsuppbuffer increases stack usage innpy__cpu_check_envbecause the buffer is now bounded byNPY__MAX_VAR_LENinstead ofsizeof(NPY_WITH_CPU_DISPATCH) + 1. On my x86_64 laptop,-fstack-usagereports the function growing from 2224 bytes to 3184 bytes, a 960 bytes increase. The function is non-recursive and only runs while parsing CPU feature environment variables during import, however if the size icrease is deemed unacceptable I can change the approach to something like heap allocation, it will require more code changes though.These tests are mainly sanitizer regressions, in non-sanitized builds, the previous out-of-bounds writes might not crash.
AI Disclosure
I used codex with the gpt 5.5 model. Guided to create the tests, (hence the tests are AI generated) and to review the C code changes, also to verify any incompatibilities (where it verified the stack increase) and to create small reproducers with the same logic for me to actually verify the issues exist.