Skip to content

Commit db5936c

Browse files
skirpichevvstinner
andauthored
gh-143050: Correct PyLong_FromString() to use _PyLong_Negate() (#145901)
The long_from_string_base() might return a small integer, when the _pylong.py is used to do conversion. Hence, we must be careful here to not smash it "small int" bit by using the _PyLong_FlipSign(). Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 829e4d0 commit db5936c

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

Include/internal/pycore_long.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@ _PyLong_IsPositive(const PyLongObject *op)
232232
return (op->long_value.lv_tag & SIGN_MASK) == 0;
233233
}
234234

235+
/* Return true if the argument is a small int */
236+
static inline bool
237+
_PyLong_IsSmallInt(const PyLongObject *op)
238+
{
239+
assert(PyLong_Check(op));
240+
bool is_small_int = (op->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
241+
assert(PyLong_CheckExact(op) || (!is_small_int));
242+
assert(_Py_IsImmortal(op) || (!is_small_int));
243+
assert((_PyLong_IsCompact(op)
244+
&& _PY_IS_SMALL_INT(_PyLong_CompactValue(op)))
245+
|| (!is_small_int));
246+
return is_small_int;
247+
}
248+
235249
static inline Py_ssize_t
236250
_PyLong_DigitCount(const PyLongObject *op)
237251
{
@@ -293,7 +307,9 @@ _PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
293307
#define NON_SIZE_MASK ~(uintptr_t)((1 << NON_SIZE_BITS) - 1)
294308

295309
static inline void
296-
_PyLong_FlipSign(PyLongObject *op) {
310+
_PyLong_FlipSign(PyLongObject *op)
311+
{
312+
assert(!_PyLong_IsSmallInt(op));
297313
unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
298314
op->long_value.lv_tag &= NON_SIZE_MASK;
299315
op->long_value.lv_tag |= flipped_sign;

Lib/test/test_capi/test_long.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,16 @@ def to_digits(num):
803803
self.assertEqual(pylongwriter_create(negative, digits), num,
804804
(negative, digits))
805805

806+
def test_bug_143050(self):
807+
with support.adjust_int_max_str_digits(0):
808+
# Bug coming from using _pylong.int_from_string(), that
809+
# currently requires > 6000 decimal digits.
810+
int('-' + '0' * 7000, 10)
811+
_testcapi.test_immortal_small_ints()
812+
# Test also nonzero small int
813+
int('-' + '0' * 7000 + '123', 10)
814+
_testcapi.test_immortal_small_ints()
815+
806816

807817
if __name__ == "__main__":
808818
unittest.main()

Modules/_testcapi/immortal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
3131
for (int i = -5; i <= 1024; i++) {
3232
PyObject *obj = PyLong_FromLong(i);
3333
assert(verify_immortality(obj));
34-
int has_int_immortal_bit = ((PyLongObject *)obj)->long_value.lv_tag & IMMORTALITY_BIT_MASK;
34+
int has_int_immortal_bit = _PyLong_IsSmallInt((PyLongObject *)obj);
3535
assert(has_int_immortal_bit);
3636
}
3737
for (int i = 1025; i <= 1030; i++) {
3838
PyObject *obj = PyLong_FromLong(i);
3939
assert(obj);
40-
int has_int_immortal_bit = ((PyLongObject *)obj)->long_value.lv_tag & IMMORTALITY_BIT_MASK;
40+
int has_int_immortal_bit = _PyLong_IsSmallInt((PyLongObject *)obj);
4141
assert(!has_int_immortal_bit);
4242
Py_DECREF(obj);
4343
}

Objects/longobject.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,11 +3119,11 @@ PyLong_FromString(const char *str, char **pend, int base)
31193119
}
31203120

31213121
/* Set sign and normalize */
3122-
if (sign < 0) {
3123-
_PyLong_FlipSign(z);
3124-
}
31253122
long_normalize(z);
31263123
z = maybe_small_long(z);
3124+
if (sign < 0) {
3125+
_PyLong_Negate(&z);
3126+
}
31273127

31283128
if (pend != NULL) {
31293129
*pend = (char *)str;
@@ -3623,21 +3623,11 @@ long_richcompare(PyObject *self, PyObject *other, int op)
36233623
Py_RETURN_RICHCOMPARE(result, 0, op);
36243624
}
36253625

3626-
static inline int
3627-
/// Return 1 if the object is one of the immortal small ints
3628-
_long_is_small_int(PyObject *op)
3629-
{
3630-
PyLongObject *long_object = (PyLongObject *)op;
3631-
int is_small_int = (long_object->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
3632-
assert((!is_small_int) || PyLong_CheckExact(op));
3633-
return is_small_int;
3634-
}
3635-
36363626
void
36373627
_PyLong_ExactDealloc(PyObject *self)
36383628
{
36393629
assert(PyLong_CheckExact(self));
3640-
if (_long_is_small_int(self)) {
3630+
if (_PyLong_IsSmallInt((PyLongObject *)self)) {
36413631
// See PEP 683, section Accidental De-Immortalizing for details
36423632
_Py_SetImmortal(self);
36433633
return;
@@ -3652,7 +3642,7 @@ _PyLong_ExactDealloc(PyObject *self)
36523642
static void
36533643
long_dealloc(PyObject *self)
36543644
{
3655-
if (_long_is_small_int(self)) {
3645+
if (_PyLong_IsSmallInt((PyLongObject *)self)) {
36563646
/* This should never get called, but we also don't want to SEGV if
36573647
* we accidentally decref small Ints out of existence. Instead,
36583648
* since small Ints are immortal, re-set the reference count.

0 commit comments

Comments
 (0)