Bug #22
closedHostFiles.NewWriter violating the specification
70%
Description
According to the specification (System/Docu/Files) in case of read-only files (shared) NewWriter should return NIL. The current implementation, however, generates a TRAP. The specification needs to be aligned with the implementation. In order to be able to avoid the TRAP, two new File methods (Shared() and Closed()) should be introduced that return the file's current state.
Reported by Ilya Ermakov, 2009-10-12.
Updated by I. Denisov almost 11 years ago
- Status changed from In Progress to Closed
Updated by J. Templ over 9 years ago
- Description updated (diff)
PROCEDURE (f: File) NewWriter (old: Writer): Writer
NEW, ABSTRACT
Returns a writer which has the appropriate type (for this file type). If old = NIL, then a new writer is allocated. If old # NIL and old has the appropriate type, old is returned. Otherwise, a new writer is allocated. The returned writer is connected to f, and its position is somewhere on the file. If an old writer is passed as parameter, the old position will be retained if possible.
If an old writer is passed as parameter, it is the application's responsibility to guarantee that it is not in use anymore. Passing an unused old writer is recommended because it avoids unnecessary allocations.
[b]Read-only files allow no writers at all. In such cases, NewWriter returns NIL.[/b]Post
result # NIL
old # NIL & old.Base() = f
result.Pos() = old.Pos()
old = NIL OR old.Base() # f
result.Pos() = f.Length()
result = NIL
read-only file
However the code of HostFiles is next:
PROCEDURE (f: File) NewWriter (old: Files.Writer): Files.Writer;
VAR w: Writer;
BEGIN (* portable *)
ASSERT(f.state # closed, 20); ASSERT(f.state # shared, 21);
IF (old # NIL) & (old IS Writer) THEN w := old(Writer) ELSE NEW(w) END;
IF w.base # f THEN
w.base := f; w.buf := NIL; w.SetPos(f.len)
END;
RETURN w
END NewWriter;
ASSERT; means TRAP except RETURN NIL.