Browse Source

* fix #39664: ensure that the 8- and 16-bit signed overloads of IntToHex return the correct number of digits
+ added test

(cherry picked from commit d32134dd1a0b52525a3dbb6c5083b8d6e90647d2)

Sven/Sarah Barth 3 years ago
parent
commit
affd8b02d9
2 changed files with 45 additions and 2 deletions
  1. 2 2
      rtl/objpas/sysutils/sysstr.inc
  2. 43 0
      tests/webtbs/tw39664.pp

+ 2 - 2
rtl/objpas/sysutils/sysstr.inc

@@ -921,7 +921,7 @@ end;
 
 
 function IntToHex(Value: Int8): string;
 function IntToHex(Value: Int8): string;
 begin
 begin
-  Result:=IntToHex(Value, 2*SizeOf(Int8));
+  Result:=IntToHex(LongInt(Value) and $ff, 2*SizeOf(Int8));
 end;
 end;
 
 
 function IntToHex(Value: UInt8): string;
 function IntToHex(Value: UInt8): string;
@@ -931,7 +931,7 @@ end;
 
 
 function IntToHex(Value: Int16): string;
 function IntToHex(Value: Int16): string;
 begin
 begin
-  Result:=IntToHex(Value, 2*SizeOf(Int16));
+  Result:=IntToHex(LongInt(Value) and $ffff, 2*SizeOf(Int16));
 end;
 end;
 
 
 function IntToHex(Value: UInt16): string;
 function IntToHex(Value: UInt16): string;

+ 43 - 0
tests/webtbs/tw39664.pp

@@ -0,0 +1,43 @@
+program tw39664;
+
+{$mode Delphi}
+
+uses
+  SysUtils;
+
+var
+  i8: Int8;
+  i16: Int16;
+  i32: Int32;
+  u32: UInt32;
+  s: String;
+
+begin
+  i8 := -42;
+  s := IntToHex(i8);
+  writeln(s);
+  if s <> 'D6' then halt(1);
+
+  i16 := -42;
+  s := IntToHex(i16);
+  writeln(s);
+  if s <> 'FFD6' then halt(2);
+
+  i32 := -42;
+  s := IntToHex(i32);
+  writeln(s);
+  if s <> 'FFFFFFD6' then halt(3);
+
+  s := i8.ToHexString;
+  writeln(s);
+  if s <> 'D6' then halt(4);
+
+  s := i16.ToHexString;
+  writeln(s);
+  if s <> 'FFD6' then halt(5);
+
+  s := i32.ToHexString;
+  writeln(s);
+  if s <> 'FFFFFFD6' then halt(6);
+end.
+