fpspreadsheet: Fix formula parser crashing with an R1C1 formula subtracting cells with a negative relative address.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7051 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2019-07-18 21:02:44 +00:00
parent 3b0e46b92c
commit 8e50105ca0
4 changed files with 21 additions and 10 deletions

View File

@ -1127,26 +1127,38 @@ begin
end;
function TsExpressionScanner.DoIdentifier: TsTokenType;
var
isInSqBr: Boolean;
isQuoted: Boolean;
function IsR1C1Char(C: Char): Boolean; inline;
begin
Result := (FParser.Dialect = fdExcelR1C1) and (C in ['[', ']', '-']);
if (FParser.Dialect = fdExcelR1C1) then
Result := (C = '[') or (C = ']') or (isInSqBr and (C = '-'))
else
Result := false;
end;
var
C: Char;
S: String;
isQuoted: Boolean;
ok: Boolean;
begin
C := CurrentChar;
isQuoted := C = '''';
isInSqBr := C = '[';
while ((not IsWordDelim(C)) or IsQuoted or IsR1C1Char(C)) and (C <> cNULL) do
begin
FToken := FToken + C;
C := NextPos;
if C = '''' then isQuoted := false;
if (FParser.Dialect = fdExcelR1C1) then begin
if (C = '[') then
isInSqBr := true
else if (C = ']') then
isInSqBr := false;
end;
end;
if (FParser.Dialect = fdExcelR1C1) then begin

View File

@ -5876,9 +5876,6 @@ begin
exit;
end;
formula := FFormulas.AddFormula(ACell^.Row, ACell^.Col, AFormula);
// wp: Why is this created when boIgnoreFormulas is active?
if not (boIgnoreFormulas in Workbook.Options) then
begin
// Remove '='; is not stored internally
@ -5895,6 +5892,8 @@ begin
else
parser.Expression := AFormula;
AFormula := parser.Expression;
formula := FFormulas.AddFormula(ACell^.Row, ACell^.Col, AFormula);
except
on E:Exception do begin
if FWorkbook.FReadWriteFlag = rwfNormal then
@ -5904,7 +5903,7 @@ begin
FName, GetCellString(ACell^.Row, ACell^.Col), E.Message]
);
parser.Free;
FFormulas.DeleteFormula(ACell^.Row, ACell^.Col);
//FFormulas.DeleteFormula(ACell^.Row, ACell^.Col);
exit;
end;
end;

View File

@ -1087,7 +1087,7 @@ begin
s := GetAttrValue(ANode, 'ss:Protected');
if s ='1' then
AWorksheet.Options := AWorksheet.Options + [soProtected];
;
ANode := ANode.FirstChild;
while ANode <> nil do begin
nodeName := ANode.NodeName;

View File

@ -89,7 +89,7 @@ type
procedure Test_Write_Read_Calc3DFormula_BIFF5;
procedure Test_Write_Read_Calc3DFormula_BIFF8;
procedure Test_Write_Read_Calc3DFormula_OOXML;
procedure Test_Write_Read_Calc3DFormula_XML;
// procedure Test_Write_Read_Calc3DFormula_XML;
procedure Test_Write_Read_Calc3DFormula_ODS;
{ Overwrite formula with other content }
@ -887,11 +887,11 @@ procedure TSpreadWriteReadFormulaTests.Test_Write_Read_Calc3DFormula_OOXML;
begin
Test_Write_Read_Calc3DFormulas(sfOOXML);
end;
{
procedure TSpreadWriteReadFormulaTests.Test_Write_Read_Calc3DFormula_XML;
begin
Test_Write_Read_Calc3DFormulas(sfExcelXML);
end;
end; }
procedure TSpreadWriteReadFormulaTests.Test_Write_Read_Calc3DFormula_ODS;
begin