Skip to content

Commit 85f0048

Browse files
committed
mp_bytecode_print_str: avoid undefined behavior
Left shift of negative numbers is undefined in the "C" standard. Multiplying by 128 has the intended effect (in the absence of integer overflow, anyway), can be implemented using the same shift instruction, but does not invoke undefined behavior. This problem was found using clang 7's scan-build static analyzer.
1 parent 46b6870 commit 85f0048

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

py/showbc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const byte *mp_bytecode_print_str(const byte *ip) {
185185
num--;
186186
}
187187
do {
188-
num = (num << 7) | (*ip & 0x7f);
188+
num = (num * 128) | (*ip & 0x7f);
189189
} while ((*ip++ & 0x80) != 0);
190190
printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
191191
break;

0 commit comments

Comments
 (0)