From e4f6c8746d67dc5a5382c41b2824050d75860ddf Mon Sep 17 00:00:00 2001 From: slederer Date: Fri, 22 Aug 2025 02:20:21 +0200 Subject: [PATCH] stdlib: fix memory leak on file errors stdlib: throw runtime error when reading invalid real number tdrimg: add another demo image --- lib/stdlib.pas | 38 +++++++++++++++++++++++++++++--------- utils/tdrimg.py | 1 + 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/stdlib.pas b/lib/stdlib.pas index 8ad45d9..eb7690d 100644 --- a/lib/stdlib.pas +++ b/lib/stdlib.pas @@ -1186,12 +1186,17 @@ ext: code := i - 1; end; +procedure errorhalt(var fil:file); +begin + RuntimeError(ioerrordesc[fil.lastError]); +end; + procedure checkerror(var fil:file); begin if fil.lastError <> 0 then begin if not fil.errorAck then - RuntimeError(ioerrordesc[fil.lastError]) + errorhalt(fil) else begin fil.lastError := 0; @@ -1339,7 +1344,10 @@ begin if f.lastError = 0 then val(buf, v, errpos); if errpos <> 0 then + begin fileerror(f, IOInvalidFormat); + checkerror(f); + end; end; procedure freadstring(var s:string; var f:file); @@ -1665,12 +1673,21 @@ begin { writeln(' readbuf data: ', fil.buffer^[0][0]); } end; +procedure close(var aFile:file); forward; + +(* Set error state on file and close it. + Buffer will not be flushed as that might + have caused the error. +*) procedure fileerror(var fil:file; error:integer); begin - (* should check if there was an error already - and throw a runtime error in that case *) fil.lastError := error; fil.errorAck := false; + if fil.buffer <> nil then + begin + fil.needsflush := false; + close(fil); + end; end; function IOResult(var fil:file):integer; @@ -2052,19 +2069,22 @@ procedure close(var aFile:file); begin if aFile.typ = IODiskFile then begin + if aFile.lastError = IOFileClosed then + errorhalt(aFile); { writeln('close needsflush:', aFile.needsflush, ' changed:', aFile.changed, ' error:', aFile.lastError); } if aFile.needsflush then flushfile(aFile); + + { writeln('close f.buffer:', aFile.buffer); } + dispose(aFile.buffer); + aFile.buffer := nil; + if aFile.lastError = 0 then begin - fileerror(aFile, IOFileClosed); - { writeln('close f.buffer:', aFile.buffer); } - dispose(aFile.buffer); - aFile.buffer := nil; - if aFile.changed then updatedirslot(aFile); - + if aFile.lastError = 0 then + fileerror(aFile, IOFileClosed); end; closevolumeid(aFile.volumeid); diff --git a/utils/tdrimg.py b/utils/tdrimg.py index 6061a98..be93ed9 100644 --- a/utils/tdrimg.py +++ b/utils/tdrimg.py @@ -604,6 +604,7 @@ def create_image_with_stuff(imgfile): slotnr = putfile("../examples/Toco_Toucan.pict", None , f, part, partstart, slotnr) slotnr = putfile("../examples/shinkansen.pict", None , f, part, partstart, slotnr) slotnr = putfile("../examples/snow_leopard.pict", None , f, part, partstart, slotnr) + slotnr = putfile("../examples/ADDS-Envoy-620.pict", None , f, part, partstart, slotnr) slotnr = putfile("../examples/benchmarks.pas", None , f, part, partstart, slotnr)