Menu

#1802 list indexing corrupts wide ints

obsolete: 8.4a5
closed-fixed
5
2002-04-19
2002-03-07
Don Porter
No

Use of a Tcl_WideInt as a list index will
shimmer it to an "int".

On a 32-bit platform:
% set index [expr {(wide(1)<<31) - 2}]
2147483646
% lindex {1 2 3} $index
% incr index
2147483647
% incr index
-2147483648

Contrast with:
% set index [expr {(wide(1)<<31) - 2}]
2147483646
% incr index
2147483647
% incr index
2147483648

It's worse on 64-bit platforms, where truncation
is to an "int" even though Tcl_Integers are really
longs:
% set index [expr {wide(1)<<32}]
4294967296
% lindex {1 2 3} $index
1
% incr index
4294967297
% lindex {1 2 3} $index
2

So here, we don't lose range on the index values,
but the indexing function "wraps around".

Suspect trouble in TclGetIntForIndex

Discussion

  • Don Porter

    Don Porter - 2002-03-07

    Logged In: YES
    user_id=80530

    More generally, any place that
    Tcl_GetIntFromObj is called should be considered
    suspect.

     
  • Donal K. Fellows

    Logged In: YES
    user_id=79902

    I supoose we could make Tcl_GetIntFromObj know about
    wideobjs...

     
  • Donal K. Fellows

    • status: open --> closed-fixed
     
  • Donal K. Fellows

    Logged In: YES
    user_id=79902

    TclGetIntForIndex really ought to use 'long' and 'long*' not
    'int' and 'int*'...