Skip to content

Commit

Permalink
pythongh-106550: Fix sign conversion in pycore_code.h
Browse files Browse the repository at this point in the history
Fix sign conversion in pycore_code.h and generated_cases.c: use
unsigned integers and cast explicitly when needed.
  • Loading branch information
vstinner committed Dec 2, 2023
1 parent 5c5022b commit d7f63b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,27 +394,28 @@ write_varint(uint8_t *ptr, unsigned int val)
val >>= 6;
written++;
}
*ptr = val;
*ptr = (uint8_t)val;
return written;
}

static inline int
write_signed_varint(uint8_t *ptr, int val)
{
unsigned int uval;
if (val < 0) {
val = ((-val)<<1) | 1;
uval = ((unsigned int)(-val) << 1) | 1;
}
else {
val = val << 1;
uval = (unsigned int)val << 1;
}
return write_varint(ptr, val);
return write_varint(ptr, uval);
}

static inline int
write_location_entry_start(uint8_t *ptr, int code, int length)
{
assert((code & 15) == code);
*ptr = 128 | (code << 3) | (length - 1);
*ptr = 128 | ((uint8_t)code << 3) | (uint8_t)(length - 1);
return 1;
}

Expand Down Expand Up @@ -454,9 +455,9 @@ write_location_entry_start(uint8_t *ptr, int code, int length)


static inline uint16_t
adaptive_counter_bits(int value, int backoff) {
return (value << ADAPTIVE_BACKOFF_BITS) |
(backoff & ((1<<ADAPTIVE_BACKOFF_BITS)-1));
adaptive_counter_bits(uint16_t value, uint16_t backoff) {
return ((value << ADAPTIVE_BACKOFF_BITS)
| (backoff & ((1 << ADAPTIVE_BACKOFF_BITS) - 1)));
}

static inline uint16_t
Expand All @@ -473,12 +474,12 @@ adaptive_counter_cooldown(void) {

static inline uint16_t
adaptive_counter_backoff(uint16_t counter) {
unsigned int backoff = counter & ((1<<ADAPTIVE_BACKOFF_BITS)-1);
uint16_t backoff = counter & ((1 << ADAPTIVE_BACKOFF_BITS) - 1);
backoff++;
if (backoff > MAX_BACKOFF_VALUE) {
backoff = MAX_BACKOFF_VALUE;
}
unsigned int value = (1 << backoff) - 1;
uint16_t value = (uint16_t)(1 << backoff) - 1;
return adaptive_counter_bits(value, backoff);
}

Expand Down
4 changes: 2 additions & 2 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d7f63b4

Please sign in to comment.