compile: double free of pc->fail_pop in codegen_pattern_or on allocation failure with nested or-pattern
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 55/100
Línea de trabajo
Start in Python/codegen.c at codegen_pattern_or and codegen_match, using the reported lines 6573 and 6775 to trace ownership of fail_pop through the allocation failure path. Reproduce with the provided _testcapi.call_with_nomemory script on an ASan build; done when injected failures raise MemoryError, the pointer is freed once, and the script prints “survived.”
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
When an allocation fails while compiling a match statement whose or-pattern contains another or-pattern, codegen_pattern_or frees pc->fail_pop and codegen_match then frees it again, so compile() crashes instead of raising MemoryError.
The failure is injected with the test-only _testcapi.call_with_nomemory:
import ast, _testcapi
tree = ast.parse('def f(x):\n match x:\n case [1|2] | 3: pass\n')
for off in range(1500):
held = None
held = [[] for _ in range(128)] # refill/empty the list freelist so PyList_New in codegen really allocates
try:
_testcapi.call_with_nomemory(off, off + 1, compile, tree, 'a.py', 'exec')
except (MemoryError, SyntaxError):
pass
print('survived')
Saved as input.py and run on an ASan build that includes _testcapi:
python input.py
Output:
ERROR: AddressSanitizer: heap-use-after-free on address 0x503000002988
READ of size 1 at 0x503000002988 thread T0
#0 in _PyMem_DebugCheckAddress Objects/obmalloc.c:3351
#4 in PyMem_Free Objects/obmalloc.c:1292
#5 in codegen_match Python/codegen.c:6775
...
freed by thread T0 here:
#5 in PyMem_Free Objects/obmalloc.c:1292
#6 in codegen_pattern_or Python/codegen.c:6573
Every injected failure should surface as MemoryError from compile(), with the pointer freed once, and the script should print survived.
Found at commit 3646ea571f3d8290806a3029d555b179f8be1539. It still reproduces at 3acc941f2b72 (latest upstream checked): ASan heap-use-after-free in the checking build, and the release build segfaults (exit code 139). case (1|2) | 3, where the inner or-pattern is not inside a sequence pattern, survives.
Looks like in the inner codegen_pattern_or, old_pc = *pc copies the outer fail_pop array, and if the first PyList_New(0) in the loop fails, goto error runs before pc->fail_pop = NULL. The error block then frees old_pc.fail_pop while pc->fail_pop still points to it, and codegen_match frees it again.
Found with an LLM-based testing tool; I used Claude to reduce it and look for the cause.
Linked PRs
- gh-160875
- Lenguaje dominante
- Python
- Estrellas
- 77.8k
- Forks
- 37.7k
- Merge medio
- 17 h 24 min
- PR fusionados (30 d)
- 716
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python/cpython
-
build OS-unsupported OS-windows type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 60/100
Los mantenedores suelen responder en 1 día
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689Posiblemente ocupada @TheLouisMahdi la tomó hace 1 día. Abiertodocs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
python/cpython#161078 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
OS-windows type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
python/cpython#161007 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
-
Null dereferencing of a pointer in initconfig.cPosiblemente ocupada @lazerg la tomó hace 4 días. Abiertointerpreter-core topic-C-API type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
python/cpython#160962 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de python/cpython
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
microsoft/markitdown#2820 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
yt-project/yt#5624 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
NousResearch/hermes-agent#148376 ·
Los mantenedores suelen responder en 1 día