The Delphi Bug List

Entry No.
674
RTL - Sys - System
If you use Seek in a file larger than the range of an Integer (2147483647), an I/O error is generated.
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
N/AExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsUnknown
Description
Reported by Doeke de Vries; checked by Jordan Russell

Jordan Russell:
Here is some code to reproduce the problem.

var
  F: File;
begin
  AssignFile(F, 'BigFile');   // a file larger than 2GB
  Reset(F, 1000000);
  Seek(F, 2148);              // I/O error 131 here
  CloseFile(F);
end;

This problem should be easy for Borland to fix. The Seek procedure does use a 64-bit multiply instruction to multiply the record number and record size together, but it discards the high-order 32 bits and passes NULL for the lpDistanceToMoveHigh parameter of the SetFilePointer call. This causes the error. In addition to properly passing the high-order 32 bits, it needs to use GetLastError for error checking since a FFFFFFFF result may not mean failure when a 64-bit offset is used.

Here's how I think the middle part of Delphi 6's _Seek procedure (in System.pas) should be written:

//  SetFilePointer(f.Handle, recNum*f.RecSize, FILE_BEGIN
        MOV      EAX,[ECX].TFileRec.RecSize
        MUL      EDX
        PUSH     EDX
        MOV      EDX,ESP
        PUSH     FILE_BEGIN    // pass dwMoveMethod
        PUSH     EDX           // pass lpDistanceToMoveHigh
        PUSH     EAX           // pass lDistanceToMove
        PUSH     [ECX].TFileRec.Handle   // pass hFile
        CALL     SetFilePointer          // get current position
        POP      EDX
        INC      EAX
        JNZ      @@exit
        CALL     GetLastError
        TEST     EAX,EAX
        JZ       @@exit
        JMP      InOutError
Note: The FilePos function also has a problem with >2GB offsets.
Solution / workaround
You can seek past 2GB by calling the SetFilePointer (Win32) function instead.
Latest update of this entry: 2002-04-07

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.