GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-154387: validate `PyStructSequence_Desc` field invariants by XuehaiPan · Pull Request #154684 · python/cpython

Conversation

@XuehaiPan

@XuehaiPan XuehaiPan commented Jul 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The current PyStructSequence related code is heavily under the assumptions:

  • {every unnamed field} in {visible range}
  • n_unnamed <= n_in_sequence and n_in_sequence <= n_fields
  • n_in_sequence >= 0

but 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 lookup seq.name.

This PR enforces the constraints mentioned above. 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.

  • Issue: structseq_repr mislabels unnamed PyStructSequence fields (e.g. os.stat_result slots 7–9) #154387

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@XuehaiPan
fix(structseq): validate PyStructSequence_Desc field invariants
4260f76
`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.
@XuehaiPan
XuehaiPan requested a review from ZeroIntensity as a code owner July 25, 2026 09:00
@bedevere-app bedevere-app Bot mentioned this pull request Jul 25, 2026
structseq_repr mislabels unnamed PyStructSequence fields (e.g. os.stat_result slots 7–9) #154387
Open
@bedevere-app bedevere-app Bot added the awaiting review label Jul 25, 2026
@read-the-docs-community

read-the-docs-community Bot commented Jul 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35579092 | 📁 Comparing ba9b93e against main (bff0051)

  🔍 Preview build  

434 files changed · + 10 added · ± 424 modified

+ Added

  • builtins/constants.html
  • builtins/exceptions.html
  • builtins/functions.html
  • builtins/index.html
  • builtins/stdtypes.html
  • builtins/threadsafety.html
  • builtins/time-complexity.html
  • c-api/pyexpat.html
  • library/time-complexity.html
  • library/tkinter.dialogs.html

± Modified

  • glossary.html
  • index.html
  • license.html
  • py-modindex.html
  • _static/tachyon-example-heatmap.html
  • c-api/arg.html
  • c-api/bool.html
  • c-api/buffer.html
  • c-api/bytearray.html
  • c-api/bytes.html
  • and 414 more...
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

XuehaiPan added 7 commits July 25, 2026 18:06
@XuehaiPan
fix(structseq): reject negative n_in_sequence and fix int format spec…
a06b349
…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.
@XuehaiPan
fix: remove test_ prefix for functions in _testcapi
29c4c2e
@XuehaiPan
Merge remote-tracking branch 'origin/main' into fix-structseq-validat…
34ae4ba
…e-desc
@XuehaiPan
Merge branch 'main' into fix-structseq-validate-desc
b49a073
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
de1210f
…ate-desc
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
0d573e9
…ate-desc
@XuehaiPan
ci: try fix ci
57095f0
XuehaiPan
XuehaiPan commented Jul 29, 2026
View reviewed changes
Comment thread Lib/test/test_class.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@XuehaiPan XuehaiPan mentioned this pull request Jul 29, 2026
gh-154387: fix structseq_repr by labeling unnamed fields in repr as <unnamed@N> #154687
Open
XuehaiPan added 8 commits August 4, 2026 14:58
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
7f8b035
…ate-desc
@XuehaiPan
ci: try fix ci
d5771c0
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
7508a14
…ate-desc
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
c1c5795
…ate-desc
@XuehaiPan
Merge branch 'main' into fix-structseq-validate-desc
9ba8e7c
@XuehaiPan
Merge branch 'main' into fix-structseq-validate-desc
245642f
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
54a1721
…ate-desc
@XuehaiPan
Merge remote-tracking branch 'upstream/main' into fix-structseq-valid…
ba9b93e
…ate-desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@XuehaiPan
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.