Repository navigation
Conversation
This branch has not been deployed
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.
Description
The
std::array/std::valarraytype caster ininclude/pybind11/stl.hreads the length of the incoming Python object twice from two independent__len__calls: once to size its storage (resize(l.size()), or thel.size() != Sizecheck) and again when the range-for over thesequencefetches itsend()sentinel. A caller can pass an object whose__len__returns different values on successive calls, so the count driving the writes no longer matches the storage that was allocated. In the resizable path the loop then writes(*value)[ctr++]past the end of the resizedvalarray, and in the fixed-size pathvector_to_array<ArrayType, Size>reads past the end of atempvector that ended up shorter thanSize. I hit both while fuzzing the STL casters with a sequence object that mutates its reported length between calls, and AddressSanitizer flags a heap-buffer-overflow write for the valarray case and an OOB read (SEGV) for the array case. The fix snapshots the length once and bounds the write loop against it, and rejects a sequence that produced fewer thanSizeelements before handingtemptovector_to_array, so the conversion stays insideconvert_elementsand never trusts the second length. A regression test drives both paths with a mutating-__len__object and segfaults on the current headers.Suggested changelog entry:
std::array/std::valarraycaster when a Python object reports an inconsistent length across__len__calls during conversion.