Browse Source

* patch by Bart B to fix the value of Code for unsigned types if the input is negative, resolves #39523

florian 3 years ago
parent
commit
fc3b635013
2 changed files with 25 additions and 3 deletions
  1. 12 3
      rtl/inc/sstrings.inc
  2. 13 0
      tests/webtbs/tw39523.pp

+ 12 - 3
rtl/inc/sstrings.inc

@@ -1258,7 +1258,10 @@ begin
   fpc_Val_UInt_Shortstr:=0;
   Code:=InitVal(s,negative,base);
   If Negative or (Code>length(s)) Then
-    Exit;
+   begin
+     if Negative then Code:=Pos('-',S);
+     Exit;
+   end;
   if (s[Code]=#0) then
     begin
       if (Code>1) and (s[Code-1]='0') then
@@ -1365,7 +1368,10 @@ end;
     fpc_val_qword_shortstr:=0;
     Code:=InitVal(s,negative,base);
     If Negative or (Code>length(s)) Then
-      Exit;
+      begin
+        if Negative then Code:=Pos('-',S);
+        Exit;
+      end;
     if (s[Code]=#0) then
       begin
         if (Code>1) and (s[Code-1]='0') then
@@ -1564,7 +1570,10 @@ end;
     fpc_val_word_shortstr:=0;
     Code:=InitVal(s,negative,base);
     If Negative or (Code>length(s)) Then
-      Exit;
+      begin
+        if Negative then Code:=Pos('-',S);
+        Exit;
+      end;
     if (s[Code]=#0) then
       begin
         if (Code>1) and (s[Code-1]='0') then

+ 13 - 0
tests/webtbs/tw39523.pp

@@ -0,0 +1,13 @@
+var
+  w: Word;
+  Code: Integer;
+begin
+  Val('-1',W,Code);
+  writeln('Code: ',Code); //outputs: 2
+  if Code<>1 then
+    halt(1);
+  Val('-0x1',w,Code);
+  writeln('Code: ',Code); //outputs: 4
+  if Code<>1 then
+    halt(2);
+end.