Overview
| Artifact ID: | e149ab6796c5bd3146de22fa852536c19ffb1234846194d01d9c11aeeaa8b3df |
|---|---|
| Ticket: | 94c6511e870541dd5c4fbf8af014a07868b30f37
Undefined pointer arithmetic in `INST_LREPLACE` when compiled `linsert`, `lreplace` or `ledit` insert two or more elements |
| User & Date: | bef on 2026-11-06 18:46:33 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-markdown"
- comment changed to:
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](https://core.tcl-lang.org/tcl/info/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. - foundin changed to: "9.0.4, 9.1.0, 9.1.1 trunk"
- is_private changed to: "0"
- login: "bef"
- priority changed to: "5 Medium"
- private_contact changed to: "37918056d323dc69a2f76cecc85476189094f3b8"
- resolution changed to: "None"
- severity changed to: "Minor"
- status changed to: "Open"
- submitter changed to: "bef"
- subsystem changed to: "17. Commands I-L"
- title changed to:
Undefined pointer arithmetic in `INST_LREPLACE` when compiled `linsert`, `lreplace` or `ledit` insert two or more elements
- type changed to: "Bug"