gh-88116: Avoid undefined behavior when decoding varints in code objects (GH-94375)

(cherry picked from commit c485ec014c)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-06-28 06:54:30 -07:00 committed by GitHub
parent 648469299d
commit 50a2e36ce9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View file

@ -346,9 +346,9 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
static int
scan_varint(const uint8_t *ptr)
{
int read = *ptr++;
int val = read & 63;
int shift = 0;
unsigned int read = *ptr++;
unsigned int val = read & 63;
unsigned int shift = 0;
while (read & 64) {
read = *ptr++;
shift += 6;
@ -360,7 +360,7 @@ scan_varint(const uint8_t *ptr)
static int
scan_signed_varint(const uint8_t *ptr)
{
int uval = scan_varint(ptr);
unsigned int uval = scan_varint(ptr);
if (uval & 1) {
return -(int)(uval >> 1);
}
@ -839,9 +839,9 @@ read_byte(PyCodeAddressRange *bounds)
static int
read_varint(PyCodeAddressRange *bounds)
{
int read = read_byte(bounds);
int val = read & 63;
int shift = 0;
unsigned int read = read_byte(bounds);
unsigned int val = read & 63;
unsigned int shift = 0;
while (read & 64) {
read = read_byte(bounds);
shift += 6;
@ -853,7 +853,7 @@ read_varint(PyCodeAddressRange *bounds)
static int
read_signed_varint(PyCodeAddressRange *bounds)
{
int uval = read_varint(bounds);
unsigned int uval = read_varint(bounds);
if (uval & 1) {
return -(int)(uval >> 1);
}