Forum > General

Possible bug with range checking in StrUtils.IfThen function

(1/5) > >>

BSaidus:
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

Bart:
IIRC then all parameters of a function call will be evaluated.
So the expression CEth[lIndex-1] will be evaluated wether or not lIndex >= 1.

The fact that the function is called IfThen does not imply that boolean shortcutting is applied to it's parameter evaluation...

Bart

BSaidus:

--- Quote from: Bart on December 07, 2023, 11:22:59 pm ---IIRC then all parameters of a function call will be evaluated.
So the expression CEth[lIndex-1] will be evaluated wether or not lIndex >= 1.

The fact that the function is called IfThen does not imply that boolean shortcutting is applied to it's parameter evaluation...

Bart

--- End quote ---
Thanks for your answer.
Looking inside the code of IfThen in StrUtils, it do not looks (evaluate) the ATrue value if the condition is not sitisfied.

--- 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 IfThen(AValue: Boolean; const ATrue: string; const AFalse: string): string; begin  if avalue then    result:=atrue  else    result:=afalse;end;   

Fibonacci:
Your code didnt work for me, I had to use specialize:


--- 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 := specialize IfThen<String>(lIndex>=1, CEth[lIndex-1], 'tralllla');
FPC trunk, no problems, no range errors

BSaidus:

--- Quote from: Fibonacci on December 07, 2023, 11:30:44 pm ---Your code didnt work for me, I had to use specialize:


--- 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 := specialize IfThen<String>(lIndex>=1, CEth[lIndex-1], 'tralllla');
FPC trunk, no problems, no range errors

--- End quote ---
Did you try to compile it in full debug mode ??

Navigation

[0] Message Index

[#] Next page

Go to full version