Skip to content

Commit 2c8ccd3

Browse files
jeplerdpgeorge
authored andcommitted
py: Fix undefined left shift operations.
By ensuring the value to be shifted is an unsigned of the appropriate type. This fixes several runtime diagnostics such as: ../../py/binary.c:199:28: runtime error: left shift of 32768 by 16 places cannot be represented in type 'int' Signed-off-by: Jeff Epler <[email protected]>
1 parent 8162468 commit 2c8ccd3

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

py/binary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static float mp_decode_half_float(uint16_t hf) {
196196
++e;
197197
}
198198

199-
fpu.i = ((hf & 0x8000) << 16) | (e << 23) | (m << 13);
199+
fpu.i = ((hf & 0x8000u) << 16) | (e << 23) | (m << 13);
200200
return fpu.f;
201201
}
202202

py/persistentcode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ static void bit_vector_clear(bit_vector_t *self) {
759759
static bool bit_vector_is_set(bit_vector_t *self, size_t index) {
760760
const size_t bits_size = sizeof(*self->bits) * MP_BITS_PER_BYTE;
761761
return index / bits_size < self->alloc
762-
&& (self->bits[index / bits_size] & (1 << (index % bits_size))) != 0;
762+
&& (self->bits[index / bits_size] & ((uintptr_t)1 << (index % bits_size))) != 0;
763763
}
764764

765765
static void bit_vector_set(bit_vector_t *self, size_t index) {
@@ -770,7 +770,7 @@ static void bit_vector_set(bit_vector_t *self, size_t index) {
770770
self->bits = m_renew(uintptr_t, self->bits, self->alloc, new_alloc);
771771
self->alloc = new_alloc;
772772
}
773-
self->bits[index / bits_size] |= 1 << (index % bits_size);
773+
self->bits[index / bits_size] |= (uintptr_t)1 << (index % bits_size);
774774
}
775775

776776
typedef struct _mp_opcode_t {

0 commit comments

Comments
 (0)