Artifacts Associated With Ticket 94c6511e87
Ticket change [e149ab6796] (rid 261267) by bef on 2026-11-06 18:46:33:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized 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 initialized to: "9.0.4, 9.1.0, 9.1.1 trunk"
- is_private initialized to: "0"
- login: "bef"
- priority initialized to: "5 Medium"
- private_contact initialized to: "37918056d323dc69a2f76cecc85476189094f3b8"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "bef"
- subsystem initialized to: "17. Commands I-L"
- title initialized to:
Undefined pointer arithmetic in `INST_LREPLACE` when compiled `linsert`, `lreplace` or `ledit` insert two or more elements
- type initialized to: "Bug"
Ticket change [3a32de9be6] (rid 261272) by bef on 2026-11-06 18:59:05:
- icomment:
Fix on branch [bug-94c651-lreplace-pointer-overflow](/info/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.
- login: "bef"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [8044de5c4d] (rid 261280) by apnadkarni on 2026-11-07 11:43:01:
- closedate changed to: "2461351.9882061225"
- closer changed to: "apnadkarni"
- icomment:
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.
- login: "apnadkarni"
- mimetype: "text/x-fossil-plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"