summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2023-01-21 10:23:59 (GMT)
committerGitHub <noreply@github.com>2023-01-21 10:23:59 (GMT)
commit401fdf9c851eb61229250ebffa942adca99b36d1 (patch)
tree01afb0dae67636403b5955f4aef8ddddd6a21568 /Objects/longobject.c
parent9e947675ae3dc32f5863e5ed3022301cf7fd79b4 (diff)
downloadcpython-401fdf9c851eb61229250ebffa942adca99b36d1.zip
cpython-401fdf9c851eb61229250ebffa942adca99b36d1.tar.gz
cpython-401fdf9c851eb61229250ebffa942adca99b36d1.tar.bz2
gh-101037: Fix potential memory underallocation for zeros of int subtypes (#101038)
This PR fixes object allocation in long_subtype_new to ensure that there's at least one digit in all cases, and makes sure that the value of that digit is copied over from the source long. Needs backport to 3.11, but not any further: the change to require at least one digit was only introduced for Python 3.11. Fixes #101037.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 1db4ca4..51ac869 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);