Access Violation in MaskUtils.FormatMaskText lazarus / 21-General ---------------------------------------------------------------------- dmitryb · 2022-10-04 03:20 Hello Lazarus 2.2.2 (rev lazarus_2_2_2-0-g537f43754c) FPC 3.2.2 x86_64-win64-win32/win64 When I call the next code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  EditMask: String;  Result: String;begin  EditMask := '#-###-###-##-##;0;_';  Result := MaskUtils.FormatMaskText(EditMask, '');end; I got an Access Violation in function TMaskUtils.ApplyMaskToText(AValue: String): String; I can't figure out if this is a feature or a bug. ---------------------------------------------------------------------- dje · 2022-10-04 05:15 Looks like a bug to me around line 791 (MaskUtils.pas) Code in question is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       //fill text from left to rigth, skipping MaskLiterals      j := 1;    for i := 1 to FMaskLength do      begin        if not IsLiteral(FMask[i]) then        begin          if (AValue[j] = #32) then S[i]:= FSpaceChar else S[i] := AValue[j];  // <<< ERROR HERE        Inc(j);          if j > Length(AValue) then Break;        end;    end; There is no check to see if AValue has zero length. The only check comes after Inc(J), which then breaks out the loop. There is some code below this that also doesn't check AValue for an EmptyStr. Adding a length check fixes the issue. (Near the start of the function TMaskUtils.ApplyMaskToText) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for I := 1 To FMaskLength do  S := S + ClearChar(I);  if Length(AValue) > 0 then // <<<< New Code here  if FMaskSave then ---------------------------------------------------------------------- Bart · 2022-10-04 08:14 FWIW: the similar function in Lazarus' MasEdit unit returns " -   -   -  -  " (without the quotes of course). And that is because GetCodePoint() returns '' if index is out of bounds, so it kind of works by accident there. Bart ---------------------------------------------------------------------- Bart · 2022-10-04 09:00 Reported and attached a patch in the bugtracker. Bart ---------------------------------------------------------------------- Bart · 2022-10-04 11:28 And applied in #e736011c. Bart ---------------------------------------------------------------------- Josh · 2022-10-04 11:39 @Bart Just looked at diff code. Question Quote else   begin//FMaskSave = False     if FTrimType = metTrimRight then     if Value<>'' then     begin       //fill text from left to rigth, skipping MaskLiterals       j := 1;       for i := 1 to FMaskLength do Should that be AValue not Value Also would the result be set? Maybe Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  If AValue=EmptyStr then Exit(EmptyStr); ---------------------------------------------------------------------- Bart · 2022-10-04 15:54 Quote from: Josh on October 04, 2022, 01:39:33 pmJust looked at diff code. Question Should that be AValue not Value Well spotted. I sent in a fix for the fix to the bugtracker, hope it gets applied soon. Quote from: Josh on October 04, 2022, 01:39:33 pmAlso would the result be set? Maybe Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  If AValue=EmptyStr then Exit(EmptyStr);I didn't think so. MaskUtils is a bit odd. Delphi uses it to do stuff with TMaskEdit. When I came to Lazarus, TMaskEdit was utterly broken. The stuff in MaskUtils could not be applied to TMaskEdit. Hence over time I fixed TMaskEdit on Lazarus side. The release cycle of fpc is too slow to wait for that (and at the time I did not feel comfortable to suggest any patches to the fpc team). At that time TMaskEdit did not support UTF-8, it only worked with (lower) ASCII characters. Since some years now, it does work with UTF-8 (well, it might choke on surrogate pairs). So, after a while fpc's MaskUtils implementation behaved rather different than similar functions in TMaskEdit (the latter being heavily "black-box" tested against Delphi's behaviour). Once noticed, I rewrote large parts of the MaskUtils unit, so that it would act like TMaskEdit would. And since FormatMaskText (in Lazarus) calls a TMaskEdit function to accomplish this, and in Lazarus this function must not return an empty sting in this case, it was implemented in MaskUtils in the same way. "Backporting" this to fpc meant that I had to change all the UTF-8 related code back to directly accessing strings by index. (You cannot use MaskUtils with UTF-8 encoded strings.) And that's where the bug was introduced. In the mean time I have safeguarded this particular bug in Lazarus as well, since it should not depend on an implementation detail of a helper function. Bart ---------------------------------------------------------------------- Bart · 2022-10-04 15:56 OK, the fix-for-the-fix has been applied as well. Bart ---------------------------------------------------------------------- dmitryb · 2022-12-23 18:24 In the lazarus Lazarus 2.2.4 Lazarus 2.2.4 (rev lazarus_2_2_4-0-g06a78ed923) FPC 3.2.2 x86_64-win64-win32/win64 The same error occurs. Will it be fixed? ---------------------------------------------------------------------- Bart · 2022-12-23 22:22 The bug was in fpc and was fixed in fpc. So either use fpc main of fpc fixes branch (I hope it got merged). Bart