Modify

Opened 6 years ago

Last modified 14 months ago

#3717 assigned Bug

StringFormat doesn't format int64 integers correctly

Reported by: jchd18 Owned by: Jon
Milestone: Component: AutoIt
Version: 3.3.14.0 Severity: None
Keywords: Cc:

Description

The code below returns invalid results due to %i, %d and %u specifiers being limited to 32-bit integers.

Local $b = [["2^31-1", 2^31-1], ["2^31  ", 2^31], ["2^32-1", 2^32-1], ["2^32  ", 2^32], ["2^33  ", 2^33]], $s, $n

For $i = 0 To UBound($b) - 1
	$s = $b[$i][0]
	$n = $b[$i][1]
	ConsoleWrite(StringFormat("%-14s%-6s%12i", $s & "  [%i]", VarGetType($n), $n) & (StringFormat("%i", $n) = $n ? "" : "  should be  " & $b[$i][1]) & @LF)
	ConsoleWrite(StringFormat("%-14s%-6s%12u", $s & "  [%u]", VarGetType($n), $n) & (StringFormat("%u", $n) = $n ? "" : "  should be  " & $b[$i][1]) & @LF & @LF)
Next

Change History (11)

comment:1 Changed 6 years ago by water

Tested with 3.3.15.0 - same problem.

comment:2 Changed 5 years ago by jchd18

  • Summary changed from StringFormat don't format int64 integers correctly to StringFormat doesn't format int64 integers correctly

comment:3 Changed 5 years ago by jchd18

Even in X64 Stringformat is limited to 32-bit values, making it unable to format 64-bit pointers.

comment:4 Changed 5 years ago by Jpm

in fact as the %p is not implemented under AutoIt the %s must be used to format ptr's

comment:5 Changed 5 years ago by Jpm

  • Owner set to Jon
  • Status changed from new to assigned

comment:6 Changed 5 years ago by Jpm

  • Owner changed from Jon to Jpm

Hi, Fix send to Jon

comment:7 Changed 3 years ago by anonymous

Didn't like the fix. I don't think that %d/%i should use a 32bit or 64bit value based on the version of AutoIt running. In C the printf %d is always 32bit, with a special code for using 64bits. Pointers I agree can be different on x86/x64.

comment:8 Changed 3 years ago by Jon

That was me, btw.

comment:9 Changed 3 years ago by Jpm

Hi Jon,
I know that there is special code for 64bits but as I don't think it was so important to add code
to support to support them That why I propose to handle %d %i for x64 integer when running in X64 mode
Perhaps my fix was not perfect.

comment:10 Changed 3 years ago by Jpm

In fact with Jon remark
I understand that the MSDN string format usage will be

2^31-1  [%X]  Int64               7FFFFFFF  OK
2^31-1  [%d]  Int64             2147483647  OK
2^31-1  [%i]  Int64             2147483647  OK
2^31-1  [%u]  Int64             2147483647  OK
2^31-1  [%f]  Int64             2147483647  OK
2^31-1  [%s]  Int64             2147483647  OK
2^31-1  [%p]  Int64             0x7FFFFFFF  OK
2^31-1 [%hi]  Int64                     -1  OK
2^31-1 [%hu]  Int64                  65535  OK

int_max  [%X] Int32               7FFFFFFF  OK
int_max  [%d] Int32             2147483647  OK
int_max  [%i] Int32             2147483647  OK
int_max  [%u] Int32             2147483647  OK
int_max  [%f] Int32             2147483647  OK
int_max  [%s] Int32             2147483647  OK
int_max  [%p] Int32             0x7FFFFFFF  OK
int_max [%hi] Int32                     -1  OK
int_max [%hu] Int32                  65535  OK

2^31    [%X]  Int64               80000000  OK
2^31    [%d]  Int64            -2147483648  should be  2147483648
2^31    [%i]  Int64            -2147483648  should be  2147483648
2^31    [%u]  Int64             2147483648  OK
2^31    [%f]  Int64             2147483648  OK
2^31    [%s]  Int64             2147483648  OK
2^31    [%p]  Int64             0x80000000  OK
2^31   [%hi]  Int64                      0  OK
2^31   [%hu]  Int64                      0  OK

int_min  [%X] Int32               80000000  OK
int_min  [%d] Int32            -2147483648  OK
int_min  [%i] Int32            -2147483648  OK
int_min  [%u] Int32             2147483648  OK
int_min  [%f] Int32            -2147483648  OK
int_min  [%s] Int32            -2147483648  OK
int_min  [%p] Int32             0x80000000  OK
int_min [%hi] Int32                      0  OK
int_min [%hu] Int32                      0  OK

int_1  [%X]   Int32               FFFFFFFF  OK
int_1  [%d]   Int32                     -1  OK
int_1  [%i]   Int32                     -1  OK
int_1  [%u]   Int32             4294967295  OK
int_1  [%f]   Int32                     -1  OK
int_1  [%s]   Int32                     -1  OK
int_1  [%p]   Int32             0xFFFFFFFF  OK
int_1 [%hi]   Int32                     -1  OK
int_1 [%hu]   Int32                  65535  OK

2^32-1  [%X]  Int64               FFFFFFFF  OK
2^32-1  [%d]  Int64                     -1  should be  4294967295
2^32-1  [%i]  Int64                     -1  should be  4294967295
2^32-1  [%u]  Int64             4294967295  OK
2^32-1  [%f]  Int64             4294967295  OK
2^32-1  [%s]  Int64             4294967295  OK
2^32-1  [%p]  Int64             0xFFFFFFFF  OK
2^32-1 [%hi]  Int64                     -1  OK
2^32-1 [%hu]  Int64                  65535  OK

2^32    [%X]  Int64              100000000  OK
2^32    [%d]  Int64                      0  should be  4294967296
2^32    [%i]  Int64                      0  should be  4294967296
2^32    [%u]  Int64             4294967296  OK
2^32    [%f]  Int64             4294967296  OK
2^32    [%s]  Int64             4294967296  OK
2^32    [%p]  Int64             0x00000000  OK
2^32   [%hi]  Int64                      0  OK
2^32   [%hu]  Int64                      0  OK

Ptr(-1)  [%X] Ptr                 FFFFFFFF  OK
Ptr(-1)  [%d] Ptr                       -1  OK
Ptr(-1)  [%i] Ptr                       -1  OK
Ptr(-1)  [%u] Ptr               4294967295  OK
Ptr(-1)  [%f] Ptr                       -1  OK
Ptr(-1)  [%s] Ptr               0xFFFFFFFF  OK
Ptr(-1)  [%p] Ptr               0xFFFFFFFF  OK
Ptr(-1) [%hi] Ptr                       -1  OK
Ptr(-1) [%hu] Ptr                    65535  OK

2^63-1  [%X]  Int64       7FFFFFFFFFFFFFFF  OK
2^63-1  [%d]  Int64                     -1  should be  9223372036854775807
2^63-1  [%i]  Int64                     -1  should be  9223372036854775807
2^63-1  [%u]  Int64    9223372036854775807  OK
2^63-1  [%f]  Int64    9223372036854775808  OK
2^63-1  [%s]  Int64    9223372036854775807  OK
2^63-1  [%p]  Int64             0xFFFFFFFF  OK
2^63-1 [%hi]  Int64                     -1  OK
2^63-1 [%hu]  Int64                  65535  OK

2^63  [%X]    Int64       8000000000000000  OK
2^63  [%d]    Int64                      0  should be  -9223372036854775808
2^63  [%i]    Int64                      0  should be  -9223372036854775808
2^63  [%u]    Int64    9223372036854775808  OK
2^63  [%f]    Int64   -9223372036854775808  OK
2^63  [%s]    Int64   -9223372036854775808  OK
2^63  [%p]    Int64             0x00000000  OK
2^63 [%hi]    Int64                      0  OK
2^63 [%hu]    Int64                      0  OK

Do you agree to follow Jon remark?

comment:11 Changed 14 months ago by Jpm

  • Owner changed from Jpm to Jon

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.