View Issue Details

IDProjectCategoryView StatusLast Update
0033446FPCDatabasepublic2018-03-17 12:54
ReporterwpAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version3.0.4Product Build 
Target Version3.2.0Fixed in Version3.1.1 
Summary0033446: Empty MemDataset crashing if FileName specified too early and file does not exist yet.
DescriptionTMemDatasset sometimes crashes when the filename is known too early and the file does not yet exist:

---------------------------------------
program memdbtest2;

{$mode objfpc}

uses
  classes, sysutils, db,memds;

const
  FILENAME = 'MemDB.dat';
var
  MemDb: TMemDataset;
begin
  // Make sure that data file does not exist.
  if FileExists(FILENAME) then
    DeleteFile(FILENAME);

  MemDb := TMemDataset.Create(nil);
  try
    MemDb.FileName := FILENAME;
    MemDb.FieldDefs.Add('Name', ftString, 20);
    MemDb.CreateTable;
    MemDb.Open; // <-- crash here if MemDb.FileName is set
    MemDb.Append;
    MemDb.Post;
  finally
    MemDb.Free;
  end;
  WriteLn('Done. Press ENTER to quit.');
  ReadLn;
end.
---------------------------------------

This is because the method InternalOpen creates the file stream, FOpenStream, in fmOpenRead mode even if the file does not exist. Checking for existence of the file solves the issue here:

---------------------------------------
procedure TMemDataset.InternalOpen;
begin
  If (FFileName<>'') and FileExists(FFileName) then
    FOpenStream:=TFileStream.Create(FFileName,fmOpenRead);
  ...
--------------------------------------

Note that the obvious alternative to open the file in fmCreate mode crashes in the following InternalInitFieldDefs because it only checks whether the FOpenStream is nil.

TagsNo tags attached.
Fixed in Revision38547
FPCOldBugId
FPCTarget
Attached Files
  • memds.pp.patch (439 bytes)
    Index: fcl-db/src/memds/memds.pp
    ===================================================================
    --- fcl-db/src/memds/memds.pp	(revision 38544)
    +++ fcl-db/src/memds/memds.pp	(working copy)
    @@ -653,7 +653,7 @@
     procedure TMemDataset.InternalOpen;
     
     begin
    -  If (FFileName<>'') then
    +  If (FFileName<>'') and FileExists(FFileName) then
         FOpenStream:=TFileStream.Create(FFileName,fmOpenRead);
       Try
         InternalInitFieldDefs;
    
    memds.pp.patch (439 bytes)

Activities

wp

2018-03-17 10:28

reporter  

memds.pp.patch (439 bytes)
Index: fcl-db/src/memds/memds.pp
===================================================================
--- fcl-db/src/memds/memds.pp	(revision 38544)
+++ fcl-db/src/memds/memds.pp	(working copy)
@@ -653,7 +653,7 @@
 procedure TMemDataset.InternalOpen;
 
 begin
-  If (FFileName<>'') then
+  If (FFileName<>'') and FileExists(FFileName) then
     FOpenStream:=TFileStream.Create(FFileName,fmOpenRead);
   Try
     InternalInitFieldDefs;
memds.pp.patch (439 bytes)

Michael Van Canneyt

2018-03-17 10:44

administrator   ~0107188

Fixed, many thanks for the report and solution!

wp

2018-03-17 12:54

reporter   ~0107192

Thank you for this extremely fast fix.

Issue History

Date Modified Username Field Change
2018-03-17 10:24 wp New Issue
2018-03-17 10:28 wp File Added: memds.pp.patch
2018-03-17 10:44 Michael Van Canneyt Fixed in Revision => 38547
2018-03-17 10:44 Michael Van Canneyt Note Added: 0107188
2018-03-17 10:44 Michael Van Canneyt Status new => resolved
2018-03-17 10:44 Michael Van Canneyt Fixed in Version => 3.1.1
2018-03-17 10:44 Michael Van Canneyt Resolution open => fixed
2018-03-17 10:44 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-03-17 10:44 Michael Van Canneyt Target Version => 3.2.0
2018-03-17 12:54 wp Note Added: 0107192
2018-03-17 12:54 wp Status resolved => closed