Forum > General

StrToInt not raising an error when string overflowing Integer

(1/3) > >>

BrunoK:
FPC 3.2.2

Given the following program :

--- 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";}};} ---program PgmStrToInt; uses  SysUtils; const   cStr: string = '355685667191548007'; var  vStr: String;  vInt: integer;  vInt64: int64; begin  vStr := cStr + '8';                  // Force vStr as a non const string   vInt64 := StrToInt64(vStr);               // OK  WriteLn(vInt64, ' ', HexStr(vInt64, 16)); // OK   vInt := StrToInt(vStr); // Doesnt raise an error. Is  it OK ?  WriteLn(vInt, ' ', HexStr(vInt, 8)); // Seems to take only the lower 4 bytes   WriteLn(TryStrToInt(vStr, vInt));    // Returns true (Should be false)   ReadLn;end.
is there something wrong in it that prevents raising an error or returning FALSE in the TryStrToInt ?

(Edited Subject after Zvoni's comment, was erroneously labeled
  IntToStr not raising an error when string overflowing Integer )

Thaddy:
cStr is not constant but static. Typed const is on by default.
Remove the type, then it is really a const and does what you think it should do.

BrunoK:

--- Quote from: Thaddy on February 05, 2025, 03:27:17 pm ---cStr is not constant but static. Typed const is on by default.
Remove the type, then it is really a const and does what you think it should do.

--- End quote ---
Whatever cStr / vStr are, the subject is about overflowed string to integer type conversion in StrToInt or TryStrToInt.

ALLIGATOR:

--- 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";}};} ---program test; uses  SysUtils; begin  WriteLn(TryStrToInt('355685667191548007', argc));   ReadLn;end.                                                  
FALSEFPC[main] x64, Win✅ OKTRUEFPC 3.2.2 x64, Win🐞 BUGFALSEFPC 3.2.2 x32, Win✅ OK

Zvoni:
Both/All (Try)StrToXXX use the compiler-intrinsic Val-Function under the hood, which in the second argument is a typeless Var-Argument for the Result

https://www.freepascal.org/docs-html/rtl/sysutils/strtoint.html
https://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html

But:
As far as i understood the doc's,
StrToXXX only fails if it encounters a "character" it can't convert. It doesn't check a Range-Overflow.
My Guess:
StrToInt: "Hey, here's a String, convert it to integer, and stuff it into the Integer-Result-Variable. Call out if something goes wrong"
Val-Func: "Ah well. The Result-Variable only had that much bytes available. Not my Problem"

TryStrToXXX does check the Range (explicit mention in the docs)


btw: Why does the Thread-Title say "IntToStr" when we're talking about StrToInt?!?!

Navigation

[0] Message Index

[#] Next page

Go to full version