harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit bedd275fb46fd85919b64e1bc6d97663ce408168
parent 85793f307add8b31fdda881d5575f51b704c986d
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Tue,  7 Feb 2023 05:02:54 +0100

eval_binarithm: avoid illegal field accesses

When the constant is of type bool, accessing bytes that are not part of
constant.bval is UB and so on.

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msrc/eval.c | 18++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/eval.c b/src/eval.c @@ -176,10 +176,20 @@ eval_binarithm(struct context *ctx, struct expression *in, struct expression *ou return r; } - bool blval = lvalue.constant.bval, brval = rvalue.constant.bval, bval; - intmax_t ilval = lvalue.constant.ival, irval = rvalue.constant.ival, ival; - uintmax_t ulval = lvalue.constant.uval, urval = rvalue.constant.uval, uval; - double flval = lvalue.constant.fval, frval = rvalue.constant.fval, fval; + bool blval, brval, bval; + intmax_t ilval, irval, ival; + uintmax_t ulval, urval, uval; + double flval, frval, fval; + if (type_is_float(lvalue.result)) { + flval = lvalue.constant.fval, frval = rvalue.constant.fval; + } else if (type_is_signed(lvalue.result)) { + ilval = lvalue.constant.ival, irval = rvalue.constant.ival; + } else if (type_is_integer(lvalue.result)) { + ulval = lvalue.constant.uval, urval = rvalue.constant.uval; + } else if (type_dealias(lvalue.result)->storage == STORAGE_BOOL) { + blval = lvalue.constant.bval, brval = rvalue.constant.bval; + } + // Type promotion is lowered in check assert(lvalue.result->storage == rvalue.result->storage); switch (in->binarithm.op) {