View Issue Details

IDProjectCategoryView StatusLast Update
0033448FPCDatabasepublic2018-03-17 22:24
ReporterwpAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version3.1.1Product Build 
Target Version3.2.0Fixed in Version3.1.1 
Summary0033448: BufDataset crashing due to missing Index
DescriptionI came across this issue in Lazarus with this sequence of steps:

- Add a TBufDataset to a form
- Add a IndexDef to the dataset by using the IndexDefs' '...' button
- In the object inspector, click on the TBufDataset --> crash with an unspecified access violation.

According to a stack trace the crash happens in TBufDataset.GetIndexName.

From this information I built the following simpler test program:

--------------------------------
program project1;

{$mode objfpc}{$H+}

uses
  Classes, db, bufdataset;

var
  ds: TBufDataset;
begin
  WriteLn('Starting...');
  ds := TBufDataset.Create(nil);
  try
    ds.Name := 'BufDataset1Name';
    ds.IndexDefs.Add('','', []);
    WriteLn(ds.IndexName); // <------------ crash here
  finally
    ds.Free;
  end;
  ReadLn;
end.
----------------------------------

The getter of TBufDataset.IndexName

--------------------------------------------
function TCustomBufDataset.GetIndexName: String;
begin
  if FIndexes.Count>0 then
    result := CurrentIndexBuf.Name
  else
    result := FIndexName;
end;
---------------------------------------------

calls the getter of CurrentIndexBuf

-----------------------------------------
function TCustomBufDataset.GetCurrentIndexBuf: TBufIndex;
begin
  if Assigned(FCurrentIndexDef) then
    Result:=FCurrentIndexDef.BufferIndex
  else
    Result:=Nil;
end;
------------------------------------------

which happens to be nil in this particular situation.
With a check against nil in GetIndexName the crash can be avoided:

-------------------------------------------
function TCustomBufDataset.GetIndexName: String;
begin
  if (FIndexes.Count>0) and Assigned(CurrentIndexBuf) then
    result := CurrentIndexBuf.Name
  else
    result := FIndexName;
end;
------------------------------------------

I am not sure, however, if this fix shadows another error.

Additional InformationThe bug does not happen in FPC 3.0.4.
TagsNo tags attached.
Fixed in Revision38549
FPCOldBugId
FPCTarget
Attached Files
  • bufdataset.pas.patch (404 bytes)
    Index: bufdataset.pas
    ===================================================================
    --- bufdataset.pas	(revision 38547)
    +++ bufdataset.pas	(working copy)
    @@ -3028,7 +3028,7 @@
     
     function TCustomBufDataset.GetIndexName: String;
     begin
    -  if FIndexes.Count>0 then
    +  if (FIndexes.Count>0) and (CurrentIndexBuf <> nil) then
         result := CurrentIndexBuf.Name
       else
         result := FIndexName;
    
    bufdataset.pas.patch (404 bytes)

Activities

wp

2018-03-17 15:50

reporter  

bufdataset.pas.patch (404 bytes)
Index: bufdataset.pas
===================================================================
--- bufdataset.pas	(revision 38547)
+++ bufdataset.pas	(working copy)
@@ -3028,7 +3028,7 @@
 
 function TCustomBufDataset.GetIndexName: String;
 begin
-  if FIndexes.Count>0 then
+  if (FIndexes.Count>0) and (CurrentIndexBuf <> nil) then
     result := CurrentIndexBuf.Name
   else
     result := FIndexName;
bufdataset.pas.patch (404 bytes)

Michael Van Canneyt

2018-03-17 16:11

administrator   ~0107200

Consequence of a refactoring of the indexes. Thanks for the patch!

wp

2018-03-17 22:24

reporter   ~0107218

Thanks

Issue History

Date Modified Username Field Change
2018-03-17 15:50 wp New Issue
2018-03-17 15:50 wp File Added: bufdataset.pas.patch
2018-03-17 16:07 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-03-17 16:07 Michael Van Canneyt Status new => assigned
2018-03-17 16:11 Michael Van Canneyt Fixed in Revision => 38549
2018-03-17 16:11 Michael Van Canneyt Note Added: 0107200
2018-03-17 16:11 Michael Van Canneyt Status assigned => resolved
2018-03-17 16:11 Michael Van Canneyt Fixed in Version => 3.1.1
2018-03-17 16:11 Michael Van Canneyt Resolution open => fixed
2018-03-17 16:11 Michael Van Canneyt Target Version => 3.2.0
2018-03-17 22:24 wp Note Added: 0107218
2018-03-17 22:24 wp Status resolved => closed