From 19eb401498488db1e5c39fc199e59bf96df2828d Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 23 Oct 2023 00:28:39 +0100 Subject: Fixed bugs with ordering of bytes in some operations Also fixed an overflow error in vm_dup_word, where I underflow the index of vm->stack.data which causes a buffer overflow. --- src/runtime.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/runtime.c b/src/runtime.c index f653ac3..32350e1 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -504,7 +504,8 @@ err_t vm_dup_word(vm_t *vm, word w) return ERR_STACK_UNDERFLOW; byte bytes[WORD_SIZE] = {0}; for (size_t i = 0; i < WORD_SIZE; ++i) - bytes[i] = vm->stack.data[vm->stack.ptr - 1 - (WORD_SIZE * (w + 1)) + i]; + bytes[WORD_SIZE - i - 1] = + vm->stack.data[vm->stack.ptr - (WORD_SIZE * (w + 1)) + i]; return vm_push_word(vm, DWORD(convert_bytes_to_word(bytes))); } @@ -525,7 +526,7 @@ err_t vm_pop_hword(vm_t *vm, data_t *ret) { data_t b = {0}; vm_pop_byte(vm, &b); - bytes[HWORD_SIZE - 1 - i] = b.as_byte; + bytes[i] = b.as_byte; } *ret = DWORD(convert_bytes_to_hword(bytes)); return ERR_OK; @@ -540,7 +541,7 @@ err_t vm_pop_word(vm_t *vm, data_t *ret) { data_t b = {0}; vm_pop_byte(vm, &b); - bytes[WORD_SIZE - 1 - i] = b.as_byte; + bytes[i] = b.as_byte; } *ret = DWORD(convert_bytes_to_word(bytes)); return ERR_OK; -- cgit v1.2.3-13-gbd6f