gh-154387: validate PyStructSequence_Desc field invariants#154684
Open
XuehaiPan wants to merge 2 commits into
Open
gh-154387: validate PyStructSequence_Desc field invariants#154684XuehaiPan wants to merge 2 commits into
PyStructSequence_Desc field invariants#154684XuehaiPan wants to merge 2 commits into
Conversation
`count_members()` now raises `SystemError` (returning `-1`) when a struct sequence descriptor places an unnamed field outside the visible sequence fields, or when `n_in_sequence` exceeds the total number of fields; such descriptors previously built a malformed type. The failure is propagated by `PyStructSequence_NewType()`, `PyStructSequence_InitType()` and `PyStructSequence_InitType2()`. Add `_testcapi` helpers and regression tests for both rejected cases, and document the invariants in the C API reference.
Documentation build overview
|
…ifiers count_members() now rejects a PyStructSequence_Desc whose n_in_sequence is negative, completing the bounds check (only n_in_sequence <= n_fields was enforced before). A negative value otherwise inflated tp_basicsize and failed later with a cryptic SystemError from PyTuple_New(). Also fix the two existing error messages: n_in_sequence is an int but was printed with %zd (Py_ssize_t), which is undefined behavior in a variadic call. Add a _testcapi helper and regression test for the negative case, and document the non-negative constraint.
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.
The current
PyStructSequencerelated code is heavily under the assumptions:{any unnamed field} in {visible range}n_unnamed <= n_in_sequence and n_in_sequence <= n_fieldsbut we never validate these constraints. Data can be silently lost (unaccessible) if an unnamed field is defined out of the visible range, i.e., the data cannot be accessed either from slicing
seq[i]or member lookupseq.name.This PR enforces the constraints mentioned above.
count_members()now raisesSystemError(returning-1) when a struct sequence descriptor places an unnamed field outside the visible sequence fields, or whenn_in_sequenceexceeds the total number of fields; such descriptors previously built a malformed type. The failure is propagated byPyStructSequence_NewType(),PyStructSequence_InitType()andPyStructSequence_InitType2().Add
_testcapihelpers and regression tests for both rejected cases, and document the invariants in the C API reference.structseq_reprmislabels unnamedPyStructSequencefields (e.g.os.stat_resultslots 7–9) #154387