BUG: fix CPU feature env diagnostic buffer overruns (#31905) - #31953
Merged
Conversation
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.
Backport of #31905.
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.