Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 10b2d91c07d60429ada59460ddb576cb45a91ef7c7a27295b9e1d02ff7e082af
Ticket: c9e68eb6ca40bb844b6c5d172a8e203f4b419857
tclEncoding.c: undefined behavior involving NULL pointers
User & Date: chrstphrchvz on 2023-03-26 09:48:01
Changes

  1. comment changed to:
    Passing <tt>NULL</tt> as the <tt>src</tt> argument to conversion functions <tt>UtfToUtfProc()</tt> (used by <tt>UtfIntToUtfExtProc()</tt> and <tt>UtfExtToUtfIntProc()</tt>), <tt>UtfToUnicodeProc()</tt>, <tt>UnicodeToUtfProc()</tt>, <tt>Iso88591ToUtfProc()</tt>, <tt>Iso88591FromUtfProc()</tt>, <tt>EscapeToUtfProc()</tt>, <tt>EscapeFromUtfProc()</tt>, <tt>TableToUtfProc()</tt>, and <tt>TableFromUtfProc()</tt> in generic/tclEncoding.c triggers the following undefined behaviors:
    
    <ul>
    <li><tt>NULL + 0</tt> pointer arithmetic when computing <tt>srcEnd = src + srcLen</tt>
    </li>
    <li><tt>NULL &lt; NULL</tt> pointer comparison in the loop condition <tt>src &lt; srcEnd</tt>
    </li>
    <li><tt>NULL - NULL</tt> pointer subtraction when computing <tt>*srcReadPtr = src - srcStart</tt>
    </li></ul>
    
    Additionally, <tt>BinaryProc()</tt> uses <tt>memcpy()</tt> and will call it with <tt>NULL</tt> as the source, which is undefined even for copy length of 0.
    
    [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan] currently only complains for pointer arithmetic using <tt>NULL</tt> (at least when compiled with Clang <tt>-fsanitize=pointer-overflow</tt>), and for <tt>memcpy()</tt> with <tt>NULL</tt> source (when compiled with GCC <tt>-fsanitize=nonnull-attribute</tt>). I do not observe Tcl behaving unexpectedly in any of these cases.
    
    Example during startup when the <tt>TCL_LIBRARY</tt> environment variable is not set, <tt>TclpInitLibraryPath()</tt> (unix/tclUnixInit.c) calls <tt>Tcl_ExternalToUtfDString()</tt> with <tt>src == NULL</tt>:
    
    <pre>
    $ unset TCL_LIBRARY
    $ tclsh8.6 /dev/null
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2300:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    </pre>
    
    More complete example using C (not aware how to accomplish this from Tcl; I put this near the end of <tt>Tcltest_Init()</tt> for lack of a better place):
    
    <pre>
        if (1) {
    #define MYENCODINGS_LEN 6
    	const char * myencodings[MYENCODINGS_LEN] = {
    		"identity",	/* BinaryProc */
    		"utf-8",	/* UtfToUtfProc (UtfExtToUtfIntProc, UtfIntToUtfExtProc) */
    		"unicode",	/* UnicodeToUtfProc, UtfToUnicodeProc */
    		"iso8859-1",	/* Iso88591ToUtfProc, Iso88591FromUtfProc */
    		"shiftjis",	/* TableToUtfProc, TableFromUtfProc */
    		"iso2022"	/* EscapeToUtfProc, EscapeFromUtfProc */
    	};
    	int i;
    	for (i = 0; i < MYENCODINGS_LEN; i++) {
    	    Tcl_DString ds;
    	    Tcl_DStringInit(&ds);
    	    Tcl_Encoding e = Tcl_GetEncoding(interp, myencodings[i]);
    	    fprintf(stderr, "\e[32m'%s'\e[0m\n", Tcl_GetEncodingName(e));
    	    Tcl_ExternalToUtfDString(e, NULL, 0, &ds);
    	    Tcl_DStringFree(&ds);
    	    Tcl_UtfToExternalDString(e, NULL, 0, &ds);
    	    Tcl_DStringFree(&ds);
    	    Tcl_FreeEncoding(e);
    	}
        }
    </pre>
    
    Output:
    
    <pre>
    $ TCL_LIBRARY= ./tcltest /dev/null
    <span style="color:green">'identity'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2138:5: <span style="color:red">runtime error:</span> null pointer passed as argument 2, which is declared to never be null</span>
    <span style="color:green">'utf-8'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2299:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="color:green">'unicode'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2475:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2562:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="color:green">'iso8859-1'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2913:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2995:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="color:green">'shiftjis'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2683:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:2797:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="color:green">'iso2022'</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:3154:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    <span style="font-weight:bold">tcl/generic/tclEncoding.c:3364:18: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
    </pre>
    
    Some possible styles for suggested fixes: either have functions exit early with dedicated block duplicating necessary final statements (not as feasible for <tt>EscapeFromUtfProc()</tt>); use a single <tt>NULL</tt> check and <tt>goto</tt> to skip over anything which assumes <tt>src</tt> is non-<tt>NULL</tt>; or add inline checks for <tt>NULL</tt> wherever <tt>src</tt> is assumed to be non-<tt>NULL</tt>. The last approach seems not too invasive: no required rearranging or duplication of existing code, only thing duplicated is <tt>NULL</tt> check; this is what the attached patch uses.
    
    My understanding is that these conversion functions are only ever called by <tt>Tcl_ExternalToUtf()</tt>, <tt>Tcl_UtfToExternal()</tt>, <tt>Tcl_ExternalToUtfDString()</tt>, and <tt>Tcl_UtfToExternalDString()</tt>; notably they already force <tt>srcLen</tt> to 0 when <tt>src</tt> is <tt>NULL</tt> and to a nonnegative amount otherwise. So I do not expect any proposed fixes to change the actual behavior.
    
  2. icomment:
    Reuploaded patch to apply cleanly following changes in [be99e0b905a6].
    
  3. login: "chrstphrchvz"
  4. mimetype: "text/x-fossil-wiki"