Stop an armed TRAP from swallowing parse errors whole
A program with TRAP set and a line that would not parse printed nothing at all and exited zero. No error line, because akbasic_runtime_error() intercepts for the trap and deliberately prints nothing; and no handler, because the parse branch of process_line_run() then set run_finished_mode regardless -- QUIT for a file -- so the next line boundary the handler would have been entered at never came. Arming an error handler made errors disappear. The guard is to set the finished mode only when the error was actually reported. The runtime path was always right: report_and_reraise() never touched the mode. The same branch also never recorded the status, so the handler that now runs was handed ER# 0. It sets lasterrorstatus from the context the way report_and_reraise() does, and a trapped SCALE with no arguments reports 512. Found while writing the error-code appendix -- documenting what ER# can hold means provoking each code, and this is what happened on the way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,6 +53,47 @@ static void test_trap_catches(void)
|
||||
harness_stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief A *parse* error is trappable too, and carries its code into ER#.
|
||||
*
|
||||
* Found while writing the error-code appendix, and it was the worst kind of
|
||||
* defect: arming a handler made errors disappear. `akbasic_runtime_error()`
|
||||
* intercepts for the trap and leaves `errclass` clear, but the parse branch of
|
||||
* `process_line_run()` set `run_finished_mode` regardless -- QUIT for a file --
|
||||
* so the run ended before the next line boundary the handler would have been
|
||||
* entered at. No error line, because the trap suppressed it; no handler, because
|
||||
* the program had stopped. Nothing on stdout at all.
|
||||
*
|
||||
* The second half is that the same branch never recorded the status, so the
|
||||
* handler that now runs used to be handed `ER# 0`.
|
||||
*/
|
||||
static void test_trap_catches_parse_error(void)
|
||||
{
|
||||
TEST_REQUIRE_OK(run_program("10 TRAP 100\n"
|
||||
"20 SCALE\n"
|
||||
"30 PRINT \"AFTER\"\n"
|
||||
"40 END\n"
|
||||
"100 PRINT \"TRAPPED \" + ER# + \" ON LINE \" + EL#\n"
|
||||
"110 RESUME NEXT\n"));
|
||||
/* 512 is AKBASIC_ERR_SYNTAX, the base of the band. Spelled out for the same
|
||||
reason test_trap_catches() spells out 515. */
|
||||
TEST_REQUIRE_STR(HARNESS_OUTPUT, "TRAPPED 512 ON LINE 20\nAFTER\n");
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "PARSE ERROR") == NULL,
|
||||
"a trapped parse error must not also be reported, got \"%s\"",
|
||||
HARNESS_OUTPUT);
|
||||
harness_stop();
|
||||
|
||||
/* And with no trap it still reports and still stops -- the half of the
|
||||
behaviour that was never broken and must not become so. */
|
||||
TEST_REQUIRE_OK(run_program("10 SCALE\n"
|
||||
"20 PRINT \"AFTER\"\n"));
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "? 10 : PARSE ERROR") != NULL,
|
||||
"an untrapped parse error should be reported, got \"%s\"", HARNESS_OUTPUT);
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "AFTER") == NULL,
|
||||
"an untrapped parse error should stop the run, got \"%s\"", HARNESS_OUTPUT);
|
||||
harness_stop();
|
||||
}
|
||||
|
||||
/** @brief With no TRAP the error is reported and the program stops, as before. */
|
||||
static void test_untrapped_still_reports(void)
|
||||
{
|
||||
@@ -184,6 +225,7 @@ static void test_err_function(void)
|
||||
int main(void)
|
||||
{
|
||||
test_trap_catches();
|
||||
test_trap_catches_parse_error();
|
||||
test_untrapped_still_reports();
|
||||
test_trap_disarms();
|
||||
test_trap_by_label();
|
||||
|
||||
Reference in New Issue
Block a user