Tcl Source Code

View Ticket
Login
Ticket UUID: 7eb6b3a53f963364c3c6f29d7622d70ddf8dd610
Title: glob on UNC path requires additional slash
Type: Bug Created on: 2026-05-29 07:48:54
Submitter: anonymous Assigned to: nobody
Subsystem: 36. Pathname Management Severity: Important
Priority: 5 Medium Last modified: 2026-05-31 16:18:59
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.4-9.1
Description:
Glob on windows unc path sometimes works incorrectly:

% llength [glob //?/C:/WINDOWS/*]
112
% llength [glob {\\?\C:\WINDOWS\*}]
no files matched glob pattern "\\?\C:\WINDOWS\*"
% llength [glob "\\\\?\\C:\\WINDOWS\\*"]
no files matched glob pattern "\\?\C:\WINDOWS\*"

With additional slash:

% llength [glob {\\?\C:\WINDOWS\\*}]
112
% llength [glob "\\\\?\\C:\\WINDOWS\\\\*"]
112

(latest tcl 9.1 from repo on windows 10)

This behavior seems inconsistent.
User Comments:
oehhar added on 2026-05-29 08:43:22:

Bug confirmed. Great catch! Harald


apnadkarni added on 2026-05-29 11:54:36:

That is not a UNC path. As far as I can see, UNC paths work correctly.

% llength [glob //IO/src/*]
124
% llength [glob {\\IO\src\*}]
124
% llength [glob "\\\\IO\\src\\*"]
124

The \\?\ notation follows different syntax rules and semantics. For example, C:\foo\..\bar and \\?\C:\foo\..\bar are different paths as are C:\foo/bar and \\?\C:\foo/bar, or C:\foo\bar. and \\?\C:\foo\bar.

Tcl does not really support the \\?\ syntax. There are some hacks (sorry, but they really are hacks) within Tcl that attempt to deal with this in piecemeal fashion. They lead you to believe that syntax is supported but it isn't. Moreover, in my opinion this is actually not fixable because not even all Win32 API's that Tcl uses understand the syntax.

See bc7925fc50 for other cases.


anonymous added on 2026-05-29 12:26:41:
Same behaviour on network path:

% llength [glob {\\srv\share\tmp\\*}]
132
% llength [glob {\\srv\share\tmp\*}]
no files matched glob pattern "\\srv\share\tmp\*"

I'm using native windows shares from windows domain

anonymous added on 2026-05-29 12:34:11:
Hmm, works great in the top directory

% llength [glob {\\srv\share\\*}]
31
% llength [glob {\\srv\share\*}]
31

apnadkarni added on 2026-05-29 13:12:28:
Indubitably a bug then :-)

Also present in 8.6

apnadkarni added on 2026-05-31 04:40:03:

Reversing myself yet again, I think works as designed, confusing as it might be.

When globbing a component like Windows\*, the glob syntax will treat \ as escaping the following character, the *. So it is looking actually looking for the string Windows* (i.e. * is a literal character in the pattern, not a wildcard) which fails. The additional \ is needed to escape the \.

The confusing part is why the \ in the UNC leading part does not need to be doubled. Tcl parses the path into a filesystem component and a pattern. The driver for the owning filesystem is then passed the pattern.

The filesystem component itself is not treated as a pattern. In the following:

glob {\\io\src\tcl-cffi\*}

The file system component is parsed as \\io\src\. The pattern is tcl-cffi\*. The asterisk then treated not as a wildcard but an escaped literal character and no file names are returned.

The reason top level wildcards as in \\io\src\* work is that again the file system component is \\io\src\ and the pattern is simply * (unescaped).

The same issue arises with simple drive paths as well, only even more confusing :-) because of the potential for drive-relative paths. The path C:/* is treated as file system C:/ and pattern *. Is the path C:\* to be treated likewise or as a volume relative path with file system C: and an escaped \* literal asterisk? It appears the latter option is chosen so glob {C:\*} will return no files.

I'm not sure there is a satisfactory solution to all this due to the inherent ambiguities. All I can suggest is to use Tcl canonical (forward slash) patterns as far as possible.

Above analysis is not authoritative :-) so leaving ticket open.


oehhar added on 2026-05-31 16:18:59:

At least this is reasonable, thanks Ashok for looking into this.

Perhaps we can add something to the documentation:

Native Windows path ({C:\test}) may only be used for the -directory and -path options. The glob pattern argument should use the forward slash as separator for unique results. Background is the ambiguity of the backslash character, which may escape a glob character ("\*" to specify a literal "*" character). So, for "glob {C:\test\*}", it is not clear, if the last backslash is a path separator or an escaped glob character.

Take care, Harald