diff options
author | Ionite <dev@ionite.io> | 2023-01-02 21:11:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-02 21:11:49 (GMT) |
commit | d7e7f79ca7c2029e46a06d21a7a5abea631b5d13 (patch) | |
tree | 5ac123fa1dcaeebf3707476785fa1e9cf9317587 /Objects/longobject.c | |
parent | 9dee9731663d670652586c929190f227ab56bd8f (diff) | |
download | cpython-d7e7f79ca7c2029e46a06d21a7a5abea631b5d13.zip cpython-d7e7f79ca7c2029e46a06d21a7a5abea631b5d13.tar.gz cpython-d7e7f79ca7c2029e46a06d21a7a5abea631b5d13.tar.bz2 |
gh-100637: Fix int and bool __sizeof__ calculation to include the 1 element ob_digit array for 0 and False (#100663)
Fixes behaviour where int (and subtypes like bool) __sizeof__ under-reports true size as it did not take into account the size 1 `ob_digit` array for the zero int.
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 0df3b9a..1db4ca4 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5879,7 +5879,10 @@ int___sizeof___impl(PyObject *self) { Py_ssize_t res; - res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); + res = offsetof(PyLongObject, ob_digit) + /* using Py_MAX(..., 1) because we always allocate space for at least + one digit, even though the integer zero has a Py_SIZE of 0 */ + + Py_MAX(Py_ABS(Py_SIZE(self)), 1)*sizeof(digit); return res; } |