Forum > General

Possible bug with range checking in StrUtils.IfThen function

<< < (2/5) > >>

Fibonacci:
Yes

Also managed to run your code this way


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Edit1.Text := StrUtils.IfThen( lIndex>=1, CEth[lIndex-1], 'tralllla')
No errors, no crashes, TRUNK

BSaidus:
Strange, same problem with : Lazarus 3.0RC1 (rev fe49fef4) FPC 3.3.1 i386-win32-win32/win64

Fibonacci:
I compiled your project (.zip file) and well, there is range check error.

Ok, so both strings are known before the boolean is checked. Proof:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function nono: string;begin  showmessage('hello');  result := 'false';end; procedure TForm1.Button1Click(Sender: TObject);begin  Edit1.Text := IfThen(false, nono, 'hi');end;
I guess since this is an array, or in case above a function, it cant be passed by ref.

Somehow when I created my form and just copy-pasted ur code there wasnt this error.

Чебурашка:

--- Quote from: BSaidus on December 07, 2023, 11:12:27 pm ---Hello.
Testing the following code in attached project raise an exception as shown in the screenshot.
The code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    cbParentIfs: TComboBox;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation uses  StrUtils  ; CONST CEth: array[0..4] of string = ('01','02','03','04','05'); {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  lIndex: Integer;begin  lIndex := cbParentIfs.ItemIndex;  Edit1.Text := IfThen( lIndex>=1, CEth[lIndex-1], 'tralllla');end; end.  

PS: fpc 3.2.3 fixes, lazarus 2.2.7 fixes

--- End quote ---

The error is not inside the IfThen, but is before it is called, during the preparation of the stack for the function call.

https://forum.lazarus.freepascal.org/index.php/topic,59197.0.html

Bart:

--- Quote from: BSaidus on December 07, 2023, 11:25:59 pm ---Looking inside the code of IfThen in StrUtils, it do not looks (evaluate) the ATrue value if the condition is not sitisfied.

--- End quote ---

That has nothing to do with it.
The parameters are evaluated at the time you call the function.
Say you have a function like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function foo(x,y: integer): integer;Then you call it like

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---z := foo(1 div 0, 100);Now, you would not be surprised that you get an exception (div by zero).
Suppose you also have a function like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function bar(a,b: integer): integer;begin  result := a div b;end;
And then you do this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  z := foo(1, bar(1,0));This will of course also raise an exception

Now this

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function foo(b: boolean; x,y: integer): integer;...  z:= foo(False, 1, bar(1,0));
This will crash regardless of the implementation of foo (so even if the imeplementation of foo is completely empty).

Bart

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version