summaryrefslogtreecommitdiffstats
path: root/Include
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 /Include
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 'Include')
-rw-r--r--Include/cpython/longintrepr.h3
1 files changed, 3 insertions, 0 deletions
diff --git a/Include/cpython/longintrepr.h b/Include/cpython/longintrepr.h
index 68dbf9c..6d52427 100644
--- a/Include/cpython/longintrepr.h
+++ b/Include/cpython/longintrepr.h
@@ -71,6 +71,9 @@ typedef long stwodigits; /* signed variant of twodigits */
0 <= ob_digit[i] <= MASK.
The allocation function takes care of allocating extra memory
so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
+ We always allocate memory for at least one digit, so accessing ob_digit[0]
+ is always safe. However, in the case ob_size == 0, the contents of
+ ob_digit[0] may be undefined.
CAUTION: Generic code manipulating subtypes of PyVarObject has to
aware that ints abuse ob_size's sign bit.