diff --git a/lib/libesp32/berry/src/be_lexer.c b/lib/libesp32/berry/src/be_lexer.c index 6607d065d..6b600dc72 100644 --- a/lib/libesp32/berry/src/be_lexer.c +++ b/lib/libesp32/berry/src/be_lexer.c @@ -429,7 +429,7 @@ static bint scan_hexadecimal(blexer *lexer) bint res = 0; int dig, num = 0; while ((dig = be_char2hex(lgetc(lexer))) >= 0) { - res = ((bint)res << 4) + dig; + res = (bint)(((buint)res << 4) + (buint)dig); next(lexer); ++num; } diff --git a/lib/libesp32/berry/src/be_strlib.c b/lib/libesp32/berry/src/be_strlib.c index 1c95f57f0..4bc290d38 100644 --- a/lib/libesp32/berry/src/be_strlib.c +++ b/lib/libesp32/berry/src/be_strlib.c @@ -387,7 +387,7 @@ BERRY_API bint be_str2int(const char *str, const char **endstr) /* hex literal */ str += 2; /* skip 0x or 0X */ while ((c = be_char2hex(*str++)) >= 0) { - sum = sum * 16 + c; + sum = (bint)((buint)sum * 16 + (buint)c); } if (endstr) { *endstr = str - 1; diff --git a/lib/libesp32/berry/src/berry.h b/lib/libesp32/berry/src/berry.h index 702fec97c..81e91f8c8 100644 --- a/lib/libesp32/berry/src/berry.h +++ b/lib/libesp32/berry/src/berry.h @@ -48,8 +48,9 @@ extern "C" { #endif #define BE_INT_FORMAT "%" BE_INT_FMTLEN "d" /**< BE_INT_FORMAT */ -typedef uint8_t bbyte; /**< bbyte */ -typedef BE_INTEGER bint; /**< bint */ +typedef uint8_t bbyte; /**< bbyte */ +typedef BE_INTEGER bint; /**< bint */ +typedef unsigned BE_INTEGER buint; /**< buint (unsigned bint, for well-defined wrap-around arithmetic) */ #if BE_USE_SINGLE_FLOAT != 0 typedef float breal; /**< breal */ diff --git a/lib/libesp32/berry_int64/src/be_int64_class.c b/lib/libesp32/berry_int64/src/be_int64_class.c index c6a107dd8..c32adbacc 100644 --- a/lib/libesp32/berry_int64/src/be_int64_class.c +++ b/lib/libesp32/berry_int64/src/be_int64_class.c @@ -28,7 +28,7 @@ static void int64_toa(int64_t num, uint8_t* str) { uint64_t sum = num; if (num < 0) { - sum = -num; + sum = -(uint64_t)num; /* negate in unsigned domain to avoid UB on INT64_MIN */ str[0] = '-'; str++; } @@ -226,7 +226,7 @@ static int int64_fromu32(bvm *vm) { int argc = be_top(vm); uint32_t low = (uint32_t)be_toint(vm, 1); uint32_t high = (argc > 1) ? (uint32_t)be_toint(vm, 2) : 0; - int64_t val = (int64_t)low | (((int64_t)high) << 32); + int64_t val = (int64_t)((uint64_t)low | ((uint64_t)high << 32)); push_int64_instance(vm, val); be_return(vm); } @@ -347,7 +347,7 @@ static int int64_mod(bvm *vm) { static int int64_shiftleft(bvm *vm) { int64_t *i64 = self_get_p(vm); int32_t j32 = be_toint(vm, 2) & 63; - push_int64_instance(vm, *i64 << j32); + push_int64_instance(vm, (int64_t)((uint64_t)*i64 << j32)); be_return(vm); }