]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126835: Fix _PY_IS_SMALL_INT() macro (#146631)
authorVictor Stinner <vstinner@python.org>
Mon, 30 Mar 2026 12:48:18 +0000 (14:48 +0200)
committerGitHub <noreply@github.com>
Mon, 30 Mar 2026 12:48:18 +0000 (12:48 +0000)
Include/internal/pycore_long.h
Objects/longobject.c
Python/flowgraph.c

index d545ba0c3abb522b6d8d28cbe74af7f10dbbcdb0..4386e8bcad88411f5a169aed6b85035cb203f246 100644 (file)
@@ -64,7 +64,8 @@ PyAPI_FUNC(void) _PyLong_ExactDealloc(PyObject *self);
 #  error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
 #endif
 
-#define _PY_IS_SMALL_INT(val) ((val) >= 0 && (val) < 256 && (val) < _PY_NSMALLPOSINTS)
+#define _PY_IS_SMALL_INT(val) \
+    (-_PY_NSMALLNEGINTS <= (val) && (val) < _PY_NSMALLPOSINTS)
 
 // Return a reference to the immortal zero singleton.
 // The function cannot return NULL.
index f1971f0fca993c5ba86eafb62090a92f72042ba6..0d3ea9bc46c321cac6088bc2b4555765e19185d7 100644 (file)
@@ -26,7 +26,7 @@ class int "PyObject *" "&PyLong_Type"
 
 #define medium_value(x) ((stwodigits)_PyLong_CompactValue(x))
 
-#define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
+#define IS_SMALL_INT(ival) _PY_IS_SMALL_INT(ival)
 #define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)
 
 #define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d digits) for integer string conversion: value has %zd digits; use sys.set_int_max_str_digits() to increase the limit"
index f446a87ee6943219b0fab9f2de53db79b9e252a0..e988f4451007fb3462e6573a10462120de8518e6 100644 (file)
@@ -1411,7 +1411,7 @@ maybe_instr_make_load_smallint(cfg_instr *instr, PyObject *newconst,
         if (val == -1 && PyErr_Occurred()) {
             return -1;
         }
-        if (!overflow && _PY_IS_SMALL_INT(val)) {
+        if (!overflow && _PY_IS_SMALL_INT(val) && 0 <= val && val <= 255) {
             assert(_Py_IsImmortal(newconst));
             INSTR_SET_OP1(instr, LOAD_SMALL_INT, (int)val);
             return 1;