Skip to content

Commit 91ace76

Browse files
committed
PyLongWriter_Finish() detects uninitialized digits
1 parent a14685f commit 91ace76

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Include/cpython/longintrepr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ _PyLong_CompactValue(const PyLongObject *op)
134134
assert(PyUnstable_Long_IsCompact(op));
135135
sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
136136
if (sign == 0) {
137-
// gh-147988: Make sure that the digit is zero,
138-
// it helps detecting usage of uninitialized digits.
137+
// gh-147988: Make sure that the digit is zero.
138+
// It helps detecting the usage of uninitialized digits.
139139
assert(op->long_value.ob_digit[0] == 0);
140140
}
141141
return sign * (Py_ssize_t)op->long_value.ob_digit[0];

Objects/longobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6958,6 +6958,20 @@ PyLongWriter_Finish(PyLongWriter *writer)
69586958
PyLongObject *obj = (PyLongObject *)writer;
69596959
assert(Py_REFCNT(obj) == 1);
69606960

6961+
#ifdef Py_DEBUG
6962+
// gh-147988: Detect uninitialized digits:
6963+
// long_alloc() fills digits with 0xFF byte pattern.
6964+
Py_ssize_t ndigits = _PyLong_DigitCount(obj);
6965+
if (ndigits == 0) {
6966+
// Check ob_digit[0] digit for the number zero
6967+
ndigits = 1;
6968+
}
6969+
for (Py_ssize_t i=0; i < ndigits; i++) {
6970+
digit d = obj->long_value.ob_digit[i];
6971+
assert(d < PyLong_BASE);
6972+
}
6973+
#endif
6974+
69616975
// Normalize and get singleton if possible
69626976
obj = maybe_small_long(long_normalize(obj));
69636977

0 commit comments

Comments
 (0)