]> granicus.if.org Git - python/commitdiff
Issue #24999: In longobject.c, use two shifts instead of ">> 2*PyLong_SHIFT" to
authorVictor Stinner <victor.stinner@gmail.com>
Sat, 19 Sep 2015 11:39:03 +0000 (13:39 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sat, 19 Sep 2015 11:39:03 +0000 (13:39 +0200)
avoid undefined behaviour when LONG_MAX type is smaller than 60 bits.

This change should fix a warning with the ICC compiler.

Objects/longobject.c

index 40da0b15af1878741553d8316dc601fa34dcc783..5f224557d1f7d6c42829ebab72016ff9e93a4829 100644 (file)
@@ -4495,11 +4495,13 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
 simple:
     assert(Py_REFCNT(a) > 0);
     assert(Py_REFCNT(b) > 0);
-#if LONG_MAX >> 2*PyLong_SHIFT
+/* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid
+   undefined behaviour when LONG_MAX type is smaller than 60 bits */
+#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
     /* a fits into a long, so b must too */
     x = PyLong_AsLong((PyObject *)a);
     y = PyLong_AsLong((PyObject *)b);
-#elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> 2*PyLong_SHIFT
+#elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
     x = PyLong_AsLongLong((PyObject *)a);
     y = PyLong_AsLongLong((PyObject *)b);
 #else
@@ -4516,9 +4518,9 @@ simple:
         y = x % y;
         x = t;
     }
-#if LONG_MAX >> 2*PyLong_SHIFT
+#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
     return PyLong_FromLong(x);
-#elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> 2*PyLong_SHIFT
+#elif defined(PY_LONG_LONG) && PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
     return PyLong_FromLongLong(x);
 #else
 # error "_PyLong_GCD"