Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a337784d334d55c6516846020058bdae17c5746144e5e43824e85b65aff24c66
Ticket: 13d3c0d764fa4cf1c0cf0577924d7a0be5a3fd2b
lset on empty list is allowed when index argument is large
User & Date: apnadkarni on 2023-08-13 15:47:58
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    `lset` is supposed to only allow setting of elements for out of bounds indices when the specified index is just after the last valid index (i.e. equal to list length). However consider 
    
    ```
    % set L {}
    % lset L 0x7ffffffffffffffe X
    index "0x7ffffffffffffffe" out of range
    % lset L 0x7fffffffffffffff X
    X
    % lset L 0x7fffffffffffffff X
    index "0x7fffffffffffffff" out of range
    ```
    
    When the list is empty, the index 0x7fffffffffffffff (or higher) is incorrectly treated as index 0 (i.e. just beyond last valid index). As the first line shows, smaller out of bounds indices are treated correctly. As the last line shows, this bug only occurs when the list is empty.
    
    Like bug [bc076f4f0e](https://core.tcl-lang.org/tcl/tktview/bc076f4f0e), this bug was also introduced by commit [0b05b5c750df](https://core.tcl-lang.org/tcl/info/0b05b5c750df) intended to fix TIP 577 issues in Tk.
    
    While this issue may be considered relatively minor, it points to what I think is a fundamental flaw in internal index encoding in Tcl: the number of values that need to be represented in an "index" are greater than the range of the index type, no matter whether 32- or 64-bits. This is due to non-negative integers being valid (for obvious reasons), negative integers being valid (as end-N) representation, which together occupy the entire domain. Then there is a need for additional values representing "end" and "one beyond end" for which there is no room necessitating hacks like [this](https://core.tcl-lang.org/tcl/file?ci=df58824c7868a07c&name=generic/tclListObj.c&ln=2930-2931) which is the cause of the above bug. Removing that hack will cause `lset` to fail on
    
    ```
    set L {} ; lset L 0 X
    ```
    
    which should be allowed. Changing the core indexing routines to fix this breaks Tk indexing for negative (in the C API) and end relative indices.
    
    Given the above limitation in range of values I see no way around this other than living with the above bug for 9.0. If I am wrong and anyone has a solution, please verify it works with both Tk and the bigdata tests for large indices.
    
  5. foundin changed to: "9.0"
  6. is_private changed to: "0"
  7. login: "apnadkarni"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "9546e9877f1a8899e25a89082561ee694e7fb402"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "apnadkarni"
  14. subsystem changed to: "- New Builtin Commands"
  15. title changed to:
    lset on empty list is allowed when index argument is large
    
  16. type changed to: "Bug"