Mutable heap PyStructSequence types can segfault after modifying n_fields
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Crash report
What happened?
Several heap PyStructSequence types can be crashed from pure Python by modifying the writable n_fields attribute on the type and then constructing a new instance.
The issue reproduces with at least:
os.terminal_sizeos.stat_resulttime.struct_timeresource.struct_rusage
Minimal reproducer:
import os
os.terminal_size.n_fields = 100000
os.terminal_size((1, 2))
This consistently terminates the interpreter with:
Segmentation fault (core dumped)
The crash also reproduces for other mutable heap PyStructSequence types by assigning a large value to n_fields before construction.
The backtrace shows the crash occurring in structseq_new_impl():
#0 __strlen_avx2()
#1 PyUnicode_FromString()
#2 PyDict_GetItemStringRef()
#3 structseq_new_impl() at Objects/structseq.c:243
At the point of failure:
max_len = 100000
i = 2
n_unnamed_fields = 0
From inspecting Objects/structseq.c, structseq_new_impl() uses the type's n_fields value to determine how many member names to process. After modifying n_fields from Python, the constructor eventually reaches a NULL member name, leading to a crash through PyDict_GetItemStringRef() and PyUnicode_FromString().
During investigation I also verified that immutable builtin structseq types such as sys.version_info are not affected because their type attributes cannot be modified and new instances cannot be created.
I searched the existing issue tracker using keywords including:
structseq_new_implPyStructSequence_NewTypen_fieldsObjects/structseq.cPyDict_GetItemStringReftp_members
but could not find an existing report describing this behavior.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/main:e469fa9c807, Aug 7 2026, 09:37:12) [GCC 13.3.0]
Linked PRs
- gh-155361
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par Objects/structseq.c et structseq_new_impl(), puis exécutez le reproducteur minimal de os.terminal_size décrit dans l’issue. Comparez le traitement de la valeur n_fields accessible en écriture avec les noms de membres disponibles ; le travail est terminé lorsque la construction des types concernés ne provoque plus de plantage et que la couverture de régression démontre ce comportement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- c, python
- Domaine
- backend
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100