.
D 2021-12-06T08:17:56.619
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-wiki
J comment (Note\sthat\sI\sdo\snot\sobserve\sTcl\sbehaving\sunexpectedly\sdespite\sthis\scomplaint.)\r\n\r\nThe\sC\sstandard\s(at\sleast\ssince\sC89)\ssays\sthat\sshifting\sby\sthe\swidth\sof\sthe\sleft\soperand\s(after\spromotion)\sor\smore\sis\sundefined\sbehavior.\sNo\sexception\sis\smade\sfor\sthe\sleft\soperand\sbeing\s0.\sSo\sI\sbelieve\s[https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan\s(<tt>-fsanitize=shift-exponent</tt>)]\sis\scorrect\sto\scomplain\sabout\scertain\sshifts\sin\s<tt>TclParseNumber()</tt>\s(generic/tclStrToD.c)\swhen\sparsing\snumbers\scontaining\senough\sleading\szeros.\sExample\scomplaints\sfor\s64-bit\s<tt>Tcl_WideUInt</tt>\s(note\sthat\sthey\smay\sonly\sappear\sonce\sover\sthe\slifetime\sof\sthe\sinterpreter):\r\n\r\n<pre>\r\n#\stcl/generic/tclStrToD.c:714:27:\sruntime\serror:\sshift\sexponent\s66\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nscan\s"[join\s[lrepeat\s22\s0]\s{}]1"\s{%i}\r\n\r\n#\stcl/generic/tclStrToD.c:714:27:\sruntime\serror:\sshift\sexponent\s66\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nscan\s"[join\s[lrepeat\s22\s0]\s{}]1"\s{%o}\r\n\r\n#\stcl/generic/tclStrToD.c:830:42:\sruntime\serror:\sshift\sexponent\s64\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nscan\s"[join\s[lrepeat\s16\s0]\s{}]1"\s{%x}\r\n\r\n#\stcl/generic/tclStrToD.c:872:42:\sruntime\serror:\sshift\sexponent\s64\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nscan\s"[join\s[lrepeat\s64\s0]\s{}]1"\s{%b}\r\n\r\n#\stcl/generic/tclStrToD.c:1205:23:\sruntime\serror:\sshift\sexponent\s64\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nexpr\s"0b[join\s[lrepeat\s64\s0]\s{}]"\r\n\r\n#\stcl/generic/tclStrToD.c:1226:23:\sruntime\serror:\sshift\sexponent\s64\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nexpr\s"0x[join\s[lrepeat\s16\s0]\s{}]"\r\n\r\n#\stcl/generic/tclStrToD.c:1248:28:\sruntime\serror:\sshift\sexponent\s66\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nexpr\s"[join\s[lrepeat\s23\s0]\s{}]"\r\n\r\n#\stcl/generic/tclStrToD.c:1248:28:\sruntime\serror:\sshift\sexponent\s66\sis\stoo\slarge\sfor\s64-bit\stype\s'Tcl_WideUInt'\s(aka\s'unsigned\slong')\r\nexpr\s"0o[join\s[lrepeat\s22\s0]\s{}]"\r\n</pre>\r\n\r\n<hr\s/>\r\nTo\saddress\sthese,\sI\swould\sreplace\se.g.:\r\n<pre>\r\noctalSignificandWide\s=\s(octalSignificandWide\s<<\sshift)\s+\s(c\s-\s'0')\r\n</pre>\r\n\r\nwith:\r\n<pre>\r\n/*\r\n\s*\sWhen\sthe\ssignificand\sis\s0,\sit\sis\spossible\sfor\sthe\r\n\s*\samount\sto\sbe\sshifted\sto\sequal\sor\sexceed\sthe\swidth\r\n\s*\sof\sthe\ssignificand.\sDo\snot\sshift\swhen\sthe\r\n\s*\ssignificand\sis\s0\sto\savoid\sundefined\sbehavior.\s\r\n\s*/\r\n\r\nif\s(octalSignificandWide\s!=\s0)\s{\r\n\s\s\s\soctalSignificandWide\s<<=\sshift;\r\n}\r\noctalSignificandWide\s+=\sc\s-\s'0';\r\n</pre>\r\n\r\nI\salso\ssuggest\srewording\sthe\scurrent\scomment\sfor\sthe\stoo-large\sshift\scheck\s(which\sappeared\sin\s[57a68b7c98]),\sbecause\sit\sis\sundefined\sbehavior\s(not\smerely\s“de\sfacto\snonportable”)\sto\sshift,\snot\sjust\sby\smore\sthan\swidth\sof\sa\svalue,\sbut\salso\sby\sthe\sexact\swidth\sof\sa\svalue.\r\n\r\nSee\sattached\spatch.
J foundin 8.6.12
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 11.\sConversions\sfrom\sString
J title Address\s-fsanitize=shift-exponent\scomplaints\sin\sTclParseNumber()
J type Patch
K cea2c63928b19c7ade84cbb5304cc2a33514912a
U chrstphrchvz
Z 57e66ecaef665887c5d7fe886f41dab9