Overview
| Artifact ID: | aaa2d611f546df0ff5b86c461572fab512c191c3 |
|---|---|
| Ticket: | 699060ffffffffffffffffffffffffffffffffff
format %08x 32/64bit confusion |
| User & Date: | srwarren on 2003-03-06 23:09:15 |
Changes
- assignee changed to: "hobbs"
- cmimetype changed to: "text/plain"
- comment changed to:
I have found a bug in Tcl 8.4.1 for platforms with 64-bit integer support enabled, i.e. where "#ifndef TCL_WIDE_INT_IS_LONG" is true. The problem is that the format internal function treats integers (%08x format) as 32-bit, and updates object's internal "cache" value to be tclIntType instead of tclWideIntType. The docs for format say: # The l modifier is ignored for real values and on 64-bit platforms, # which are always converted as if the l modifier were present (i.e. # the types double and long are used for the internal representation # of real and integer values, respectively). If the h modifier is # specified then integer values are truncated to short before # conversion. Both h and l modifiers are ignored on all other # conversions. but I can get format to use a 32-bit internal representation on a 64-bit system, which contradicts the docs (the docs match my idea of what should be happening.) The code to reproduce the issue is as follows: ========================================= =================== proc tcl_ok {} { set a 0xaaaaaaaa set b_1 0xaaaa set b_2 aaaa set b "${b_1}${b_2}" if {$a != $b} { puts "Test ok_1: a NOT EQUAL b" } else { puts "Test ok_1: a EQUALS b" } set x [format %08lx $a] if {$a != $b} { puts "Test ok_2: a NOT EQUAL b" } else { puts "Test ok_2: a EQUALS b" } } proc tcl_error {} { set a 0xaaaaaaaa set b_1 0xaaaa set b_2 aaaa set b "${b_1}${b_2}" if {$a != $b} { puts "Test error_1: a NOT EQUAL b" } else { puts "Test error_1: a EQUALS b" } set x [format %08x $a] # format has changed $a to int internal representation # b has no type, so is interpreted as wide_int # a is sign-extended 32->64 to match b's size # hence this comparison fails if {$a != $b} { puts "Test error_2: a NOT EQUAL b" } else { puts "Test error_2: a EQUALS b" } } puts $tcl_patchLevel tcl_ok tcl_error ========================================= =================== which prints: ========================================= =================== 8.4.1 Test ok_1: a EQUALS b Test ok_2: a EQUALS b Test error_1: a EQUALS b Test error_2: a NOT EQUAL b ========================================= =================== The issue is the code in tclCmdAH.c in function Tcl_FormatObjCmd (at line 2187 in Tcl 8.4.1) This says: ========================================= =================== #ifndef TCL_WIDE_INT_IS_LONG if (useWide) { if (Tcl_GetWideIntFromObj(interp, /* INTL: Tcl source. */ objv[objIndex], &wideValue) != TCL_OK) { goto fmtError; } whichValue = WIDE_VALUE; size = 40 + precision; break; } #endif /* TCL_WIDE_INT_IS_LONG */ if (Tcl_GetLongFromObj(interp, /* INTL: Tcl source. */ objv[objIndex], &intValue) != TCL_OK) { goto fmtError; } ========================================= =================== But it should say something more like: ========================================= =================== #ifndef TCL_WIDE_INT_IS_LONG if (Tcl_GetWideIntFromObj(interp, /* INTL: Tcl source. */ objv[objIndex], &wideValue) != TCL_OK) { goto fmtError; } if (useWide) { whichValue = WIDE_VALUE; size = 40 + precision; break; } else { value_used_for_formatting = wide_value & 0xffffffff // setup other formatting params here? } #else /* TCL_WIDE_INT_IS_LONG */ if (Tcl_GetLongFromObj(interp, /* INTL: Tcl source. */ objv[objIndex], &intValue) != TCL_OK) { goto fmtError; } #endif /* TCL_WIDE_INT_IS_LONG */ ========================================= =================== i.e. on 64-bit architectures, Tcl should *always* interpret strings as 64-bit numbers, but follow the wording of the docs on the format h/l modifiers more precisely by truncating 64->32/16 bits depending on whether h/l are specified, instead of interpreting the number as 64/32/(16?) depending on whether h/l are specified. On a more general note, a lot of the Tcl code says this: #if HAVE_A_64_BIT_TYPE do_something_64 bail out, or whatever #endif do_something_32 This leads to this type of bug. It'd be better if everything said this: #if HAVE_A_64_BIT_TYPE do_something_64 #else do_something_32 #endif any_common_code Plus, shouldn't the tclIntType be completely removed if 64-bit numbers are enabled? Or, make tclIntType a #define/whatever to tclInt32Type or tclInt64Type, so that there's never ever a possibility of using the wrong internal representation for an integer. - foundin changed to: "obsolete: 8.4.1"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- status changed to: "Open"
- submitter changed to: "srwarren"
- subsystem changed to: "11. Conversions from String"
- title changed to: "format %08x 32/64bit confusion"
- type changed to: "Bug"