diff options
author | Victor Stinner <vstinner@python.org> | 2023-12-04 10:42:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-04 10:42:58 (GMT) |
commit | a74902a14cdc0952abf7bfabcf529c9b132c5cce (patch) | |
tree | 714e7fa35a132ae2cf5bec72cd6db7813fbed57f | |
parent | dee7beeb4f9d28fec945c8c495027cc22a512328 (diff) | |
download | cpython-a74902a14cdc0952abf7bfabcf529c9b132c5cce.zip cpython-a74902a14cdc0952abf7bfabcf529c9b132c5cce.tar.gz cpython-a74902a14cdc0952abf7bfabcf529c9b132c5cce.tar.bz2 |
gh-106550: Fix sign conversion in pycore_code.h (#112613)
Fix sign conversion in pycore_code.h: use unsigned integers and cast
explicitly when needed.
-rw-r--r-- | Include/internal/pycore_code.h | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index eaf84a9..73df6c3 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -394,27 +394,29 @@ 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; + // (unsigned int)(-val) has an undefined behavior for INT_MIN + uval = ((0 - (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; } @@ -454,9 +456,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 @@ -473,12 +475,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); } |