<i>tclEncoding.c: undefined behavior involving NULL pointers</i> status still Open with 4 other changes
by
.
D 2023-03-26T09:48:01.211
J comment Passing\s<tt>NULL</tt>\sas\sthe\s<tt>src</tt>\sargument\sto\sconversion\sfunctions\s<tt>UtfToUtfProc()</tt>\s(used\sby\s<tt>UtfIntToUtfExtProc()</tt>\sand\s<tt>UtfExtToUtfIntProc()</tt>),\s<tt>UtfToUnicodeProc()</tt>,\s<tt>UnicodeToUtfProc()</tt>,\s<tt>Iso88591ToUtfProc()</tt>,\s<tt>Iso88591FromUtfProc()</tt>,\s<tt>EscapeToUtfProc()</tt>,\s<tt>EscapeFromUtfProc()</tt>,\s<tt>TableToUtfProc()</tt>,\sand\s<tt>TableFromUtfProc()</tt>\sin\sgeneric/tclEncoding.c\striggers\sthe\sfollowing\sundefined\sbehaviors:\r\n\r\n<ul>\r\n<li><tt>NULL\s+\s0</tt>\spointer\sarithmetic\swhen\scomputing\s<tt>srcEnd\s=\ssrc\s+\ssrcLen</tt>\r\n</li>\r\n<li><tt>NULL\s<\sNULL</tt>\spointer\scomparison\sin\sthe\sloop\scondition\s<tt>src\s<\ssrcEnd</tt>\r\n</li>\r\n<li><tt>NULL\s-\sNULL</tt>\spointer\ssubtraction\swhen\scomputing\s<tt>*srcReadPtr\s=\ssrc\s-\ssrcStart</tt>\r\n</li></ul>\r\n\r\nAdditionally,\s<tt>BinaryProc()</tt>\suses\s<tt>memcpy()</tt>\sand\swill\scall\sit\swith\s<tt>NULL</tt>\sas\sthe\ssource,\swhich\sis\sundefined\seven\sfor\scopy\slength\sof\s0.\r\n\r\n[https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan]\scurrently\sonly\scomplains\sfor\spointer\sarithmetic\susing\s<tt>NULL</tt>\s(at\sleast\swhen\scompiled\swith\sClang\s<tt>-fsanitize=pointer-overflow</tt>),\sand\sfor\s<tt>memcpy()</tt>\swith\s<tt>NULL</tt>\ssource\s(when\scompiled\swith\sGCC\s<tt>-fsanitize=nonnull-attribute</tt>).\sI\sdo\snot\sobserve\sTcl\sbehaving\sunexpectedly\sin\sany\sof\sthese\scases.\r\n\r\nExample\sduring\sstartup\swhen\sthe\s<tt>TCL_LIBRARY</tt>\senvironment\svariable\sis\snot\sset,\s<tt>TclpInitLibraryPath()</tt>\s(unix/tclUnixInit.c)\scalls\s<tt>Tcl_ExternalToUtfDString()</tt>\swith\s<tt>src\s==\sNULL</tt>:\r\n\r\n<pre>\r\n$\sunset\sTCL_LIBRARY\r\n$\stclsh8.6\s/dev/null\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2300:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n</pre>\r\n\r\nMore\scomplete\sexample\susing\sC\s(not\saware\show\sto\saccomplish\sthis\sfrom\sTcl;\sI\sput\sthis\snear\sthe\send\sof\s<tt>Tcltest_Init()</tt>\sfor\slack\sof\sa\sbetter\splace):\r\n\r\n<pre>\r\n\s\s\s\sif\s(1)\s{\r\n#define\sMYENCODINGS_LEN\s6\r\n\tconst\schar\s*\smyencodings[MYENCODINGS_LEN]\s=\s{\r\n\t\t"identity",\t/*\sBinaryProc\s*/\r\n\t\t"utf-8",\t/*\sUtfToUtfProc\s(UtfExtToUtfIntProc,\sUtfIntToUtfExtProc)\s*/\r\n\t\t"unicode",\t/*\sUnicodeToUtfProc,\sUtfToUnicodeProc\s*/\r\n\t\t"iso8859-1",\t/*\sIso88591ToUtfProc,\sIso88591FromUtfProc\s*/\r\n\t\t"shiftjis",\t/*\sTableToUtfProc,\sTableFromUtfProc\s*/\r\n\t\t"iso2022"\t/*\sEscapeToUtfProc,\sEscapeFromUtfProc\s*/\r\n\t};\r\n\tint\si;\r\n\tfor\s(i\s=\s0;\si\s<\sMYENCODINGS_LEN;\si++)\s{\r\n\t\s\s\s\sTcl_DString\sds;\r\n\t\s\s\s\sTcl_DStringInit(&ds);\r\n\t\s\s\s\sTcl_Encoding\se\s=\sTcl_GetEncoding(interp,\smyencodings[i]);\r\n\t\s\s\s\sfprintf(stderr,\s"\\e[32m'%s'\\e[0m\\n",\sTcl_GetEncodingName(e));\r\n\t\s\s\s\sTcl_ExternalToUtfDString(e,\sNULL,\s0,\s&ds);\r\n\t\s\s\s\sTcl_DStringFree(&ds);\r\n\t\s\s\s\sTcl_UtfToExternalDString(e,\sNULL,\s0,\s&ds);\r\n\t\s\s\s\sTcl_DStringFree(&ds);\r\n\t\s\s\s\sTcl_FreeEncoding(e);\r\n\t}\r\n\s\s\s\s}\r\n</pre>\r\n\r\nOutput:\r\n\r\n<pre>\r\n$\sTCL_LIBRARY=\s./tcltest\s/dev/null\r\n<span\sstyle="color:green">'identity'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2138:5:\s<span\sstyle="color:red">runtime\serror:</span>\snull\spointer\spassed\sas\sargument\s2,\swhich\sis\sdeclared\sto\snever\sbe\snull</span>\r\n<span\sstyle="color:green">'utf-8'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2299:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="color:green">'unicode'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2475:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2562:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="color:green">'iso8859-1'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2913:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2995:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="color:green">'shiftjis'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2683:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:2797:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="color:green">'iso2022'</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:3154:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n<span\sstyle="font-weight:bold">tcl/generic/tclEncoding.c:3364:18:\s<span\sstyle="color:red">runtime\serror:</span>\sapplying\szero\soffset\sto\snull\spointer</span>\r\n</pre>\r\n\r\nSome\spossible\sstyles\sfor\ssuggested\sfixes:\seither\shave\sfunctions\sexit\searly\swith\sdedicated\sblock\sduplicating\snecessary\sfinal\sstatements\s(not\sas\sfeasible\sfor\s<tt>EscapeFromUtfProc()</tt>);\suse\sa\ssingle\s<tt>NULL</tt>\scheck\sand\s<tt>goto</tt>\sto\sskip\sover\sanything\swhich\sassumes\s<tt>src</tt>\sis\snon-<tt>NULL</tt>;\sor\sadd\sinline\schecks\sfor\s<tt>NULL</tt>\swherever\s<tt>src</tt>\sis\sassumed\sto\sbe\snon-<tt>NULL</tt>.\sThe\slast\sapproach\sseems\snot\stoo\sinvasive:\sno\srequired\srearranging\sor\sduplication\sof\sexisting\scode,\sonly\sthing\sduplicated\sis\s<tt>NULL</tt>\scheck;\sthis\sis\swhat\sthe\sattached\spatch\suses.\r\n\r\nMy\sunderstanding\sis\sthat\sthese\sconversion\sfunctions\sare\sonly\sever\scalled\sby\s<tt>Tcl_ExternalToUtf()</tt>,\s<tt>Tcl_UtfToExternal()</tt>,\s<tt>Tcl_ExternalToUtfDString()</tt>,\sand\s<tt>Tcl_UtfToExternalDString()</tt>;\snotably\sthey\salready\sforce\s<tt>srcLen</tt>\sto\s0\swhen\s<tt>src</tt>\sis\s<tt>NULL</tt>\sand\sto\sa\snonnegative\samount\sotherwise.\sSo\sI\sdo\snot\sexpect\sany\sproposed\sfixes\sto\schange\sthe\sactual\sbehavior.
J icomment Reuploaded\spatch\sto\sapply\scleanly\sfollowing\schanges\sin\s[be99e0b905a6].
J login chrstphrchvz
J mimetype text/x-fossil-wiki
K c9e68eb6ca40bb844b6c5d172a8e203f4b419857
U chrstphrchvz
Z 6041832489891d91c6bee52d7f002795