BufDataset crashing due to missing Index
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
I 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 information:
The bug does not happen in FPC 3.0.4.
Mantis conversion info:
- Mantis ID: 33448
- Version: 3.1.1
- Fixed in version: 3.1.1
- Fixed in revision: 38549 (#8fdf4b2c)
- Target version: 3.2.0