Skip to content
Tcl Source Code Artifact [4193cd7082]
Login

Artifact [4193cd7082]

Artifact 4193cd708202c56793b58668384c1e0ce77938a171e39aeea2bdadcd891b8fed:

Ticket change [4193cd7082] - New ticket [1c7f179710d5dd79|1c7f179710] <i>undefined behavior for INST_LSHIFT in ExecuteExtendedBinaryMathOp()</i>. by chrstphrchvz on 2022-02-18 03:43:30.
D 2022-02-18T03:43:30.394
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-wiki
J comment Analogous\sto\s[b0f84119c8]\sfor\sleft-shifting\sa\svalue\srepresented\sby\sa\s<tt>Tcl_WideInt</tt>\s(and\snot\sa\s<tt>long</tt>\sor\sa\sbignum)\swhen\s<tt>Tcl_WideInt</tt>\sis\s<tt>long\slong</tt>\s(because\s<tt>long</tt>\sis\sonly\s32-bit):\r\n\r\n<ul><li>Left-shifting\sby\s0\scauses\ssigned\sinteger\soverflow\s(undefined\sbehavior\sdetected\sby\sUBSan\s<tt>-fsanitize=shift-base</tt>)\sin\sthe\sexpression:\r\n\r\n<blockquote><pre>\r\n\s\s\s\s((Tcl_WideInt)1)\s&lt;&lt;\s(CHAR_BIT*sizeof(Tcl_WideInt)\s-\s1\s-\sshift)\r\n</pre></blockquote>\r\n\r\ni.e.\s1\sis\sshifted\sleft\s(64-1-0)=63\stimes,\swhich\soverflows;\sand\ssigned\sinteger\soverflow\s(undefined\sbehavior\sdetected\sby\sUBSan\s<tt>-fsanitize=signed-integer-overflow</tt>)\swhen\sthe\soverflowed\sresult\s<tt>WIDE_MIN</tt>\sof\sthe\sabove\sexpression\sis\snegated.</li>\r\n\r\n<li>If\sthe\sleft-shifted\svalue\sis\snegative,\sthen\sthere\sis\sleft-shifting\sof\sa\snegative\svalue\s(undefined\sbehavior\sdetected\sby\sUBSan\s<tt>-fsanitize=shift-base</tt>).</li></ul>\r\n\r\nExample\scommand\s(left\soperand\sis\sin\sthe\srange\s&#91;<tt>WIDE_MIN</tt>,<tt>LONG_MIN</tt>)\s&cup;\s(<tt>LONG_MAX</tt>,<tt>WIDE_MAX</tt>&#93;);\sthe\stest\ssuite\sdoes\snot\scontain\san\saffected\sexample):\r\n\r\n<blockquote><pre>\r\n%\s::tcl::mathop::&lt;&lt;\s-0x80000001\s0\r\n<span\sstyle="font-weight:\sbold">tcl/generic/tclExecute.c:8665:4:<span\sstyle="color:firebrick">\sruntime\serror:</span>\sleft\sshift\sof\s1\sby\s63\splaces\scannot\sbe\srepresented\sin\stype\s'long\slong\sint'</span>\r\n<span\sstyle="font-weight:\sbold">tcl/generic/tclExecute.c:8664:6:<span\sstyle="color:firebrick">\sruntime\serror:\s</span>negation\sof\s-9223372036854775808\scannot\sbe\srepresented\sin\stype\s'long\slong\sint';\scast\sto\san\sunsigned\stype\sto\snegate\sthis\svalue\sto\sitself</span>\r\n<span\sstyle="font-weight:\sbold">tcl/generic/tclExecute.c:8666:7:<span\sstyle="color:firebrick">\sruntime\serror:\s</span>left\sshift\sof\snegative\svalue\s-2147483649</span>\r\n-2147483649\r\n</pre></blockquote>\r\n\r\nThe\sattached\spatch\savoids\sthis\sby\scasting\sto\s<tt>Tcl_WideUInt</tt>\sin\sa\scouple\sof\splaces\sbefore\sshifting.
J foundin core-8-6-branch
J is_private 0
J login chrstphrchvz
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J submitter chrstphrchvz
J subsystem 48.\sNumber\sHandling
J title undefined\sbehavior\sfor\sINST_LSHIFT\sin\sExecuteExtendedBinaryMathOp()
J type Patch
K 1c7f179710d5dd791f4a161f2e6d34040d17bf4e
U chrstphrchvz
Z 26b9c314109bc7eb623a99d8c66153d7