<i>Undefined pointer arithmetic in `INST_LREPLACE` when compiled `linsert`, `lreplace` or `ledit` insert two or more elements</i>.
by
.
D 2026-11-06T18:46:33.367
J assignee nobody
J closer nobody
J cmimetype text/x-markdown
J comment In\s`INST_LREPLACE`\s(`generic/tclExecute.c`),\swhich\scompiled\s`linsert`,\s`lreplace`\sand\s`ledit`\suse,\s`numNewElems`\sis\sa\s`size_t`,\sso\s`&OBJ_AT_DEPTH(numNewElems\s-\s1)`\sexpands\sto\s`tosPtr[-(numNewElems\s-\s1)]`\swith\san\sunsigned\soffset.\sWhenever\stwo\sor\smore\selements\sare\sinserted,\sthe\soffset\swraps\sand\sthe\spointer\sarithmetic\soverflows,\son\sboth\scalls\sof\s`Tcl_ListObjReplace`\s(shared\sand\sunshared\slist).\sThe\sresulting\saddress\sis\sthe\sright\sone\sand\sthe\sresult\sis\scorrect,\salso\son\san\s`-O2`\sbuild,\sbut\sthe\sarithmetic\sis\sundefined,\sand\sUBSan's\spointer-overflow\scheck\sstops\son\sit.\sThat\shides\sany\slater\sreport,\sand\son\sa\sUBSan\sbuild\sit\sstops\s`make\stest`\sbefore\sthe\sfirst\stest\sfile\sruns:\r\n\r\n\s\s\s\sapply\s{{l\si}\s{\slreplace\s$l\s$i\s$i\sx\sy\s}}\s{a\sb\sc}\s1\r\n\r\n\s\s\s\stclExecute.c:5574:21:\sruntime\serror:\saddition\sof\sunsigned\soffset\sto\s0x6110000063c0\soverflowed\sto\s0x6110000063b8\r\n\r\nTo\ssee\sit,\sbuild\swith\sclang's\s`-fsanitize=undefined`,\swhich\sincludes\sthe\spointer-overflow\scheck.\sFrom\sthe\stop\sof\sa\strunk\scheckout\s(tested\swith\sApple\sclang\s21\son\smacOS):\r\n\r\n\s\s\s\smkdir\sbuild-ubsan\s&&\scd\sbuild-ubsan\r\n\s\s\s\s../unix/configure\s--disable-shared\sCC=clang\s\\\r\n\s\s\s\s\s\s\s\sCFLAGS="-g\s-O1\s-fsanitize=undefined\s-fno-sanitize-recover=undefined"\s\\\r\n\s\s\s\s\s\s\s\sLDFLAGS="-fsanitize=undefined"\r\n\s\s\s\smake\stcltest\r\n\s\s\s\smake\stest\sTESTFLAGS="-file\sappend.test"\r\n\r\n\s\s\s\s.../generic/tclExecute.c:5574:21:\sruntime\serror:\saddition\sof\sunsigned\soffset\sto\s0x00099103c868\soverflowed\sto\s0x00099103c858\r\n\s\s\s\sSUMMARY:\sUndefinedBehaviorSanitizer:\sundefined-behavior\s.../generic/tclExecute.c:5574:21\r\n\s\s\s\smake:\s***\s[test-tcl]\sAbort\strap:\s6\r\n\r\nAny\stest\sfile\sgives\sthe\ssame\sresult,\sbecause\s`runAllTests`\sin\s`library/tcltest/tcltest.tcl`\sbuilds\seach\schild's\scommand\sline\swith\s`linsert\s$childargv\s0\s|\s$shell\s$file`\sbefore\sany\stest\sruns.\s`-fno-sanitize-recover=undefined`\smakes\sthe\sreport\sfatal.\sWithout\sit,\sUBSan\sprints\sthe\sreport\sonce\sper\slocation\sand\sthe\stests\sgo\son.\sRunning\sa\stest\sfile\sdirectly\swith\s`./tcltest\s../tests/lreplace.test`,\swithout\s`all.tcl`,\sstops\sat\s`lreplace-1.8`,\sthe\sfirst\stest\sthat\sinserts\smore\sthan\sone\selement.\r\n\r\nAffected:\s9.0.4\s(`INST_LREPLACE4`),\s9.1.0\sand\strunk\s(as\sof\s2026-11-06,\s[6a05bcdb32](https://core.tcl-lang.org/tcl/info/6a05bcdb32)),\sand\sthe\stip\sof\score-9-0-branch.\s8.6\sdoes\snot\shave\sthis\sinstruction.\s`lpop`\salso\scompiles\sto\sit,\sbut\salways\swithout\snew\selements,\sso\sit\sis\snot\saffected.
J foundin 9.0.4,\s9.1.0,\s9.1.1\strunk
J is_private 0
J login bef
J priority 5\sMedium
J private_contact 37918056d323dc69a2f76cecc85476189094f3b8
J resolution None
J severity Minor
J status Open
J submitter bef
J subsystem 17.\sCommands\sI-L
J title Undefined\spointer\sarithmetic\sin\s`INST_LREPLACE`\swhen\scompiled\s`linsert`,\s`lreplace`\sor\s`ledit`\sinsert\stwo\sor\smore\selements
J type Bug
K 94c6511e870541dd5c4fbf8af014a07868b30f37
U bef
Z fbde8a5029fbd93d1b75d728d3e32cd8