From 16afc16769bee303d18042df78a6c4391c5dc9d3 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 22 Aug 2022 21:33:45 +0100 Subject: [PATCH] gh-96187: Prevent _PyCode_GetExtra to return garbage for negative indexes --- .../2022-08-22-21-33-28.gh-issue-96187.W_6SRG.rst | 2 ++ Objects/codeobject.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-08-22-21-33-28.gh-issue-96187.W_6SRG.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-22-21-33-28.gh-issue-96187.W_6SRG.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-22-21-33-28.gh-issue-96187.W_6SRG.rst new file mode 100644 index 00000000000000..fd194faa68545d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-22-21-33-28.gh-issue-96187.W_6SRG.rst @@ -0,0 +1,2 @@ +Fixed a bug that caused ``_PyCode_GetExtra`` to return garbage for negative +indexes. Patch by Pablo Galindo diff --git a/Objects/codeobject.c b/Objects/codeobject.c index aeb6a8c0804e54..72712f40e42c7c 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1339,7 +1339,7 @@ _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra) PyCodeObject *o = (PyCodeObject*) code; _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra; - if (co_extra == NULL || co_extra->ce_size <= index) { + if (co_extra == NULL || index < 0 || co_extra->ce_size <= index) { *extra = NULL; return 0; }