Recent

Author Topic: TSynEdit crashes when loading text  (Read 978 times)

domasz

  • Sr. Member
  • ****
  • Posts: 322
TSynEdit crashes when loading text
« on: June 01, 2023, 08:13:29 pm »
When I click this button twice the program crashes. Might be something with highlighters (TSynMultiSyn, TSynPHPSyn, TSynHTMLSyn).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var Str: String;
  3.     L: TStringList;
  4. begin
  5.   L := TStringList.Create;
  6.   L.LoadFromFile('test.htm');
  7.  
  8.   Str := L.Text;
  9.   L.Free;
  10.  
  11.  SynEdit1.Text:=Str; //crashes
  12. end;
  13.  

Code: Pascal  [Select][+][-]
  1.   object SynMultiSyn1: TSynMultiSyn
  2.     Enabled = False
  3.     Schemes = <    
  4.       item
  5.         StartExpr = '<style>'
  6.         EndExpr = '</style>'
  7.         Highlighter = SynCssSyn1
  8.         MarkerAttri.Background = clNone
  9.         MarkerAttri.Foreground = 16744448
  10.       end        
  11.       item
  12.         StartExpr = '<\?php'
  13.         EndExpr = '\?>'
  14.         Highlighter = SynPHPSyn1
  15.         MarkerAttri.Background = clNone
  16.         MarkerAttri.Foreground = 16512
  17.       end>
  18.     DefaultHighlighter = SynHTMLSyn1
  19.     Left = 592
  20.     Top = 40
  21.   end

Attached is super simple demo.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9383
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit crashes when loading text
« Reply #1 on: June 01, 2023, 08:25:21 pm »
I can reproduce...

It seems something in the Multi-HL. Not sure yet

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9383
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit crashes when loading text
« Reply #2 on: June 01, 2023, 08:30:46 pm »
Ideally report on the bug tracker please

domasz

  • Sr. Member
  • ****
  • Posts: 322
Re: TSynEdit crashes when loading text
« Reply #3 on: June 01, 2023, 08:35:39 pm »
Ideally report on the bug tracker please
Not sure if I did it right. Don't know how to post files there.
https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40292

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9383
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit crashes when loading text
« Reply #4 on: June 01, 2023, 08:40:39 pm »
ok, thanks

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9383
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit crashes when loading text
« Reply #5 on: June 02, 2023, 07:34:05 pm »
A temporary patch. I still need to do some testing.

Code: Diff  [Select][+][-]
  1. diff --git a/components/synedit/synhighlightermulti.pas b/components/synedit/synhighlightermulti.pas
  2. index 1343e5d554..ca662dc37d 100644
  3. --- a/components/synedit/synhighlightermulti.pas
  4. +++ b/components/synedit/synhighlightermulti.pas
  5. @@ -845,22 +845,22 @@ procedure TSynHLightMultiVirtualLines.RealLinesDeleted(AIndex, ACount: Integer);
  6.      // Real-lines starting in the middle of the Section
  7.      CountInSection := Min(AIndex + ACount, p^.EndPos.y + 1) - AIndex;
  8.      FirstVLine := p^.VirtualLine + AIndex - p^.StartPos.y;
  9. -    PrevEndVLine := p^.VirtualLine + p^.EndPos.y - p^.EndPos.y;
  10. +    PrevEndVLine := p^.VirtualLine + p^.EndPos.y - p^.StartPos.y;
  11.      p^.EndPos.y := p^.EndPos.y - CountInSection;
  12.      inc(i);
  13.      if i = FSectionList.Count then begin
  14.        DelVLines;
  15.        exit;
  16.      end;
  17.      p := FSectionList.PSections[i];
  18.     VLineCount := CountInSection;
  19.    end;
  20.    while p^.EndPos.y < AIndex + ACount do begin
  21.      // Completly delete node (All Real lines deleted)
  22.      VLineCount := VLineCount + p^.EndPos.y - p^.StartPos.y + 1;
  23.      if PrevEndVLine = p^.VirtualLine then
  24.        dec(VLineCount);
  25. -    PrevEndVLine := p^.VirtualLine + p^.EndPos.y - p^.EndPos.y;
  26. +    PrevEndVLine := p^.VirtualLine + p^.EndPos.y - p^.StartPos.y;
  27.      FSectionList.Delete(i);
  28.      if i = FSectionList.Count then begin
  29.        DelVLines;
  30.  
  31.  

domasz

  • Sr. Member
  • ****
  • Posts: 322
Re: TSynEdit crashes when loading text
« Reply #6 on: June 02, 2023, 08:46:13 pm »
Seems to work great, thank you for such a quick fix!

I tried it with hundreds of files from real websites and I found no problems.
« Last Edit: June 02, 2023, 09:55:33 pm by domasz »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9383
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit crashes when loading text
« Reply #7 on: June 03, 2023, 09:37:47 pm »
Found a few other issues too. For patch, see the commit mentioned in the reported issue.

 

TinyPortal © 2005-2018