Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-04-26
16:29 • Closed ticket [fa00fbbbab]: lseq / lindex discrepancies plus 6 other changes artifact: fb5d0ecad2 user: griffin
2023-04-21
15:44 • Ticket [fa00fbbbab]: 3 changes artifact: b57b6950c2 user: griffin
2023-04-17
03:10 • Ticket [fa00fbbbab]: 3 changes artifact: b45c490cfb user: griffin
01:59 • Ticket [fa00fbbbab]: 3 changes artifact: 04fcc06f03 user: apnadkarni
2023-04-16
23:02 • Ticket [fa00fbbbab]: 3 changes artifact: 901c1085fd user: griffin
2023-04-14
03:01 • Ticket [fa00fbbbab]: 3 changes artifact: a08d9bf350 user: apnadkarni
02:54 • Ticket [fa00fbbbab]: 2 changes artifact: b5b7d0c2b6 user: apnadkarni
02:54 • Ticket [fa00fbbbab]: 3 changes artifact: 301d94bd2c user: apnadkarni
2023-04-12
15:33 • Ticket [fa00fbbbab]: 3 changes artifact: c37e20118a user: apnadkarni
14:31 • New ticket [fa00fbbbab]. artifact: d315be1fe1 user: apnadkarni

lseq / lindex discrepancies

Closed / Fixedfa00fbbbabe2d77904ac0086884d5debf9c529a5Created 4 years agoUpdated 4 years ago
Descriptionmarkdown2023-04-12 14:31:09

I expect lindex with indices larger than list size to return empty string. This is the case with small lseq ranges but not large ones (64-bit build)

% lindex [lseq 0x10] 0x11 -> correctly returns empty string
% lindex [lseq 0x7fffffff] 0x80000000  -> generates error instead
index 2147483648 is out of bounds 0 to 2147483646
%

Another possibly related (on 32-bit build). This may be related to signed/unsigned indexing and may go away with TIP 660.

% lindex [lseq 0xffffffff] 0x80000000 -> should return 2147483648
%

Finally, some check (and exception) is needed when list limit is exceeded. The following on 32-bit Tcl should generate an error since the full range of elements cannot be accessed by indexing.

% llength [lseq 0x100000000]
0

I have not tried this on the tip-636 branch.

/Ashok

apnadkarni Ticket [fa00fbbbab]: 3 changes [History diff]2023-04-12 15:33:52
apnadkarniplain2023-04-14 02:54:19
The equivalent on 64 bit builds also fails.

```
% set l [lseq 0x7fffffffffffffff]; llength $l
9223372036854775807
% lindex $l 9223372036854775800 -> returns empty string
% 
```

Again, I'm not sure if this is a Tcl index implementation issue or lseq since it is not possible to test equivalent in "traditional" lists that cannot reach that size.
apnadkarni Ticket [fa00fbbbab]: 2 changes [History diff]2023-04-14 02:54:56
apnadkarniplain2023-04-14 03:01:47
There is also a difference between the compiled and uncompiled results, neither of which is the correct result:

```
% set l [lseq 0x7fffffffffffffff]; llength $l
9223372036854775807
% lindex $l 9223372036854775800
% set lindex lindex
lindex
% $lindex $l 9223372036854775800
-9223372036854775808% set l [lseq 0x7fffffffffffffff]; llength $l
9223372036854775807
% lindex $l 9223372036854775800
% set lindex lindex
lindex
% $lindex $l 9223372036854775800
-9223372036854775808
```
griffinplain2023-04-16 23:02:10
The last issue is a general problem with byte compiler and index values. This comment from tclUtil.c explains:

 * TclIndexEncode --
 *
 *      Parse objPtr to determine if it is an index value. Two cases
 *	are possible.  The value objPtr might be parsed as an absolute
 *	index value in the C signed int range.  Note that this includes
 *	index values that are integers as presented and it includes index
 *      arithmetic expressions. The absolute index values that can be
 *	directly meaningful as an index into either a list or a string are
 *	those integer values >= TCL_INDEX_START (0)
 *	and < INT_MAX.
 *      The largest string supported in Tcl 8 has bytelength INT_MAX.
 *      This means the largest supported character length is also INT_MAX,
 *      and the index of the last character in a string of length INT_MAX
 *      is INT_MAX-1.

It looks like this was missed when moving from 32-bit indices to 64-bit indices.
apnadkarniplain2023-04-17 01:59:33
I don't think that TclIndexEncode was missed in moving to 64-bits but rather it was intentional. Iirc, the byte code format is limited to 32-bits and the TclIndexEncode/TclIndexDecode are only used for immediate literal operands (N or end-N) in that range. Operands in variables or outside that range use the TclGetIntForIndexM (or its function equivalent) and load the value from Tcl_Objs on the stack. That is my understanding.

Having said that, there is a ticket logged for some index values so there are definitely some issues that need to be resolved in that area.
griffinplain2023-04-17 03:10:31
Ok then.  I will fix the compilation to detect overflow for literals and take appropriate measures.
Thanks!
griffin Ticket [fa00fbbbab]: 3 changes [History diff]2023-04-21 15:44:23
griffinplain2023-04-26 16:29:15
Fixed with check-in [a610e50b2faa8c52]
Log in (anonymous is fine) to add a comment.