Skip to content

Commit 50a2e36

Browse files
gh-88116: Avoid undefined behavior when decoding varints in code objects (GH-94375)
(cherry picked from commit c485ec0) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 6484692 commit 50a2e36

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
@@ -346,9 +346,9 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
346346
static int
347347
scan_varint(const uint8_t *ptr)
348348
{
349-
int read = *ptr++;
350-
int val = read & 63;
351-
int shift = 0;
349+
unsigned int read = *ptr++;
350+
unsigned int val = read & 63;
351+
unsigned int shift = 0;
352352
while (read & 64) {
353353
read = *ptr++;
354354
shift += 6;
@@ -360,7 +360,7 @@ scan_varint(const uint8_t *ptr)
360360
static int
361361
scan_signed_varint(const uint8_t *ptr)
362362
{
363-
int uval = scan_varint(ptr);
363+
unsigned int uval = scan_varint(ptr);
364364
if (uval & 1) {
365365
return -(int)(uval >> 1);
366366
}
@@ -839,9 +839,9 @@ read_byte(PyCodeAddressRange *bounds)
839839
static int
840840
read_varint(PyCodeAddressRange *bounds)
841841
{
842-
int read = read_byte(bounds);
843-
int val = read & 63;
844-
int shift = 0;
842+
unsigned int read = read_byte(bounds);
843+
unsigned int val = read & 63;
844+
unsigned int shift = 0;
845845
while (read & 64) {
846846
read = read_byte(bounds);
847847
shift += 6;
@@ -853,7 +853,7 @@ read_varint(PyCodeAddressRange *bounds)
853853
static int
854854
read_signed_varint(PyCodeAddressRange *bounds)
855855
{
856-
int uval = read_varint(bounds);
856+
unsigned int uval = read_varint(bounds);
857857
if (uval & 1) {
858858
return -(int)(uval >> 1);
859859
}

0 commit comments

Comments
 (0)