Forum > FPC development
PosSetEx bug
dje:
Im using FPC 3.2.2. The code for PosSetEx doesn't quite match the code for PosEx
I checked the code for PosSetEx which is identical to my installed version:
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-objpas/src/inc/strutils.pp
So, the issue is the following two lines "should" return the same value:
--- 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";}};} --- WriteLn(PosEx('p', 'freepascal', -1000)); WriteLn(PosSetEx(['p'], 'freepascal', -1000)); Except I get:
--- 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";}};} ---0-891
The PosEx function includes this code which returns 0 for an Offset outside the string length:
--- 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";}};} ---if (Offset < 1) or (Offset > SizeUInt(Length(S))) then exit(0);
While the PosSetEx function only checks the upper bounds:
--- 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";}};} --- i:=length(s); j:=count; if j>i then begin result:=0; exit; end;
I would think the PosSetEx should also check the lower bounds. ie:
--- 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";}};} --- if (j < 1) or (j>i) then begin result:=0; exit; end;
marcov:
I wonder if it really useful to spend the two cycles for obvious wrong input.
Bart:
If I understand the code correctlyt then, if count < 0 then j will become count (negative) and s[j] will be accessed, but the index (j) is out of bounds.
That may potentially be dangerous.
Bart
KodeZwerg:
--- Quote from: marcov on December 09, 2022, 11:32:59 am ---I wonder if it really useful to spend the two cycles for obvious wrong input.
--- End quote ---
But it is a bug, I agree to OP. Wrong input or not. When I do read the documentation it should return 0.
--- Quote ---Description
PosSetEx returns the position in s of the first found character which is in the set c, and starts searching at character position Count. If none of the characters in c is found in s, then 0 is returned.
Errors
None.
--- End quote ---
In his example it returns a very strange result, dont you think that too?
I mean it find something but probably not at that negative adress :P
marcov:
--- Quote from: KodeZwerg on December 09, 2022, 03:52:04 pm ---
--- Quote from: marcov on December 09, 2022, 11:32:59 am ---I wonder if it really useful to spend the two cycles for obvious wrong input.
--- End quote ---
But it is a bug,
--- End quote ---
However, is it a code or a documentation bug ? When I wrote it, I definitely didn't consider negative input logical, as it serves no purpose whatsoever.
Navigation
[0] Message Index
[#] Next page