From 4e513dbf011e67810aa13fe3d9bdaa8dbddfdcbb Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 18 Jun 2024 23:48:54 +0100 Subject: [PATCH] Fix stupid issues in prog_write_bytecode In particular, not writing the full words for the program header and incrementing bytes prematurely. --- lib/inst.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/inst.c b/lib/inst.c index 6198387..9460c27 100644 --- a/lib/inst.c +++ b/lib/inst.c @@ -434,14 +434,15 @@ size_t prog_write_bytecode(prog_t program, byte_t *bytes, size_t size_bytes) { if (size_bytes < PROG_HEADER_SIZE || prog_bytecode_size(program) < size_bytes) return 0; + size_t b_iter = 0; // Write program header i.e. the start and count - word_t start = word_byteswap(program.start_address); - *(bytes++) = start; - word_t count = word_byteswap(program.count); - *(bytes++) = count; + convert_word_to_bytes(program.start_address, bytes); + b_iter += WORD_SIZE; + convert_word_to_bytes(program.count, bytes + b_iter); + b_iter += WORD_SIZE; // Write instructions - size_t p_iter = 0, b_iter = PROG_HEADER_SIZE; + size_t p_iter = 0; for (; p_iter < program.count && b_iter < size_bytes; ++p_iter) { size_t written =