From 771956b5b631fc31679455ad0aef332a8a64a401 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 17 Jul 2024 08:50:02 -0500 Subject: [PATCH] Fix some -fsanitize=undefined behavior for #1475 Also fix issue with os/clock and default values that were incorrect. The api shold have been a little nicer here to prevent this issue. --- src/core/bytecode.c | 4 ++-- src/core/math.c | 8 ++++---- src/core/os.c | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/bytecode.c b/src/core/bytecode.c index ff431974..5ab3048c 100644 --- a/src/core/bytecode.c +++ b/src/core/bytecode.c @@ -140,7 +140,7 @@ void janet_bytecode_remove_noops(JanetFuncDef *def) { /* relative pc is in DS field of instruction */ old_jump_target = i + (((int32_t)instr) >> 8); new_jump_target = pc_map[old_jump_target]; - instr += (new_jump_target - old_jump_target + (i - j)) << 8; + instr += (uint32_t)(new_jump_target - old_jump_target + (i - j)) << 8; break; case JOP_JUMP_IF: case JOP_JUMP_IF_NIL: @@ -149,7 +149,7 @@ void janet_bytecode_remove_noops(JanetFuncDef *def) { /* relative pc is in ES field of instruction */ old_jump_target = i + (((int32_t)instr) >> 16); new_jump_target = pc_map[old_jump_target]; - instr += (new_jump_target - old_jump_target + (i - j)) << 16; + instr += (uint32_t)(new_jump_target - old_jump_target + (i - j)) << 16; break; default: break; diff --git a/src/core/math.c b/src/core/math.c index a95d3ab3..f3128de1 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -85,10 +85,10 @@ void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, int32_t len) { uint8_t state[16] = {0}; for (int32_t i = 0; i < len; i++) state[i & 0xF] ^= bytes[i]; - rng->a = state[0] + (state[1] << 8) + (state[2] << 16) + (state[3] << 24); - rng->b = state[4] + (state[5] << 8) + (state[6] << 16) + (state[7] << 24); - rng->c = state[8] + (state[9] << 8) + (state[10] << 16) + (state[11] << 24); - rng->d = state[12] + (state[13] << 8) + (state[14] << 16) + (state[15] << 24); + rng->a = state[0] + ((uint32_t) state[1] << 8) + ((uint32_t) state[2] << 16) + ((uint32_t) state[3] << 24); + rng->b = state[4] + ((uint32_t) state[5] << 8) + ((uint32_t) state[6] << 16) + ((uint32_t) state[7] << 24); + rng->c = state[8] + ((uint32_t) state[9] << 8) + ((uint32_t) state[10] << 16) + ((uint32_t) state[11] << 24); + rng->d = state[12] + ((uint32_t) state[13] << 8) + ((uint32_t) state[14] << 16) + ((uint32_t) state[15] << 24); rng->counter = 0u; /* a, b, c, d can't all be 0 */ if (rng->a == 0) rng->a = 1u; diff --git a/src/core/os.c b/src/core/os.c index 7fb42f4a..125b8289 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -1582,8 +1582,8 @@ JANET_CORE_FN(os_clock, janet_sandbox_assert(JANET_SANDBOX_HRTIME); janet_arity(argc, 0, 2); - JanetKeyword sourcestr = janet_optkeyword(argv, argc, 0, (const uint8_t *) "realtime"); - if (janet_cstrcmp(sourcestr, "realtime") == 0) { + JanetKeyword sourcestr = janet_optkeyword(argv, argc, 0, NULL); + if (sourcestr == NULL || janet_cstrcmp(sourcestr, "realtime") == 0) { source = JANET_TIME_REALTIME; } else if (janet_cstrcmp(sourcestr, "monotonic") == 0) { source = JANET_TIME_MONOTONIC; @@ -1596,8 +1596,8 @@ JANET_CORE_FN(os_clock, struct timespec tv; if (janet_gettime(&tv, source)) janet_panic("could not get time"); - JanetKeyword formatstr = janet_optkeyword(argv, argc, 1, (const uint8_t *) "double"); - if (janet_cstrcmp(formatstr, "double") == 0) { + JanetKeyword formatstr = janet_optkeyword(argv, argc, 1, NULL); + if (formatstr == NULL || janet_cstrcmp(formatstr, "double") == 0) { double dtime = (double)(tv.tv_sec + (tv.tv_nsec / 1E9)); return janet_wrap_number(dtime); } else if (janet_cstrcmp(formatstr, "int") == 0) {