Skip to content

Commit 25bbf4d

Browse files
committed
Fix: Use Py_ARITHMETIC_RIGHT_SHIFT to avoid the undefined padding bahavior of C right shift operator.
1 parent dcd2796 commit 25bbf4d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Objects/longobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4497,15 +4497,15 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
44974497
digit lomask, himask, omitmark;
44984498

44994499
/* for a positive integrate x
4500-
-x >> m = -(x >> m) when x is two's pow
4501-
= -(x >> m) -1 otherwise */
4500+
-x >> m = -(x >> m) when the dropped bits are all zeros,
4501+
= -(x >> m) -1 otherwise. */
45024502

45034503
if (IS_MEDIUM_VALUE(a)) {
45044504
stwodigits sval;
45054505
if (wordshift > 0)
4506-
return get_small_int((sdigit)Py_SIZE(a) >> 1);
4506+
return get_small_int(-(Py_SIZE(a) < 0));
45074507

4508-
sval = medium_value(a) >> remshift;
4508+
sval = Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, medium_value(a), remshift);
45094509
if (IS_SMALL_INT(sval))
45104510
return get_small_int((sdigit)sval);
45114511

0 commit comments

Comments
 (0)