|
2025-12-20
| ||
| 02:07 | • New ticket [983f933f11] source test triggers undefined behavior. artifact: 449953ad35 user: chrstphrchvz | |
|
2025-09-24
| ||
| 03:12 | • Ticket [c9e68eb6ca] tclEncoding.c: undefined behavior involving NULL pointers</i... artifact: 17ca83eb42 user: apnadkarni | |
|
2025-09-23
| ||
| 22:54 | • Ticket [c9e68eb6ca]: 5 changes artifact: d8119c274e user: cjmcdonald | |
| 12:20 | • Closed ticket [c9e68eb6ca]. artifact: 318c7d95c2 user: apnadkarni | |
| 12:19 | Merge 9.0 - Fix [c9e68eb6ca] getenv checks check-in: 4760a5fda3 user: apnadkarni tags: trunk, main | |
| 12:00 | Merge 8.6 - Fix [c9e68eb6ca] getenv checks check-in: 33587c757d user: apnadkarni tags: core-9-0-branch | |
| 11:09 | Merge mark - core-8-6-branch. Do not pick up fix for [c9e68eb6ca]. leaf check-in: 721c379877 user: apnadkarni tags: mistake | |
| 10:25 | Bug [c9e68eb6ca] - check result of getenv/GetEnvironmentVariable check-in: 9c4f3a6de8 user: apnadkarni tags: core-8-6-branch | |
|
2025-09-22
| ||
| 17:58 | • Ticket [c9e68eb6ca] tclEncoding.c: undefined behavior involving NULL pointers</i... artifact: 056044201e user: jan.nijtmans | |
| 10:38 | • Ticket [c9e68eb6ca]: 3 changes artifact: 5c1f20cee4 user: apnadkarni | |
| 10:34 | Bug [c9e68eb6ca] - Windows version closed check-in: 175029b594 user: apnadkarni tags: bug-c9e68eb6ca | |
| 10:09 | Bug [c9e68eb6ca] - check getenv result for TCL_LIBRARY check-in: 4d541791f6 user: apnadkarni tags: bug-c9e68eb6ca | |
|
2025-09-21
| ||
| 17:02 | • Ticket [c9e68eb6ca] tclEncoding.c: undefined behavior involving NULL pointers</i... artifact: 7037472277 user: apnadkarni | |
| 15:55 | • Ticket [c9e68eb6ca]: 3 changes artifact: 91ba573a1d user: apnadkarni | |
|
2025-09-20
| ||
| 20:52 | • Ticket [c9e68eb6ca]: 3 changes artifact: f7014b3707 user: chrstphrchvz | |
|
2023-03-26
| ||
| 09:48 | • Ticket [c9e68eb6ca]: 4 changes artifact: 10b2d91c07 user: chrstphrchvz | |
| 09:43 | • Add attachment c9e68eb6ca.diff to ticket [c9e68eb6ca] artifact: 61665d55d2 user: chrstphrchvz | |
|
2022-02-16
| ||
| 23:18 | • Ticket [c9e68eb6ca] tclEncoding.c: undefined behavior involving NULL pointers</i... artifact: 0221bedb0e user: chrstphrchvz | |
|
2021-12-20
| ||
| 18:54 | • Add attachment c9e68eb6ca.diff to ticket [c9e68eb6ca] artifact: 99bcd86548 user: chrstphrchvz | |
| 18:53 | • New ticket [c9e68eb6ca] tclEncoding.c: undefined behavior involving NULL pointer... artifact: 87c7209699 user: chrstphrchvz | |
tclEncoding.c: undefined behavior involving NULL pointers
- 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.
4760a5fda3Merge 9.0 - Fix [c9e68eb6ca] getenv checks check-in2025-09-23 12:19:1633587c757dMerge 8.6 - Fix [c9e68eb6ca] getenv checks check-in2025-09-23 12:00:19721c379877Merge mark - core-8-6-branch. Do not pick up fix for [c9e68eb6ca]. check-in2025-09-23 11:09:259c4f3a6de8Bug [c9e68eb6ca] - check result of getenv/GetEnvironmentVariable check-in2025-09-23 10:25:04175029b594Bug [c9e68eb6ca] - Windows version check-in2025-09-22 10:34:224d541791f6Bug [c9e68eb6ca] - check getenv result for TCL_LIBRARY check-in2025-09-22 10:09:29
Reuploaded patch to apply cleanly following changes in [be99e0b905a6].
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;
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
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.
Proposed fix in bug-c9e68eb6ca branch.
Fix looks good to me.
Thanks!
Fixed in [784ba203b5]. Checks add for getenv/GetEnvironmentVariable return values
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_PATHwhich 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.
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:
- c9e68eb6ca.diff [download] added by chrstphrchvz on 2023-03-26 09:43:52. [details]