From ee514323a553596ee2e937a8f8463eb741c09cb6 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 14 Jan 2023 17:01:49 +0000 Subject: [PATCH] gh-101037: Fix memory allocation for zeros of int subtypes --- .../2023-01-14-17-03-08.gh-issue-101037.9ATNuf.rst | 2 ++ Objects/longobject.c | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-01-14-17-03-08.gh-issue-101037.9ATNuf.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-14-17-03-08.gh-issue-101037.9ATNuf.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-14-17-03-08.gh-issue-101037.9ATNuf.rst new file mode 100644 index 000000000000000..a48756657a29d30 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-14-17-03-08.gh-issue-101037.9ATNuf.rst @@ -0,0 +1,2 @@ +Fix potential memory underallocation issue for instances of :class:`int` +subclasses with value zero. diff --git a/Objects/longobject.c b/Objects/longobject.c index 1db4ca418e066ec..51ac86961c99542 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5638,6 +5638,11 @@ long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase) n = Py_SIZE(tmp); if (n < 0) n = -n; + /* Fast operations for single digit integers (including zero) + * assume that there is always at least one digit present. */ + if (n == 0) { + n = 1; + } newobj = (PyLongObject *)type->tp_alloc(type, n); if (newobj == NULL) { Py_DECREF(tmp);