Forum > General

Short strings small bug

(1/6) > >>

Avinash:
FPC 3.2.2 Win32


--- 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";}};} ---var  S: String; begin   S[0]:= #255;  S := S + #0;  WriteLn(Length(S));  { gives correct 255 }   S[0]:= #255;  S[Length(S) + 1] := #0;  WriteLn(Length(S));  { gives wrong 0,  Turbo Pascal gives 255 } end.

Code responsible for this:


--- Code: ASM  [+][-]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";}};} ---; [11] S[Length(S) + 1]:= #0;%LINE 11+0                movzx   eax,byte [U_$P$PROGRAM_$$_S]                lea     eax,[eax+1]                movzx   eax,al                mov     byte [U_$P$PROGRAM_$$_S+eax*1],0 
And with {$O+} (what, by the way, does it turn on? I can't reproduce its effect by the -O1..4 switches):


--- Code: ASM  [+][-]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";}};} ---; [12] S[Length(S) + 1]:= #0;%LINE 12+0                movzx   eax,byte [U_$P$PROGRAM_$$_S]                add     eax,1                and     eax,255                mov     byte [U_$P$PROGRAM_$$_S+eax],0 

Bart:

--- Quote from: Avinash on November 10, 2022, 05:25:54 am ---FPC 3.2.2 Win32


--- 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";}};} ---var  S: String; begin   S[0]:= #255;  S := S + #0;  WriteLn(Length(S));  { gives correct 255 }   S[0]:= #255;  S[Length(S) + 1] := #0;  WriteLn(Length(S));  { gives wrong 0,  Turbo Pascal gives 255 } end.
--- End quote ---

Absoultey NOT a bug.
TP gives range check error at compile time.
Only if compiled with range checking off it'll output 255.
However, you are accessing invalid memory in this case and therefore the behaviour is therefore undefined.
You're just lucky the app doesn't crash.

One can guess that fpc treats the index of a shortstring as a byte and this value will overflow: 255+1=0.
Some compiler devel can comment on that.

Bart

Thaddy:
A case of a "programmer" not counting from zero?
The range is #0..#255 which is 256, so if you add/concat something to #255 you have a range error or an overflow.
If you want to avoid that, use AnsiString, not shortstring.

As Bart wrote, this is absolutely not a bug from the compiler side, it is frankly programmer error.
The most important thing is  that after an overflow the compiler's behavior is undefined, hence it can be either rubbish, 0, or 255. Since it is undefined you can not rely on such things. As Bart also wrote, you should use overflow checking, at least during debugging. Wrong code is wrong code....

MarkMLl:
Is assignment to element zero of a string even legal? I thought that other than the shortstring case it had been retired in favour of SetLength.

MarkMLl

Thaddy:
Good question. I oversaw it, but.. element zero stores the length if it is shortstring, and strings start from element 1.
Any other pascal string type also stores information below element 1, a.k.a. known as negative offset.

Navigation

[0] Message Index

[#] Next page

Go to full version