Skip to content
Tcl Source Code History Of Ticket c9e68eb6ca
Login

History Of Ticket c9e68eb6ca

Artifacts Associated With Ticket c9e68eb6ca

  1. Ticket change [87c7209699] (rid 196901) by chrstphrchvz on 2021-12-20 18:53:45:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized 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 assuming <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.
      
    5. foundin initialized to: "8.6.12"
    6. is_private initialized to: "0"
    7. login: "chrstphrchvz"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "chrstphrchvz"
    13. subsystem initialized to: "52. Portability Support"
    14. title initialized to:
      tclEncoding.c: undefined behavior involving NULL pointers
      
    15. type initialized to: "Patch"
  2. Add attachment "c9e68eb6ca.diff" [99bcd86548] (rid 196902) by chrstphrchvz on 2021-12-20 18:54:50.

  3. Ticket change [0221bedb0e] (rid 198191) by chrstphrchvz on 2022-02-16 23:18:31:

    1. login: "chrstphrchvz"
    2. mimetype: "text/plain"
  4. Add attachment "c9e68eb6ca.diff" [61665d55d2] (rid 210941) by chrstphrchvz on 2023-03-26 09:43:52.

  5. Ticket change [10b2d91c07] (rid 210942) by chrstphrchvz on 2023-03-26 09:48:01:

    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"
  6. Ticket change [f7014b3707] (rid 245696) by chrstphrchvz on 2025-09-20 20:52:41:

    1. icomment:
      The attached patch still applies cleanly to core-8-6-branch, but it seems like core-9-0-branch would need to be a separate effort.
      
      I am also wondering if there is a concern where `NULL != src` should instead not be part of the for loop condition, e.g.:
      
      ```
      @@ -3042,7 +3044,8 @@
           dstStart = dst;
           dstEnd = dst + dstLen - TCL_UTF_MAX;
       
      -    for (numChars = 0; src < srcEnd && numChars <= charLimit; src += 2, numChars++) {
      +    numChars = 0;
      +    if (NULL != src) for (; src < srcEnd && numChars <= charLimit; src += 2, numChars++) {
       	if (dst > dstEnd && !HIGH_SURROGATE(ch)) {
       	    result = TCL_CONVERT_NOSPACE;
       	    break;
      ```
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-markdown"
  7. Ticket change [91ba573a1d] (rid 245705) by apnadkarni on 2025-09-21 15:55:31:

    1. icomment:
      In general, the Tcl C API expects the caller to have checked for NULL pointers unless explicitly documented otherwise so I am not in favor of the proposed patch. Unless we make a universal change and modify the entire API surface accordingly.
      
      The real bug is the C code that does not do this check in tclUnixInit.c.
      
      /Ashok
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  8. Ticket change [7037472277] (rid 245706) by apnadkarni on 2025-09-21 17:02:15:

    1. icomment:
      Examining the code, seems the issue is with tclUnixInit.c not checking the return value from `getenv` and passing the NULL result into the decoding routines. And looking at the Windows version, that too ignores the status code of `GetEnvironmentVariable` as well as the status code from subsequent WideCharToMultiByte(). The difference is that because of the differing API (GetEnvironmentVariable accepts an output buffer instead of returning a pointer), what is passed into the decoding routines is not NULL but a pointer to complete garbage. Stuff works because garbage path does not exist and Tcl falls back to looking into the parent directory of the executable.
      
      Definitely something in need of fixing but not in the encoding routines.
      
      Thanks for reporting.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  9. Ticket change [5c1f20cee4] (rid 245731) by apnadkarni on 2025-09-22 10:38:12:

    1. icomment:
      Proposed fix in [bug-c9e68eb6ca](https://core.tcl-lang.org/tcl/timeline?r=bug-c9e68eb6ca) branch.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  10. Ticket change [056044201e] (rid 245747) by jan.nijtmans on 2025-09-22 17:58:12:

    1. assignee changed to: "apnadkarni"
    2. icomment:
      Fix looks good to me.
      
      Thanks!
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
  11. Ticket change [318c7d95c2] (rid 245784) by apnadkarni on 2025-09-23 12:20:42:

    1. closedate changed to: "2460942.0143862"
    2. closer changed to: "apnadkarni"
    3. icomment:
      Fixed in [784ba203b5]. Checks add for getenv/GetEnvironmentVariable return values
      
    4. login: "apnadkarni"
    5. mimetype: "text/x-fossil-plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
  12. Ticket change [d8119c274e] (rid 245850) by cjmcdonald on 2025-09-23 22:54:11:

    1. closedate changed to: "2460942.4543008"
    2. closer changed to: "cjmcdonald"
    3. icomment:
      In win/tclWinInit.c the dw result from GetEnvironmentVariableW should be checked against the size of "wBuf", not "buf", i.e.:
      <pre>
      	dw >= (sizeof(wBuf) / sizeof(wBuf[0]))
      </pre>
      Or simply:
      <pre>
      	dw >= MAX_PATH
      </pre>
      which I think is preferable because then the buffer size (in wide chars) is specified in the same form when calling GetEnvironmentVariableW and when checking its result.
      
    4. login: "cjmcdonald"
    5. mimetype: "text/x-fossil-wiki"
  13. Ticket change [17ca83eb42] (rid 245855) by apnadkarni on 2025-09-24 03:12:17:

    1. closedate changed to: "2460942.63354138"
    2. closer changed to: "apnadkarni"
    3. icomment:
      Indeed so :blush:. Thanks Colin.
      
      I generally prefer the sizeof idiom so any changes to the size in the declaration do not require modification of the references. But here for consistency with the rest of the code, agree with you MAX_PATH constant makes sense.
      
    4. login: "apnadkarni"
    5. mimetype: "text/plain"