View Issue Details

IDProjectCategoryView StatusLast Update
0009055FPCRTLpublic2008-04-13 10:07
ReporterAlex Zolotov Assigned ToMichael Van Canneyt  
PrioritynormalSeveritymajorReproducibilityalways
Status closedResolutionfixed 
Product Version2.3.1 
Fixed in Version2.2.2 
Summary0009055: SeekEOF does not work with streamio from FCL
DescriptionSeekEOF does not work with streamio because it assume that TextFile is real text file, not arbitrary stream and directly uses
TextRec(t).handle
Do_IsDevice
Do_FilePos
do_seek
etc..

see attached file. In this example we cannot (and should not) replace SeekEOF to Seek, because it raise exception in last extra empty lines
TagsNo tags attached.
Fixed in Revision
FPCOldBugId0
FPCTarget-
Attached Files

Activities

2007-06-11 12:56

 

test.pas (590 bytes)

Jonas Maebe

2007-06-11 13:07

manager   ~0013123

How would you propose to fix this? I think the only solution is to state that SeekEOF is unsupported for anything but regular files, and that while this routine makes a best effort to guarantee against wrong usage and will do so successfully in case you limit yourself to the system unit's built-in textfile support, there is no failsafe way to do so if you start using custom textfile implementations.

Alex Zolotov

2007-06-18 09:43

reporter   ~0013240

Last edited: 2007-06-18 09:55

At first, we need correct AssignStream to add FillChar(F, SizeOf(TextRec), 0) (see attached patch)

If we can force Do_IsDevice return TRUE for zero-handle: THandle(0), then it will work

Or we can just correct one line in SeekEof:

replace
   isdevice := Do_IsDevice(TextRec(t).handle);
by
   isdevice := (TextRec(t).handle = 0) or Do_IsDevice(TextRec(t).handle);

(see patch)

PS: may be it will be merged to fixes_2_2...

2007-06-18 09:44

 

patch_seekeof (1,901 bytes)   
Index: rtl/inc/text.inc
===================================================================
--- rtl/inc/text.inc	(revision 7719)
+++ rtl/inc/text.inc	(working copy)
@@ -291,7 +291,7 @@
   oldbufend := TextRec(t).BufEnd;
   reads := 0;
   oldfilepos := -1;
-  isdevice := Do_IsDevice(TextRec(t).handle);
+  isdevice := (TextRec(t).handle = 0) or Do_IsDevice(TextRec(t).handle);
   repeat
     If TextRec(t).BufPos>=TextRec(t).BufEnd Then
      begin
Index: packages/fcl-base/src/inc/streamio.pp
===================================================================
--- packages/fcl-base/src/inc/streamio.pp	(revision 7719)
+++ packages/fcl-base/src/inc/streamio.pp	(working copy)
@@ -110,33 +110,31 @@
   ---------------------------------------------------------------------}
 
 
-Procedure AssignStream(var F: Textfile; Stream : TStream);
-
-Var
+procedure AssignStream(var F: Textfile; Stream : TStream);
+var
   E : EInoutError;
-
 begin
-  if (Stream=Nil) then
-    begin
-    E:=EInOutError.Create(SErrNilStream);
-    E.ErrorCode:=6;
+  if (Stream = nil) then
+  begin
+    E := EInOutError.Create(SErrNilStream);
+    E.ErrorCode := 6;
     Raise E;
-    end;
+  end; // if
+  FillChar(F, SizeOf(TextRec), 0);
   with TTextRec(F) do
-    begin
-    OpenFunc:=@StreamOpen;
-    CloseFunc:=@StreamClose;
- Case DefaultTextLineBreakStyle Of
+  begin
+    OpenFunc := @StreamOpen;
+    CloseFunc := @StreamClose;
+    PStream(@UserData)^ := Stream;
+    Mode := fmClosed;
+    BufSize := SizeOf(Buffer);
+    BufPtr := @Buffer;
+    case DefaultTextLineBreakStyle Of
     tlbsLF: TextRec(f).LineEnd := #10;
     tlbsCRLF: TextRec(f).LineEnd := #13#10;
     tlbsCR: TextRec(f).LineEnd := #13;
-  End;
-    PStream(@UserData)^:=Stream;
-    Mode:=fmClosed;
-    BufSize:=SizeOf(Buffer);
-    BufPtr:=@Buffer;
-    Name[0]:=#0;
-    end;
+    end; // case
+  end; // with TTextRec(F)
 end;
 
 
patch_seekeof (1,901 bytes)   

Michael Van Canneyt

2007-06-18 09:52

administrator   ~0013241

The do_isdevice is wrong, you cannot do that.
File descriptor 0 is not necessarily a device. You cannot know what
kind of file it is.

Alex Zolotov

2007-06-18 10:02

reporter   ~0013242

Yes, zero-handle is not device... and it was bad idea to touch Do_IsDevice, and I propose another solution - patch seekeof and AssignStream only..

I can conjecture what zero-handle is used on all platforms to mark that there are no device/file/object... the same as NIL-pointer...

Michael Van Canneyt

2007-12-30 21:56

administrator   ~0016972

Since there is no way to decently cover all cases, I added a note to the documentation of seekeof which says that it can lead to weird results when used on not regular files

Issue History

Date Modified Username Field Change
2007-06-11 12:56 Alex Zolotov New Issue
2007-06-11 12:56 Alex Zolotov File Added: test.pas
2007-06-11 13:07 Jonas Maebe Note Added: 0013123
2007-06-15 15:45 Marco van de Voort FPCTarget => -
2007-06-15 15:45 Marco van de Voort Status new => feedback
2007-06-18 09:43 Alex Zolotov Note Added: 0013240
2007-06-18 09:44 Alex Zolotov File Added: patch_seekeof
2007-06-18 09:52 Michael Van Canneyt Note Added: 0013241
2007-06-18 09:55 Alex Zolotov Note Edited: 0013240
2007-06-18 10:02 Alex Zolotov Note Added: 0013242
2007-10-21 21:15 Florian Status feedback => assigned
2007-10-21 21:15 Florian Assigned To => Michael Van Canneyt
2007-12-30 21:56 Michael Van Canneyt Status assigned => resolved
2007-12-30 21:56 Michael Van Canneyt Resolution open => fixed
2007-12-30 21:56 Michael Van Canneyt Note Added: 0016972
2008-04-13 10:07 Jonas Maebe FPCOldBugId => 0
2008-04-13 10:07 Jonas Maebe Status resolved => closed
2008-04-13 10:07 Jonas Maebe Fixed in Version => 2.2.1