SOLVED: SIGSEV when include a custom type if lazarus / 21-General ---------------------------------------------------------------------- torbente · 2021-01-08 00:03 Hi everyone; I spent all the day trying to figure out how this "twilight zone" situation happens but i finally will ask for help. 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 TMyData= Packed Record  Data1 : string[16];  Data2 : Integer;  end;var  CheckThisData : TMyData; Function MyFuncion():integer;Beginif CheckThisData.Data1 <> '' then // Do somehting if the string is not emptyend; This works perfect. But if i do this: 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 Function MyFuncion():integer;Beginif ((CheckThisData.Data1 <> '') and (CheckThisData.Data2 = 3)) then // Do somehting if the string is not empty and Data2 is equal to a specified numberend;[/code] The execution crashes with a sigsev error. What i tried: - Eliminate this function: the program runs perfect. - Try multiple conditions for data2, example, Data2 <> 0: SIGSEV - Initialize CheckThisData with Default : SIGSEV - Change the funtion to a procedure : SIGSEV Why the program runs perfect with the first conditional only, but crashes if i try to check 2 conditionals? This is BY FAR the most incredible situation i ever seen usig laz/fp ---------------------------------------------------------------------- jamie · 2021-01-08 00:08 maybe you forgot it in the example but I see no "END" on your record define ---------------------------------------------------------------------- torbente · 2021-01-08 00:13 Quote from: jamie on January 08, 2021, 01:08:41 ammaybe you forgot it in the example but I see no "END" on your record define The define record have it end, i forgot to type it in the post. Actually, i deleted it and the program actually do not compile. ---------------------------------------------------------------------- jamie · 2021-01-08 00:44 you need to tell us the compiler version, OS target etc ---------------------------------------------------------------------- jamie · 2021-01-08 00:59 I just looked at some sample ASM code using your test code here.. It would appear that the short string is getting converted to a Ansi string first before it makes a compare check.. If the first char in slot 0 indicates more chars than 16 while reading in the short string it could over flow into unreadable memory.. I think for performing a simple test one a short string I find it strange the compiler would waste code space doing this.. In short, initiate the record first or at least set the string to :='' Test Short strings like this for now on.. if MyShortString[0] := #0 Then.... ---------------------------------------------------------------------- lainz · 2021-01-08 02:32 Or this way too 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 Function MyFuncion():integer;Beginif Length(CheckThisData.Data1) = 0 thenwriteln('OK');end; Or this way 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 const  Empty: string[1] = ''; Function MyFuncion():integer;Beginif CheckThisData.Data1 = Empty thenwriteln('OK');end; But not this way 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 CheckThisData.Data1.isEmpty // <-- using sysutils helper doesn't works as well ---------------------------------------------------------------------- lainz · 2021-01-08 02:36 Anyways, I'm using trunk and the original code works fine without problems. ---------------------------------------------------------------------- Thaddy · 2021-01-08 07:43 This depends on boolean evaluation settings. You correctly test the first condition, but if shortcut evaluation is off, the second will also be evaluated and in this case can cause problems. See the docs: https://www.freepascal.org/docs- html/current/prog/progsu4.html#x11-100001.2.4 In essence in {$B+} state, the second can not be solved safely, because it relies on the first and if that fails you can not have the second. Shortcut boolean evaluation is a better solution in such cases: {$B-} Note full boolean evaluation is only useful in very specific cases such as timing attacks on crypto. Simply make sure it is off. ---------------------------------------------------------------------- PascalDragon · 2021-01-08 07:56 Quote from: Thaddy on January 08, 2021, 08:43:46 amThis depends on boolean evaluation settings. This is irrelevant here as no side effects or pointers are involved. Quote from: torbente on January 08, 2021, 01:03:57 amWhy the program runs perfect with the first conditional only, but crashes if i try to check 2 conditionals? This is BY FAR the most incredible situation i ever seen usig laz/fp Would you please provide a full example that shows this and not merely snippets? ---------------------------------------------------------------------- krolikbest · 2021-01-08 09:46 Hi, check this simply code. should run whitout problems. Laz 2.0.10, Win7, if not maybe something wrong with configuration/options ? 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   TMyData= Packed Record  Data1 : string[16];  Data2 : Integer;  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1;  CheckThisData : TMyData; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  CheckThisData.Data2 := 3;  CheckThisData.Data1:=edit1.Text;  if ((CheckThisData.Data1 <> '') and (CheckThisData.Data2 = 3)) then   showmessage('OK')     else    showmessage('NOT OK')end; end. ---------------------------------------------------------------------- dseligo · 2021-01-08 10:30 I tried it on FPC 3.2.0, Windows 64-bit and it runs without SIGSEV. You can try code below on your end. What version of FPC are you using and on what OS? 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 program test; typeTMyData= Packed Record  Data1 : string[16];  Data2 : Integer;  end;var  CheckThisData : TMyData; Function MyFuncion1():integer;Begin  MyFuncion1:=0;  if CheckThisData.Data1 <> '' then MyFuncion1:=1;end; Function MyFuncion():integer;Begin  MyFuncion:=0;  if ((CheckThisData.Data1 <> '') and (CheckThisData.Data2 = 3)) then MyFuncion:=1;end;  begin  WriteLn(MyFuncion1);  WriteLn(MyFuncion);  ReadLn;end. ---------------------------------------------------------------------- torbente · 2021-01-08 12:33 Using lazarus 2.0.10, FPC 3.2.0 on windows 8.1 I tried using length(data1) > 0 and it crashes anyway; but using a simplified example like the one i posted it worked, so something was crashing the compiler in my program, and it was my fault, not any FPC bug. So i checked the value of data2 at the time of the logical operation: 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 Data1 <> '' then show(data2); And i got a totally unexpected 0. Since myfunction is only called AFTER a value is asigned to checkmydata, this was a logical error on my design. I had to study again all my flow chart to  find the error and eureka! The stuff to be done when both conditionals are ok, calls a -1 index of an array since i did not initialize that array properly when CheckMyData value was asigned.  Im sorry for this mistake and thanks to you all for your time.