| Ticket UUID: | 24d7f1a69572ef507638447eb87ccce0c9d489b9 | ||
| Title: | Recursive [namespace delete] causes a segfault | ||
| Type: | Bug | Created on: | 2025-12-06 22:56:14 |
| Submitter: | teclab | Assigned to: | jan.nijtmans |
| Subsystem: | 21. [namespace] | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2026-04-22 16:09:00 |
| Status: | Closed | Closed by: | dgp |
| Resolution: | Fixed | Closed on: | 2026-04-22 16:09:00 |
| Version: | core-8-6-branch | ||
| Description: | ||||
|
Dear TCL Team, might be an artifial problem, but this code below makes TCL crash:
That's how you call it:
Now a second time:
boom Thank You! | ||||
| User Comments: | ||||
pointsman added on 2025-12-07 15:04:17:
Confirmed and confirmed for 9.0.1 also. The stack trace with 9.0.1 (as reported by valgrind) is: Invalid read of size 8 at 0x489B624: TclCreateObjCommandInNs (tclBasic.c:2861) by 0x48A4457: TclNRCreateCommandInNs (tclBasic.c:8668) by 0x49F1320: Tcl_ProcObjCmd (tclProc.c:214) by 0x4897943: procObjCmd (tclBasic.c:308) by 0x489DBFB: Dispatch (tclBasic.c:4640) by 0x489DC5F: TclNRRunCallbacks (tclBasic.c:4656) by 0x489D510: Tcl_EvalObjv (tclBasic.c:4395) by 0x489FC98: TclEvalEx (tclBasic.c:5495) by 0x49C0D2E: Tcl_FSEvalFileEx (tclIOUtil.c:1793) by 0x49D23D1: Tcl_MainEx (tclMain.c:404) by 0x1091D3: main (tclAppInit.c:94) Address 0x110 is not stack'd, malloc'd or (recently) free'd Process terminating with default action of signal 11 (SIGSEGV) Access not within mapped region at address 0x110 at 0x489B624: TclCreateObjCommandInNs (tclBasic.c:2861) by 0x48A4457: TclNRCreateCommandInNs (tclBasic.c:8668) by 0x49F1320: Tcl_ProcObjCmd (tclProc.c:214) by 0x4897943: procObjCmd (tclBasic.c:308) by 0x489DBFB: Dispatch (tclBasic.c:4640) by 0x489DC5F: TclNRRunCallbacks (tclBasic.c:4656) by 0x489D510: Tcl_EvalObjv (tclBasic.c:4395) by 0x489FC98: TclEvalEx (tclBasic.c:5495) by 0x49C0D2E: Tcl_FSEvalFileEx (tclIOUtil.c:1793) by 0x49D23D1: Tcl_MainEx (tclMain.c:404) by 0x1091D3: main (tclAppInit.c:94) chw added on 2025-12-07 19:48:02:
What about this patch?
---snip---
Index: generic/tclNamesp.c
==================================================================
--- generic/tclNamesp.c
+++ generic/tclNamesp.c
@@ -2538,14 +2538,21 @@
TclEnsureNamespace(
Tcl_Interp *interp,
Tcl_Namespace *namespacePtr)
{
Namespace *nsPtr = (Namespace *) namespacePtr;
+ Tcl_Namespace *newNsPtr;
+
if (!(nsPtr->flags & NS_DYING)) {
- return namespacePtr;
+ return namespacePtr;
}
- return Tcl_CreateNamespace(interp, nsPtr->fullName, NULL, NULL);
+ newNsPtr = Tcl_CreateNamespace(interp, nsPtr->fullName, NULL, NULL);
+ if (newNsPtr == NULL) {
+ nsPtr->flags &= ~(NS_DYING|NS_KILLED);
+ newNsPtr = (Tcl_Namespace *) nsPtr;
+ }
+ return newNsPtr;
}
/*
*----------------------------------------------------------------------
*
---snip---
jan.nijtmans added on 2025-12-08 10:58:31:
Put in a [655eb6343f1c353f|branch] now. Please test (everyone who want/can). On first sight, it looks good to me. Thanks! chw added on 2025-12-10 04:57:31:
More and more my impression is that TclEnsureNamespace() introduced this regression. Despite its name this function does not ensure that the given namespace is kept, since in case it got the NS_DYING flag it usually cannot be recreated, and the function ends up with returning NULL, i.e. no namespace at all. Given that TclEnsureNamespace()'s sole call site is in tclBasic.c IMO this function should be ditched once and for good. chw added on 2025-12-10 05:37:02:
Please forget my last comment, it would re-introduce the
test failure in basic-15.2 which got fixed by TclEnsureNamespace()
according to ticket [0e4d88b6502ac8fe0ccc912c2bd2ef4c16279837].
Sorry for the noise!
apnadkarni added on 2026-04-20 16:54:24:
A simpler illustration of the same issue, just to reduce the noise
Before even attempting a fix, I'm not clear on what the semantics should be when (a) a namespace is deleted in the middle of its execution and (b) when it is recreated from within its own eval. apnadkarni added on 2026-04-21 11:53:06:
Proposed fix in bug-24d7f1a6-apn. Passes test and valgrind. Note the fix results in the namespace being recreated after deletion. I am not entirely sure that is the desirable behavior but that is how the current (non-crashing) test cases behave so I have preserved that. apnadkarni added on 2026-04-22 11:12:00:
Fixed for 8.6, 9.0 and 9.1. dgp added on 2026-04-22 16:09:00:
Namespace lifetime management has been fertile ground for crashes and bugs. It is hard to get right, and often hard to even define what "right" is. For whatever benefit it may bring to future developers, this particular segfault originates in checkin [4039b2b895]. Apply the patch to that checkin and it fixes the issue back then as well. | ||||
