Skip to content

Commit

Permalink
Don't overflow shift in PyLong_FromLong.
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon committed Aug 23, 2021
1 parent 1f2d47c commit 649c311
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ PyLong_FromLong(long ival)
if (!(abs_ival >> PyLong_SHIFT)) {
return _PyLong_FromMedium((sdigit)ival);
}
/* Must be at least two digits */
unsigned long t = abs_ival >> (PyLong_SHIFT *2);
/* Must be at least two digits.
* Do shift in two steps to avoid undefined behavior. */
unsigned long t = (abs_ival >> PyLong_SHIFT) >> PyLong_SHIFT;
Py_ssize_t ndigits = 2;
while (t) {
++ndigits;
Expand Down

0 comments on commit 649c311

Please sign in to comment.