Fixed bytecode quotation processing (incorrect number of items).
h3rald h3rald@h3rald.com
Thu, 09 Jan 2025 09:01:15 +0100
2 files changed,
46 insertions(+),
2 deletions(-)
M
scripts/test.hex
→
scripts/test.hex
@@ -237,6 +237,21 @@ _str "test1.txt" write
"test1.txt" read _str == "_str" # "rm test1.txt" exec pop ) + (("a" not) (error) try "[symbol not] Integer required" ==) + ((0x1 () xor) (error) try "[symbol xor] Two integers required" ==) + ((() "aaa" cat) (error) try "[symbol cat] Two quotations or two strings required" ==) + ;124 + + ((0x4 () cat) (error) try "[symbol cat] Two quotations or two strings required" ==) + ((0x4 len) (error) try "[symbol len] Quotation or string required" ==) + ((0x1 0x4 index) (error) try "[symbol index] Quotation or string required" ==) + ((("a" 0x2 "b") "-" join) (error) try "[symbol join] Quotation must contain only strings" ==) + ;128 + + ((("a" "b" "c") 0x1 join) (error) try "[symbol join] Quotation and string required" ==) + (("a" 0x1 split) (error) try "[symbol split] Two strings required" ==) + + ;(() (error) try "" ==) ) "tests" :
M
src/vm.c
→
src/vm.c
@@ -481,6 +481,7 @@ size_t n_items = 0;
int shift = 0; // Decode the variable-length integer for the number of items + do { if (*size == 0)@@ -488,11 +489,39 @@ {
hex_error(ctx, "[interpret bytecode quotation] Bytecode size too small to contain a quotation length"); return 1; } - n_items |= ((**bytecode & 0x7F) << shift); + + // Extract the current byte + uint8_t current_byte = **bytecode; + + // Add the lower 7 bits of the current byte to the result + n_items |= ((current_byte & 0x7F) << shift); + + // Check if MSB is set (continuation flag) + if ((current_byte & 0x80) == 0) + { + // Last byte of the integer; decoding complete + break; + } + + // Update shift for the next byte shift += 7; + + // Prevent overflow for excessively large shifts + if (shift >= 32) + { + hex_error(ctx, "[interpret bytecode quotation] Shift overflow while decoding variable-length integer"); + return 1; + } + + // Move to the next byte (*bytecode)++; (*size)--; - } while (**bytecode & 0x80); + + } while (1); // Loop until the break condition is met + + // Move to the next byte after the loop + (*bytecode)++; + (*size)--; hex_debug(ctx, ">> PUSHQT[03]: <start> (items: %zu)", n_items);