Tcl Source Code

View Ticket
Login
Ticket UUID: 94c6511e870541dd5c4fbf8af014a07868b30f37
Title: Undefined pointer arithmetic in `INST_LREPLACE` when compiled `linsert`, `lreplace` or `ledit` insert two or more elements
Type: Bug Created on: 2026-11-06 18:46:33
Submitter: bef Assigned to: nobody
Subsystem: 17. Commands I-L Severity: Minor
Priority: 5 Medium Last modified: 2026-11-07 11:43:01
Status: Closed Closed by: apnadkarni
Resolution: Fixed Closed on: 2026-11-07 11:43:01
Version: 9.0.4, 9.1.0, 9.1.1 trunk
Description:

In INST_LREPLACE (generic/tclExecute.c), which compiled linsert, lreplace and ledit use, numNewElems is a size_t, so &OBJ_AT_DEPTH(numNewElems - 1) expands to tosPtr[-(numNewElems - 1)] with an unsigned offset. Whenever two or more elements are inserted, the offset wraps and the pointer arithmetic overflows, on both calls of Tcl_ListObjReplace (shared and unshared list). The resulting address is the right one and the result is correct, also on an -O2 build, but the arithmetic is undefined, and UBSan's pointer-overflow check stops on it. That hides any later report, and on a UBSan build it stops make test before the first test file runs:

apply {{l i} { lreplace $l $i $i x y }} {a b c} 1

tclExecute.c:5574:21: runtime error: addition of unsigned offset to 0x6110000063c0 overflowed to 0x6110000063b8

To see it, build with clang's -fsanitize=undefined, which includes the pointer-overflow check. From the top of a trunk checkout (tested with Apple clang 21 on macOS):

mkdir build-ubsan && cd build-ubsan
../unix/configure --disable-shared CC=clang \
    CFLAGS="-g -O1 -fsanitize=undefined -fno-sanitize-recover=undefined" \
    LDFLAGS="-fsanitize=undefined"
make tcltest
make test TESTFLAGS="-file append.test"

.../generic/tclExecute.c:5574:21: runtime error: addition of unsigned offset to 0x00099103c868 overflowed to 0x00099103c858
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior .../generic/tclExecute.c:5574:21
make: *** [test-tcl] Abort trap: 6

Any test file gives the same result, because runAllTests in library/tcltest/tcltest.tcl builds each child's command line with linsert $childargv 0 | $shell $file before any test runs. -fno-sanitize-recover=undefined makes the report fatal. Without it, UBSan prints the report once per location and the tests go on. Running a test file directly with ./tcltest ../tests/lreplace.test, without all.tcl, stops at lreplace-1.8, the first test that inserts more than one element.

Affected: 9.0.4 (INST_LREPLACE4), 9.1.0 and trunk (as of 2026-11-06, 6a05bcdb32), and the tip of core-9-0-branch. 8.6 does not have this instruction. lpop also compiles to it, but always without new elements, so it is not affected.

User Comments:
bef added on 2026-11-06 18:59:05:

Fix on branch bug-94c651-lreplace-pointer-overflow.

The fix makes numNewElems a Tcl_Size, like the other stack counts in that function. Then numNewElems - 1 is at least -1, and the offset stays small and signed. Test lreplace-5.3 covers lreplace on a shared and an unshared list, as well as linsert and ledit. It checks the result, but it only fails under UBSan with the pointer-overflow check. On the branch, lreplace.test, lsearch.test, linsert.test, compile.test, execute.test and clock.test pass under ASan and UBSan with no report.


apnadkarni added on 2026-11-07 11:43:01:
Fixed for 9.0 in [77a0c6a995] and 9.1 in [81a4f948d7].

The original type was signed. I have no idea as to what the rationale was for making them unsigned leading to undefined behaviour. So reverting back to signed as in the original code and the patch.

Whoever feels these need to be unsigned please indicate as to why.