Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-12-30
03:44
Merge 8.6 Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtf... check-in: c37c2cf1e3 user: apnadkarni tags: core-9-0-branch
03:26
Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in: 5b63b08a34 user: apnadkarni tags: core-8-6-branch
03:22 • Closed ticket [983f933f11]: source test triggers undefined behavior plus 6 o... artifact: 65615ba0cc user: apnadkarni
03:10
Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in: ea956d4e83 user: apnadkarni tags: trunk, main
2025-12-29
17:35 • Ticket [983f933f11] source test triggers undefined behavior status still Ope... artifact: 9398b05337 user: chrstphrchvz
2025-12-28
05:13 • Ticket [983f933f11]: 3 changes artifact: 32e0e04600 user: apnadkarni
05:06
Bug [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. closed check-in: 28845a8668 user: apnadkarni tags: bug-983f933f11
2025-12-26
23:06 • Ticket [983f933f11] source test triggers undefined behavior status still Ope... artifact: 622614b5bb user: chrstphrchvz
2025-12-20
02:07 • New ticket [983f933f11]. artifact: 449953ad35 user: chrstphrchvz

source test triggers undefined behavior

Closed / Fixed983f933f114af3a77d9df39359205811637ee2c2Created 348 days agoUpdated 338 days ago
Descriptionwiki2025-12-20 02:07:37
Example command (based on test source-7.4) and UBSan error:
% source -encoding bogus /dev/null
tcl91/generic/tclEncoding.c:2477:18: runtime error: applying zero offset to null pointer
tcl91/generic/tclEncoding.c:1236:6: runtime error: applying zero offset to null pointer
unknown encoding "bogus"

As with [c9e68eb6], this sort of problem is not because of functions where the undefined behavior occurs, but rather because of whatever is passing NULL to those functions. LLDB backtrace for first error:

  * frame #0: 0x0000000100557920 libclang_rt.asan_osx_dynamic.dylib`__ubsan_on_report
    frame #1: 0x0000000100552000 libclang_rt.asan_osx_dynamic.dylib`__ubsan::Diag::~Diag() + 240
    frame #2: 0x000000010055707a libclang_rt.asan_osx_dynamic.dylib`handlePointerOverflowImpl(__ubsan::PointerOverflowData*, unsigned long, unsigned long, __ubsan::ReportOptions) + 1066
    frame #3: 0x0000000100556c46 libclang_rt.asan_osx_dynamic.dylib`__ubsan_handle_pointer_overflow + 54
    frame #4: 0x0000000101a2fa27 libtcl9.1.dylib`UtfToUtfProc(clientData=0x0000000000000200, src=0x0000000000000000, srcLen=0, flags=16778243, statePtr=0x00007ff7bfefbb20, dst="", dstLen=199, srcReadPtr=0x00007ff7bfefbb40, dstWrotePtr=0x00007ff7bfefbb50, dstCharsPtr=0x00007ff7bfefbb60) at tclEncoding.c:2477:18
    frame #5: 0x0000000101a3e534 libtcl9.1.dylib`Tcl_ExternalToUtfDStringEx(interp=0x0000000000000000, encoding=0x0000607000000fe0, src=0x0000000000000000, srcLen=0, flags=16778243, dstPtr=0x00007ff7bfefbf20, errorLocPtr=0x0000000000000000) at tclEncoding.c:1231:11
    frame #6: 0x0000000101a3dce7 libtcl9.1.dylib`Tcl_ExternalToUtfDString(encoding=0x0000000000000000, src=0x0000000000000000, srcLen=0, dstPtr=0x00007ff7bfefbf20) at tclEncoding.c:1132:5
    frame #7: 0x0000000101f83f7b libtcl9.1.dylib`TclGetProcessGlobalValue(pgvPtr=0x000000010277f240) at tclUtil.c:4258:2
    frame #8: 0x0000000101a45ab0 libtcl9.1.dylib`OpenEncodingFileChannel(interp=0x0000619000003780, name="bogus") at tclEncoding.c:1768:20
    frame #9: 0x0000000101a3a774 libtcl9.1.dylib`LoadEncodingFile(interp=0x0000619000003780, name="bogus") at tclEncoding.c:1884:12
    frame #10: 0x0000000101a2bc33 libtcl9.1.dylib`Tcl_GetEncoding(interp=0x0000619000003780, name="bogus") at tclEncoding.c:757:12
    frame #11: 0x0000000101c0cdee libtcl9.1.dylib`Tcl_SetChannelOption(interp=0x0000619000003780, chan=0x00006060000b13e0, optionName="-encoding", newValue="bogus") at tclIO.c:8299:17
    frame #12: 0x0000000101cee7b5 libtcl9.1.dylib`TclNREvalFile(interp=0x0000619000003780, pathPtr=0x0000604000297f90, encodingName="bogus") at tclIOUtil.c:1879:9
    frame #13: 0x00000001017fc685 libtcl9.1.dylib`TclNRSourceObjCmd(dummy1170=0x0000000000000000, interp=0x0000619000003780, objc=4, objv=0x0000611000014ae0) at tclCmdMZ.c:1217:14
    frame #14: 0x00000001016535bb libtcl9.1.dylib`Dispatch(data=0x00006040002982d8, interp=0x0000619000003780, dummy4623=0) at tclBasic.c:4662:12
    frame #15: 0x0000000101635eed libtcl9.1.dylib`TclNRRunCallbacks(interp=0x0000619000003780, result=0, rootPtr=0x0000000000000000) at tclBasic.c:4678:11
    frame #16: 0x00000001016484f8 libtcl9.1.dylib`TclEvalObjEx(interp=0x0000619000003780, objPtr=0x00006040001cb210, flags=131072, invoker=0x0000000000000000, word=0) at tclBasic.c:6125:12
    frame #17: 0x0000000101648373 libtcl9.1.dylib`Tcl_EvalObjEx(interp=0x0000619000003780, objPtr=0x00006040001cb210, flags=131072) at tclBasic.c:6106:12
    frame #18: 0x0000000101bb91b2 libtcl9.1.dylib`Tcl_RecordAndEvalObj(interp=0x0000619000003780, cmdPtr=0x00006040001cb210, flags=131072) at tclHistory.c:182:11
    frame #19: 0x0000000101d86d81 libtcl9.1.dylib`Tcl_MainEx(argc=-1, argv=0x00007ff7bfeff560, appInitProc=(tclsh9.1`Tcl_AppInit at tclAppInit.c:124), interp=0x0000619000003780) at tclMain.c:523:13
    frame #20: 0x0000000100001e2d tclsh9.1`main(argc=1, argv=0x00007ff7bfeff560) at tclAppInit.c:98:5
    frame #21: 0x00007ff803fe8530 dyld`start + 3056
Referenced by4
  • c37c2cf1e3Merge 8.6 Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in2025-12-30 03:44:40
  • 5b63b08a34Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in2025-12-30 03:26:20
  • ea956d4e83Fix [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in2025-12-30 03:10:18
  • 28845a8668Bug [983f933f11] UBSan warnings with null pointer in TclGetProcessGlobal/Tcl_ExternalToUtfDString. check-in2025-12-28 05:06:58
chrstphrchvzmarkdown2025-12-26 23:06:52

Would something like the following be an appropriate fix?

--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -4255,8 +4255,12 @@ TclGetProcessGlobalValue(
 	 * in our epoch-indexed cache.
 	 */
 
-	Tcl_ExternalToUtfDString(NULL, pgvPtr->value, pgvPtr->numBytes, &newValue);
-	value = Tcl_DStringToObj(&newValue);
+	if (pgvPtr->value) {
+	    Tcl_ExternalToUtfDString(NULL, pgvPtr->value, pgvPtr->numBytes, &newValue);
+	    value = Tcl_DStringToObj(&newValue);
+	} else {
+	    TclNewObj(value);
+	}
 	hPtr = Tcl_CreateHashEntry(cacheMap,
 		INT2PTR(pgvPtr->epoch), NULL);
 	Tcl_MutexUnlock(&pgvPtr->mutex);
apnadkarnimarkdown2025-12-28 05:13:48

Christopher,

Could you please retest with the bug-983f933f11 branch? I could not verify myself as I could not reproduce the warnings.

What were the configure flags used? I tried the following and could not reproduce the error on Ubuntu 20.

../../configure CFLAGS="-fsanitize=undefined,pointer-overflow" LDFLAGS="-fsanitize=undefined,pointer-overflow"
make -s shell
make -s test TESTFLAGS='-f source.test'

Also, I presume you were using the trunk for the build (the ticket says core-8-6-branch) but the stack dump shows 9.1.

With regards to your proposed patch, it may fix the warnings in this case but not in cases where Tcl_ExternalToUtfDString is called with NULL pointers from elsewhere so not sure if it is adequate. Fixing Tcl_ETUDS feels safer assuming the branch resolves the issue.

chrstphrchvzwiki2025-12-29 17:35:37

I did verify the issue exists in 8.6, and that the bug-983f933f11 branch resolves the issue. The lack of offset with NULL pointer UBSan error could be due to the compiler used; I have only seen it when using sufficiently recent versions of Clang, and not under GCC.

apnadkarniplain2025-12-30 03:22:10
Fixed in [ea956d4e83].
Log in (anonymous is fine) to add a comment.