Tcl Source Code

View Ticket
Login
Ticket UUID: c9e68eb6ca40bb844b6c5d172a8e203f4b419857
Title: tclEncoding.c: undefined behavior involving NULL pointers
Type: Patch Created on: 2021-12-20 18:53:45
Submitter: chrstphrchvz Assigned to: apnadkarni
Subsystem: 52. Portability Support Severity: Minor
Priority: 5 Medium Last modified: 2025-09-24 03:12:17
Status: Closed Closed by: apnadkarni
Resolution: Fixed Closed on: 2025-09-24 03:12:17
Version: 8.6.12
Description:

Passing NULL as the src argument to conversion functions UtfToUtfProc() (used by UtfIntToUtfExtProc() and UtfExtToUtfIntProc()), UtfToUnicodeProc(), UnicodeToUtfProc(), Iso88591ToUtfProc(), Iso88591FromUtfProc(), EscapeToUtfProc(), EscapeFromUtfProc(), TableToUtfProc(), and TableFromUtfProc() in generic/tclEncoding.c triggers the following undefined behaviors:

  • NULL + 0 pointer arithmetic when computing srcEnd = src + srcLen
  • NULL < NULL pointer comparison in the loop condition src < srcEnd
  • NULL - NULL pointer subtraction when computing *srcReadPtr = src - srcStart

Additionally, BinaryProc() uses memcpy() and will call it with NULL as the source, which is undefined even for copy length of 0.

UBSan currently only complains for pointer arithmetic using NULL (at least when compiled with Clang -fsanitize=pointer-overflow), and for memcpy() with NULL source (when compiled with GCC -fsanitize=nonnull-attribute). I do not observe Tcl behaving unexpectedly in any of these cases.

Example during startup when the TCL_LIBRARY environment variable is not set, TclpInitLibraryPath() (unix/tclUnixInit.c) calls Tcl_ExternalToUtfDString() with src == NULL:

$ unset TCL_LIBRARY
$ tclsh8.6 /dev/null
tcl/generic/tclEncoding.c:2300:18: runtime error: applying zero offset to null pointer

More complete example using C (not aware how to accomplish this from Tcl; I put this near the end of Tcltest_Init() for lack of a better place):

    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);
	}
    }

Output:

$ TCL_LIBRARY= ./tcltest /dev/null
'identity'
tcl/generic/tclEncoding.c:2138:5: runtime error: null pointer passed as argument 2, which is declared to never be null
'utf-8'
tcl/generic/tclEncoding.c:2299:18: runtime error: applying zero offset to null pointer
'unicode'
tcl/generic/tclEncoding.c:2475:18: runtime error: applying zero offset to null pointer
tcl/generic/tclEncoding.c:2562:18: runtime error: applying zero offset to null pointer
'iso8859-1'
tcl/generic/tclEncoding.c:2913:18: runtime error: applying zero offset to null pointer
tcl/generic/tclEncoding.c:2995:18: runtime error: applying zero offset to null pointer
'shiftjis'
tcl/generic/tclEncoding.c:2683:18: runtime error: applying zero offset to null pointer
tcl/generic/tclEncoding.c:2797:18: runtime error: applying zero offset to null pointer
'iso2022'
tcl/generic/tclEncoding.c:3154:18: runtime error: applying zero offset to null pointer
tcl/generic/tclEncoding.c:3364:18: runtime error: applying zero offset to null pointer

Some possible styles for suggested fixes: either have functions exit early with dedicated block duplicating necessary final statements (not as feasible for EscapeFromUtfProc()); use a single NULL check and goto to skip over anything which assumes src is non-NULL; or add inline checks for NULL wherever src is assumed to be non-NULL. The last approach seems not too invasive: no required rearranging or duplication of existing code, only thing duplicated is NULL check; this is what the attached patch uses.

My understanding is that these conversion functions are only ever called by Tcl_ExternalToUtf(), Tcl_UtfToExternal(), Tcl_ExternalToUtfDString(), and Tcl_UtfToExternalDString(); notably they already force srcLen to 0 when src is NULL and to a nonnegative amount otherwise. So I do not expect any proposed fixes to change the actual behavior.

User Comments:
chrstphrchvz added on 2023-03-26 09:48:01:

Reuploaded patch to apply cleanly following changes in [be99e0b905a6].


chrstphrchvz added on 2025-09-20 20:52:41:

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;

apnadkarni added on 2025-09-21 15:55:31:
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

apnadkarni added on 2025-09-21 17:02:15:

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.


apnadkarni added on 2025-09-22 10:38:12:

Proposed fix in bug-c9e68eb6ca branch.


jan.nijtmans added on 2025-09-22 17:58:12:

Fix looks good to me.

Thanks!


apnadkarni added on 2025-09-23 12:20:42:
Fixed in [784ba203b5]. Checks add for getenv/GetEnvironmentVariable return values

cjmcdonald added on 2025-09-23 22:54:11:

In win/tclWinInit.c the dw result from GetEnvironmentVariableW should be checked against the size of "wBuf", not "buf", i.e.:

	dw >= (sizeof(wBuf) / sizeof(wBuf[0]))
Or simply:
	dw >= MAX_PATH
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.


apnadkarni added on 2025-09-24 03:12:17:
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.

Attachments: