Skip to content

Commit c485ec0

Browse files
authored
gh-88116: Avoid undefined behavior when decoding varints in code objects (#94375)
1 parent 44fa03d commit c485ec0

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue when reading line numbers from code objects if the encoded line
2+
numbers are close to ``INT_MIN``. Patch by Pablo Galindo

Objects/codeobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,9 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
354354
static int
355355
scan_varint(const uint8_t *ptr)
356356
{
357-
int read = *ptr++;
358-
int val = read & 63;
359-
int shift = 0;
357+
unsigned int read = *ptr++;
358+
unsigned int val = read & 63;
359+
unsigned int shift = 0;
360360
while (read & 64) {
361361
read = *ptr++;
362362
shift += 6;
@@ -368,7 +368,7 @@ scan_varint(const uint8_t *ptr)
368368
static int
369369
scan_signed_varint(const uint8_t *ptr)
370370
{
371-
int uval = scan_varint(ptr);
371+
unsigned int uval = scan_varint(ptr);
372372
if (uval & 1) {
373373
return -(int)(uval >> 1);
374374
}
@@ -847,9 +847,9 @@ read_byte(PyCodeAddressRange *bounds)
847847
static int
848848
read_varint(PyCodeAddressRange *bounds)
849849
{
850-
int read = read_byte(bounds);
851-
int val = read & 63;
852-
int shift = 0;
850+
unsigned int read = read_byte(bounds);
851+
unsigned int val = read & 63;
852+
unsigned int shift = 0;
853853
while (read & 64) {
854854
read = read_byte(bounds);
855855
shift += 6;
@@ -861,7 +861,7 @@ read_varint(PyCodeAddressRange *bounds)
861861
static int
862862
read_signed_varint(PyCodeAddressRange *bounds)
863863
{
864-
int uval = read_varint(bounds);
864+
unsigned int uval = read_varint(bounds);
865865
if (uval & 1) {
866866
return -(int)(uval >> 1);
867867
}

0 commit comments

Comments
 (0)