From ba7e8a9fb4210d0c9c8aa9b8e33e32ac11c963a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigrid=20Solveig=20Hafl=C3=ADnud=C3=B3ttir?= Date: Sat, 25 Dec 2021 23:29:36 +0100 Subject: [PATCH] uxn_eval: multiply as two uint32s to avoid UB --- src/uxn.c | 4 ++-- src/uxn.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/uxn.c b/src/uxn.c index 06ce139..5d251e0 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -120,7 +120,7 @@ uxn_eval(Uxn *u, Uint16 vec) /* Arithmetic */ case 0x18: /* ADD */ a = pop(u->src), b = pop(u->src); push(u->src, b + a); break; case 0x19: /* SUB */ a = pop(u->src), b = pop(u->src); push(u->src, b - a); break; - case 0x1a: /* MUL */ a = pop(u->src), b = pop(u->src); push(u->src, b * a); break; + case 0x1a: /* MUL */ a = pop(u->src), b = pop(u->src); push(u->src, (Uint32)b * a); break; case 0x1b: /* DIV */ a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); break; case 0x1c: /* AND */ a = pop(u->src), b = pop(u->src); push(u->src, b & a); break; case 0x1d: /* ORA */ a = pop(u->src), b = pop(u->src); push(u->src, b | a); break; @@ -138,7 +138,7 @@ uxn_eval(Uxn *u, Uint16 vec) int uxn_boot(Uxn *u) { - unsigned int i; + Uint32 i; char *cptr = (char *)u; for(i = 0; i < sizeof(*u); ++i) cptr[i] = 0x00; diff --git a/src/uxn.h b/src/uxn.h index 9c31544..5ad45db 100644 --- a/src/uxn.h +++ b/src/uxn.h @@ -13,6 +13,7 @@ typedef unsigned char Uint8; typedef signed char Sint8; typedef unsigned short Uint16; typedef signed short Sint16; +typedef unsigned int Uint32; #define PAGE_PROGRAM 0x0100