Format('%x', [...]) returns strange things for values between High(LongInt) and High(LongWord)
Original Reporter info from Mantis: FPCCore
-
Reporter name: FPC core team
Original Reporter info from Mantis: FPCCore
- Reporter name: FPC core team
Description:
(Note: before playing with this bug, be sure that bug 3477 is fixed, otherwise some tests may seem strange.)
When calling Format and giving as argument value that fits in 32 bits but does not fit inside (signed!) LongInt range (like $99999999), Format outputs strange things. What happens is that inside Format implementation vl is initialized with
Args[Doarg].VInteger
which is a negative value. And vl is Int64. But then string is constructed like
HexStr(qword(vl),Prec)
(because HexStr can't actually convert negative values) and example result is string 'FFFFFFFF99999999' from value $99999999.
I see a few solutions to this:
- Since Format('%x', [...]) can't handle negative values anyway, you can just apply this simple patch
@@ -1066,7 +1066,7 @@
'X' : begin
if Checkarg(vtinteger,false) then
begin
- vl:=Args[Doarg].VInteger;
+ vl:=Cardinal(Args[Doarg].VInteger);
index:=16;
end
else
- But I don't know if I like this 1st solution, it looks like a non-general solution to a general problem. A cleaner solution would be to pass values > High(LongInt) and <= High(LongWord) as vtInt64 values. And the cleanest solution seems to be to add something like vtCardinal.
However both of them are not Delphi-compatible: I just checked with Kylix3, and values like $99999999, $FFFFFFFF are passed as vtInteger. So called procedure has no way to distinguish between $99999999 and $99999999 typecasted to LongInt (i.e. 32 bit signed type).
Additional information:
Reporter: Michalis Kamburelis
EMail: michalis at camelot dot homedns dot org
Mantis conversion info:
- Mantis ID: 5471
- OS: Linux
- Version: 1.9.5
- Fixed in version: 1.9.5