[namespace children] doesn't match non-glob patterns below the global namespace
Closed / Fixed63449c05144fa85be0217ee3f82a78c6605bbdd3Created 3 years agoUpdated 1 year ago
Descriptionplain2024-02-22 07:17:55
# Create namespaces
namespace eval a { namespace eval b {} }
# Good result with non-glob pattern, and global namespace
namespace children :: a ;# => ::a
# Good result with single-char glob pattern, and other namespace
namespace children a {[b]} ;# => ::a::b
# Good result because of glob char in pattern, and other namespace
namespace children a b* ;# => ::a::b
# Bad result with non-glob pattern, and other namespace
namespace children a b ;# => {}
In generic/tclNamespace.c, function NamespaceChildrenCmd (lines 2988-3108), Tcl_StringMatch will be called if a glob char is present in the pattern, otherwise the matching will be performed with a combination of strncmp() and Tcl_FindHashEntry:
line 3067: if ((pattern != NULL) && TclMatchIsTrivial(pattern)) {
The hash table lookup:
line 3075 or 3078: Tcl_FindHashEntry(&nsPtr->childTable, pattern+length) != NULL
These two chars might be added but they won't be taken into account when matching: lines 3053-3055:
if (nsPtr != globalNsPtr) {
TclDStringAppendLiteral(&buffer, "::");
}
With a quick hack patch:
-Tcl_FindHashEntry(&nsPtr->childTable, pattern+length) != NULL
+Tcl_FindHashEntry(&nsPtr->childTable, 2+pattern+length) != NULL
The child namespace can now be found:
namespace children a b ;# => ::a::b
However namespace ::a can no longer be found in the global namespace!
namespace children :: a ;# => {}
The two namespace separator ("::") chars are not being accounted for properly.
Referenced by5
9f26385a03Fix [63449c0514]: [namespace children] doesn't match non-glob patterns below the global namespace. check-in2024-12-16 09:59:12da751091af(backport): Fix [63449c0514]: [namespace children] doesn't match non-glob patterns below the global namespace. Also backport some formatting changes check-in2024-12-15 18:27:44d39828737cAdd explanation for fix for ticket [63449c0514] check-in2024-12-15 17:57:10b6ae5707d9Fix [63449c0514]: [namespace children] doesn't match non-glob patterns below the global namespace. Candidate for inclusion in Tcl 9.0.1 (to be discussed) check-in2024-12-14 23:38:22ee821ec00fProposed fix for [63449c0514]: [namespace children] doesn't match non-glob patterns below the global namespace check-in2024-12-13 14:53:09
stu Ticket [63449c0514]: 4 changes
[History diff]2024-02-22 07:18:54
stuplain2024-02-22 07:32:03
This patch handles global and non-global. q&d poc -Tcl_FindHashEntry(&nsPtr->childTable, 2+pattern+length) != NULL +Tcl_FindHashEntry(&nsPtr->childTable, (nsPtr != globalNsPtr ? 2 : 0)+pattern+length) != NULL
stuplain2024-02-22 07:33:25
Correction: -Tcl_FindHashEntry(&nsPtr->childTable, pattern+length) != NULL +Tcl_FindHashEntry(&nsPtr->childTable, (nsPtr != globalNsPtr ? 2 : 0)+pattern+length) != NULL
stu Ticket [63449c0514]: 3 changes
[History diff]2024-06-11 20:42:31
stuplain2024-12-02 15:02:07
The patch is just a suggestion. The problem is an offset of 2 is not added when it should be. I'm not familiar enough with the code to pick the best place to do this.
jan.nijtmanswiki2024-12-13 15:06:13
Proposed fix [here]
jan.nijtmanswiki2024-12-14 23:40:59
The CI build shows no regressions. So, Yes I think this should be included into Tcl 9.0.2 (to be discussed within the TTT).
Was this problem introduced in Tcl 8.7 or 9.0, or was it always there? Should it be backported to 8.7 and/or 8.6? Therefore, leaving this ticket open. No hurry to decide that.
oehhar (claiming to be oehar)wiki2024-12-15 10:04:56
Great ! I would love some explanatory text around the fix source. Maybe, I care tomorrow !
And it is 9.0.1, not 9.0.2, right ?
Take care, Harald
dgpplain2025-07-31 18:45:11
Backport landed on the 8.6 branch last December, so I guess the decision got made. Closing.
Log in (anonymous is fine) to add a comment.