Skip to content
Tcl Source Code View Ticket
Login

View Ticket

list indexing corrupts wide ints

Closed / Fixed526717ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2002-03-07 00:33:14
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
dgpplain2002-03-07 07:36:26
Logged In: YES 
user_id=80530

More generally, any place that 
Tcl_GetIntFromObj is called should be considered
suspect.
dkfplain2002-03-14 22:03:44
Logged In: YES 
user_id=79902

I supoose we could make Tcl_GetIntFromObj know about
wideobjs...
dkfplain2002-04-19 20:09:06
Logged In: YES 
user_id=79902

TclGetIntForIndex really ought to use 'long' and 'long*' not
'int' and 'int*'...
Log in (anonymous is fine) to add a comment.