summary_fix-fileio-null.md 1.3 KB

Fix — file I/O NULL-handle crash (Showcase11 SIGSEGV)

Symptom

Showcase11 appeared to segfault "when entering an integer". The crash was actually in the Files section that runs just after: the program writes/reads gen_ssa/_showcase11.txt relative to the current directory. Run from anywhere lacking gen_ssa/ (repo root, or from inside gen_ssa/), Files.OpenWrite returns Modula-2 NIL (a C FILE* of 0), and the shim then called fputc(NULL) / getc(NULL) → SIGSEGV (exit 139).

Fix

runtime/syslib/shim.c: every file routine now treats handle 0 as a safe no-op / returns 0 — m2fgetc, m2fputc, m2fputs, m2freadint, m2fwriteint, m2fwriteln, m2fread, m2fwriteintw (m2fclose was already guarded). A failed open now degrades gracefully instead of crashing.

Result

  • Run from compiler/: Showcase11 unchanged (exit 80).
  • Run elsewhere: no crash (prints 0 for the unreadable file, exit 38).
  • New regression test compiler/tests/t_badfile.mod: opens a path that cannot exist, then does WriteChar/WriteInt/ReadInt/ Close on the NIL handle; expects exit 42. Registered as TBadFile in run_tests.sh.

Suite 107/107.

Files

runtime/syslib/shim.c, compiler/tests/t_badfile.mod, compiler/run_tests.sh.