Parcourir la source

* proper range checking for TryStrToDWord, should resolve all issues in #39406

(cherry picked from commit 983fbff8711a8c625e494a981891a90d47847e14)
florian il y a 3 ans
Parent
commit
d1166342ca
2 fichiers modifiés avec 21 ajouts et 3 suppressions
  1. 7 3
      rtl/objpas/sysutils/sysstr.inc
  2. 14 0
      tests/webtbs/tw39406.pp

+ 7 - 3
rtl/objpas/sysutils/sysstr.inc

@@ -1006,10 +1006,14 @@ begin
 end;
 
 function TryStrToDWord(const s: string; Out D: DWord): boolean;
-var Error : word;
+var 
+  Error : word;
+  lq : QWord;
 begin
-  Val(s, D, Error);
-  TryStrToDWord:=Error=0
+  Val(s, lq, Error);
+  TryStrToDWord:=(Error=0) and (lq<=High(DWord));
+  if TryStrToDWord then
+    D:=lq;
 end;
 
 function StrToUInt(const s: string): Cardinal;

+ 14 - 0
tests/webtbs/tw39406.pp

@@ -0,0 +1,14 @@
+{$mode objfpc}
+program fpbug;
+
+uses
+  SysUtils;
+
+var
+  Value:   Cardinal;
+  Success: Boolean;
+begin
+  Success := TryStrToDword('7795000000', Value);
+  if Success then
+    halt(1);
+end.