You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: ON ERROR GOTO to a module-level handler keeps the failing procedure's frame #483
The handler runs with the failing procedure's stack and frame pointer instead of the handler's. On current master (5714d10) this reproduces with the C and GAS64 backends:
backend
optimization
result
gcc (C backend)
-O 0
garbage module-local values
gcc (C backend)
-O 2
SIGSEGV
clang (C backend)
-O 0
garbage module-local values
gas64
-O 0 and -O 2
garbage module-local values
ON LOCAL ERROR GOTO is unaffected because its handler remains in the same procedure.
Cause
fb_ErrorThrow*() returns ctx->handler, then generated code jumps to that address. If the error was raised in a procedure and the handler is at module level, the transfer does not restore the handler frame. The module-level handler reads locals through the wrong frame pointer; with optimized C output the stack can also violate the platform ABI and crash in the next runtime call.
Required direction
A correct nonlocal handler transfer needs to capture the handler context where ON ERROR GOTO installs it and restore it before entering the handler, similar to setjmp/longjmp. That has a RESUME consequence: a transfer that unwinds the failing procedure cannot later resume inside it. A same-procedure handler can retain ordinary RESUME behavior.
Each ON ERROR GOTO activation now gets its own error-handler context backed by
setjmp/longjmp. An error raised in a nested procedure transfers control to the
handler in the procedure that installed it, with that procedure's frame
restored. The reproducer above prints "marker= 4711 counter= 13" with the C
backend (checked with clang on macOS arm64, -e at -O 0 and -O 2). Locals
changed after ON ERROR keep their values under C-backend optimization. I have
not re-run the table above for gas64 or for gcc on Linux.
Semantics worth knowing:
RESUME and RESUME NEXT continue in the procedure where the error occurred, so
they need a handler installed in that procedure (ON LOCAL ERROR GOTO). From a
handler in another procedure they end the program with runtime error 5
(illegal resume), because the failing procedure has already been left.
Enclosing local handlers are restored when a procedure exits.
The existing fb_ErrorThrow* ABI is kept through compatibility wrappers.
The patch is not a drop-in for fbc master, since it touches the rtlib and the
C and assembly backends together. If there is interest I can prepare a version
against master.
Activity
metaneutrons commentedon Nov 4, 2026
This is fixed in freebasic-ng (https://github.com/metaneutrons/freebasic-ng),
in PR #165 (commit 3094c9c18).
Each ON ERROR GOTO activation now gets its own error-handler context backed by
setjmp/longjmp. An error raised in a nested procedure transfers control to the
handler in the procedure that installed it, with that procedure's frame
restored. The reproducer above prints "marker= 4711 counter= 13" with the C
backend (checked with clang on macOS arm64, -e at -O 0 and -O 2). Locals
changed after ON ERROR keep their values under C-backend optimization. I have
not re-run the table above for gas64 or for gcc on Linux.
Semantics worth knowing:
they need a handler installed in that procedure (ON LOCAL ERROR GOTO). From a
handler in another procedure they end the program with runtime error 5
(illegal resume), because the failing procedure has already been left.
The patch is not a drop-in for fbc master, since it touches the rtlib and the
C and assembly backends together. If there is interest I can prepare a version
against master.