python / python/cpython

readline.c: Missing NULL check on PyLong_FromLong

Abierto
#154,385 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

extension-modules type-crash
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

Original gist: https://gist.github.com/devdanzin/60aed55f44fba133ee2d34d46aa8d197

readline.c: Missing NULL check on PyLong_FromLong in setup_readline

Summary

PyLong_FromLong(0L) at lines 1418-1419 stored in begidx/endidx without NULL check. On OOM during module init, readline.get_begidx() does Py_NewRef(NULL) → segfault.

Reproducer

import _testcapi, sys
if "readline" in sys.modules: del sys.modules["readline"]
for n in range(1, 80):
    if "readline" in sys.modules: del sys.modules["readline"]
    _testcapi.set_nomemory(n, 0)
    try:
        import readline
        _testcapi.remove_mem_hooks()
        break
    except MemoryError:
        _testcapi.remove_mem_hooks()
    except ImportError:
        _testcapi.remove_mem_hooks()
    except:
        _testcapi.remove_mem_hooks()
# Assertion: obj != NULL in PyStackRef_FromPyObjectSteal

The original gist said we should check the result of PyLong_FromLong(0L), but I think this is a false positive, because 0 is small int and should not failed while creating a Python int object from it.

However there are other line of code in this file for calling PyLong_FromLong with begidx/endidx withoud check the result:

https://github.com/python/cpython/blob/d333e5aa59f6cc20198502f4ff50b14eabde9b8b/Modules/readline.c#L1330-L1331

I guess the LLM found a real bug but reported it at the wrong place. Either way, we should fix it.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-154386

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Lee Modules/readline.c alrededor de setup_readline y las llamadas a PyLong_FromLong cerca de las líneas 1330-1331; primero ejecuta el reproductor de memory-hook de _testcapi proporcionado e inspecciona el PR vinculado gh-154386. Se considera terminado cuando la ruta afectada de falta de memoria ya no permite que un objeto NULL llegue a readline accessors y las pruebas relevantes de CPython pasan.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
c, python
Área
cli
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.