Skip to content

Commit 649c311

Browse files
committed
Don't overflow shift in PyLong_FromLong.
1 parent 1f2d47c commit 649c311

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Objects/longobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ PyLong_FromLong(long ival)
273273
if (!(abs_ival >> PyLong_SHIFT)) {
274274
return _PyLong_FromMedium((sdigit)ival);
275275
}
276-
/* Must be at least two digits */
277-
unsigned long t = abs_ival >> (PyLong_SHIFT *2);
276+
/* Must be at least two digits.
277+
* Do shift in two steps to avoid undefined behavior. */
278+
unsigned long t = (abs_ival >> PyLong_SHIFT) >> PyLong_SHIFT;
278279
Py_ssize_t ndigits = 2;
279280
while (t) {
280281
++ndigits;

0 commit comments

Comments
 (0)