Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39a6ee2

Browse files
committed
Issue #16626: Fix infinite recursion in glob.glob() on Windows when the pattern contains a wildcard in the drive or UNC path.
Patch by Serhiy Storchaka.
2 parents 09bc642 + 3c331bb commit 39a6ee2

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/glob.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def iglob(pathname):
2828
if not dirname:
2929
yield from glob1(None, basename)
3030
return
31-
if has_magic(dirname):
31+
# `os.path.split()` returns the argument itself as a dirname if it is a
32+
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
33+
# contains magic characters (i.e. r'\\?\C:').
34+
if dirname != pathname and has_magic(dirname):
3235
dirs = iglob(dirname)
3336
else:
3437
dirs = [dirname]

Lib/test/test_glob.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import glob
55
import os
66
import shutil
7+
import sys
78

89

910
class GlobTests(unittest.TestCase):
@@ -110,6 +111,18 @@ def test_glob_broken_symlinks(self):
110111
eq(self.glob('sym1'), [self.norm('sym1')])
111112
eq(self.glob('sym2'), [self.norm('sym2')])
112113

114+
@unittest.skipUnless(sys.platform == "win32", "Win32 specific test")
115+
def test_glob_magic_in_drive(self):
116+
eq = self.assertSequencesEqual_noorder
117+
eq(glob.glob('*:'), [])
118+
eq(glob.glob(b'*:'), [])
119+
eq(glob.glob('?:'), [])
120+
eq(glob.glob(b'?:'), [])
121+
eq(glob.glob('\\\\?\\c:\\'), ['\\\\?\\c:\\'])
122+
eq(glob.glob(b'\\\\?\\c:\\'), [b'\\\\?\\c:\\'])
123+
eq(glob.glob('\\\\*\\*\\'), [])
124+
eq(glob.glob(b'\\\\*\\*\\'), [])
125+
113126

114127
def test_main():
115128
run_unittest(GlobTests)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ Core and Builtins
167167
Library
168168
-------
169169

170+
- Issue #16626: Fix infinite recursion in glob.glob() on Windows when the
171+
pattern contains a wildcard in the drive or UNC path. Patch by Serhiy
172+
Storchaka.
173+
170174
- Issue #15783: Except for the number methods, the C version of decimal now
171175
supports all None default values present in decimal.py. These values were
172176
largely undocumented.

0 commit comments

Comments
 (0)