Skip to content

Commit 0432f6c

Browse files
committed
Added some tests and error checking for return stack over and underflow.
1 parent 4263074 commit 0432f6c

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

include/ltconst.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ extern const size_t EXIT_POB;
3636
extern const size_t EXIT_OP;
3737
extern const size_t EXIT_STR;
3838
extern const size_t EXIT_ARGS;
39+
extern const size_t EXIT_RSOF;
40+
extern const size_t EXIT_RSUF;
3941

4042
#endif

src/ltconst.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ const size_t EXIT_POB = 6;
4242
const size_t EXIT_OP = 7;
4343
const size_t EXIT_STR = 8;
4444
const size_t EXIT_ARGS = 9;
45+
const size_t EXIT_RSOF = 10;
46+
const size_t EXIT_RSUF = 11;
4547

src/ltrun.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ size_t execute(WORD* memory, size_t length, WORD* data_stack, WORD* return_stack
2727
"Error: program counter out of bounds, pc: %hx, bfp: %hd\n",
2828
pc, bfp);
2929
exit (EXIT_POB);
30-
} else if (dsp > 0x8000) { // i.e. it has wrapped around into negative numbers
30+
} else if (dsp > 0x8000) { // i.e. it has wrapped around into negatives
3131
fprintf(stderr, "Error: stack underflow, sp: %hx or %hd\n", dsp, dsp);
3232
exit (EXIT_SUF);
3333
} else if (dsp > END_STACK) {
3434
fprintf(stderr, "Error: stack overflow, sp: %hx or %hd\n", dsp, dsp);
3535
exit (EXIT_SOF);
36+
} else if (rsp > 0x8000) { // i.e. it has wrapped around into negatives
37+
fprintf(stderr, "Error: return stack underflow, sp: %hx or %hd\n",
38+
rsp, rsp);
39+
exit (EXIT_RSUF);
40+
} else if (rsp > END_RETURN) {
41+
fprintf(stderr, "Error: return stack overflow, sp: %hx or %hd\n",
42+
rsp, rsp);
43+
exit (EXIT_RSOF);
3644
}
3745

3846
switch (memory[pc] & 0xff) {

test/test_errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
"01 00 aa aa 3D 00 00 00",
2424
vmtest.EXIT_POB
2525
),
26+
Test(
27+
"Return stack overflow",
28+
"01 00 aa aa 0A 00 01 00 00 00 3D 00 00 00",
29+
vmtest.EXIT_RSOF
30+
),
31+
Test(
32+
"Return stack underflow",
33+
"0B 00 00 00",
34+
vmtest.EXIT_RSUF
35+
),
2636
]
2737

2838

test/vmtest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
EXIT_SUF = 5;
1313
EXIT_POB = 6;
1414
EXIT_OP = 7;
15+
EXIT_STR = 8;
16+
EXIT_ARGS = 9;
17+
EXIT_RSOF = 10;
18+
EXIT_RSUF = 11;
1519

1620
### Test Classes ###
1721
class VmTests(TestSuite):

0 commit comments

Comments
 (0)