From 7c2fb70c4a60811e95151c63e132233d82040c8c Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 26 Jun 2022 21:20:23 -0500 Subject: [PATCH] freopen improvements This adds a check for the filename argument being null, in which case it bails out and returns NULL. Previously, it would just dereference the null pointer and treat the contents of memory location 0 as a filename, with unpredictable results. A null filename indicates freopen should try to reopen the same file with a different mode, but the degree of support for that is implementation-defined. We still don't really support it, but at least this will report the failure and avoid unpredictable behavior. It also removes some unused code at the end, but still forces fputc and fgetc to be linked in. These are needed because there are weak references to them in putchar and getchar, which may need to be used if stdin/stdout have been reopened. --- stdio.asm | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/stdio.asm b/stdio.asm index e9e60a8..017adb4 100644 --- a/stdio.asm +++ b/stdio.asm @@ -1079,7 +1079,10 @@ cl1 ldy #FILE_file branch if the file is not a disk file ; ; create a GS/OS file name ; -cn1 ph4 filename get the length of the name buffer +cn1 lda filename bail out if filename is NULL + ora filename+2 (reopening same file not supported) + jeq rt2 + ph4 filename get the length of the name buffer jsl ~osname sta OSname stx OSname+2 @@ -1259,33 +1262,9 @@ crFileType ds 2 cl dc i'1' parameter block for OSclose clRefNum ds 2 ; -; Patch for standard out +; Force inclusion of functions that have weak references elsewhere ; -stdoutFile jmp stdoutPatch - -stdoutPatch phb - plx - ply - pla - pha - pha - pha - phy - phx - plb - lda >stdout - sta 6,S - lda >stdout+2 - sta 8,S - brl fputc -; -; Patch for standard in -; -stdinFile jmp stdinPatch - -stdinPatch ph4 #stdin+4 - jsl fgetc - rtl + dc r'fputc,fgetc' end ****************************************************************