Skip to content

Commit 5dceb93

Browse files
[3.11] gh-136065: Fix quadratic complexity in os.path.expandvars() (GH-134952) (GH-140848)
(cherry picked from commit f029e8d) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent a18b381 commit 5dceb93

File tree

5 files changed

+94
-112
lines changed

5 files changed

+94
-112
lines changed

Lib/ntpath.py

Lines changed: 41 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,23 @@ def expanduser(path):
378378
# XXX With COMMAND.COM you can use any characters in a variable name,
379379
# XXX except '^|<>='.
380380

381+
_varpattern = r"'[^']*'?|%(%|[^%]*%?)|\$(\$|[-\w]+|\{[^}]*\}?)"
382+
_varsub = None
383+
_varsubb = None
384+
381385
def expandvars(path):
382386
"""Expand shell variables of the forms $var, ${var} and %var%.
383387
384388
Unknown variables are left unchanged."""
385389
path = os.fspath(path)
390+
global _varsub, _varsubb
386391
if isinstance(path, bytes):
387392
if b'$' not in path and b'%' not in path:
388393
return path
389-
import string
390-
varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
391-
quote = b'\''
394+
if not _varsubb:
395+
import re
396+
_varsubb = re.compile(_varpattern.encode(), re.ASCII).sub
397+
sub = _varsubb
392398
percent = b'%'
393399
brace = b'{'
394400
rbrace = b'}'
@@ -397,94 +403,44 @@ def expandvars(path):
397403
else:
398404
if '$' not in path and '%' not in path:
399405
return path
400-
import string
401-
varchars = string.ascii_letters + string.digits + '_-'
402-
quote = '\''
406+
if not _varsub:
407+
import re
408+
_varsub = re.compile(_varpattern, re.ASCII).sub
409+
sub = _varsub
403410
percent = '%'
404411
brace = '{'
405412
rbrace = '}'
406413
dollar = '$'
407414
environ = os.environ
408-
res = path[:0]
409-
index = 0
410-
pathlen = len(path)
411-
while index < pathlen:
412-
c = path[index:index+1]
413-
if c == quote: # no expansion within single quotes
414-
path = path[index + 1:]
415-
pathlen = len(path)
416-
try:
417-
index = path.index(c)
418-
res += c + path[:index + 1]
419-
except ValueError:
420-
res += c + path
421-
index = pathlen - 1
422-
elif c == percent: # variable or '%'
423-
if path[index + 1:index + 2] == percent:
424-
res += c
425-
index += 1
426-
else:
427-
path = path[index+1:]
428-
pathlen = len(path)
429-
try:
430-
index = path.index(percent)
431-
except ValueError:
432-
res += percent + path
433-
index = pathlen - 1
434-
else:
435-
var = path[:index]
436-
try:
437-
if environ is None:
438-
value = os.fsencode(os.environ[os.fsdecode(var)])
439-
else:
440-
value = environ[var]
441-
except KeyError:
442-
value = percent + var + percent
443-
res += value
444-
elif c == dollar: # variable or '$$'
445-
if path[index + 1:index + 2] == dollar:
446-
res += c
447-
index += 1
448-
elif path[index + 1:index + 2] == brace:
449-
path = path[index+2:]
450-
pathlen = len(path)
451-
try:
452-
index = path.index(rbrace)
453-
except ValueError:
454-
res += dollar + brace + path
455-
index = pathlen - 1
456-
else:
457-
var = path[:index]
458-
try:
459-
if environ is None:
460-
value = os.fsencode(os.environ[os.fsdecode(var)])
461-
else:
462-
value = environ[var]
463-
except KeyError:
464-
value = dollar + brace + var + rbrace
465-
res += value
466-
else:
467-
var = path[:0]
468-
index += 1
469-
c = path[index:index + 1]
470-
while c and c in varchars:
471-
var += c
472-
index += 1
473-
c = path[index:index + 1]
474-
try:
475-
if environ is None:
476-
value = os.fsencode(os.environ[os.fsdecode(var)])
477-
else:
478-
value = environ[var]
479-
except KeyError:
480-
value = dollar + var
481-
res += value
482-
if c:
483-
index -= 1
415+
416+
def repl(m):
417+
lastindex = m.lastindex
418+
if lastindex is None:
419+
return m[0]
420+
name = m[lastindex]
421+
if lastindex == 1:
422+
if name == percent:
423+
return name
424+
if not name.endswith(percent):
425+
return m[0]
426+
name = name[:-1]
484427
else:
485-
res += c
486-
index += 1
487-
return res
428+
if name == dollar:
429+
return name
430+
if name.startswith(brace):
431+
if not name.endswith(rbrace):
432+
return m[0]
433+
name = name[1:-1]
434+
435+
try:
436+
if environ is None:
437+
return os.fsencode(os.environ[os.fsdecode(name)])
438+
else:
439+
return environ[name]
440+
except KeyError:
441+
return m[0]
442+
443+
return sub(repl, path)
488444

489445

490446
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.

Lib/posixpath.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -287,56 +287,53 @@ def expanduser(path):
287287
# This expands the forms $variable and ${variable} only.
288288
# Non-existent variables are left unchanged.
289289

290-
_varprog = None
291-
_varprogb = None
290+
_varpattern = r'\$(\w+|\{[^}]*\}?)'
291+
_varsub = None
292+
_varsubb = None
292293

293294
def expandvars(path):
294295
"""Expand shell variables of form $var and ${var}. Unknown variables
295296
are left unchanged."""
296297
path = os.fspath(path)
297-
global _varprog, _varprogb
298+
global _varsub, _varsubb
298299
if isinstance(path, bytes):
299300
if b'$' not in path:
300301
return path
301-
if not _varprogb:
302+
if not _varsubb:
302303
import re
303-
_varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII)
304-
search = _varprogb.search
304+
_varsubb = re.compile(_varpattern.encode(), re.ASCII).sub
305+
sub = _varsubb
305306
start = b'{'
306307
end = b'}'
307308
environ = getattr(os, 'environb', None)
308309
else:
309310
if '$' not in path:
310311
return path
311-
if not _varprog:
312+
if not _varsub:
312313
import re
313-
_varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII)
314-
search = _varprog.search
314+
_varsub = re.compile(_varpattern, re.ASCII).sub
315+
sub = _varsub
315316
start = '{'
316317
end = '}'
317318
environ = os.environ
318-
i = 0
319-
while True:
320-
m = search(path, i)
321-
if not m:
322-
break
323-
i, j = m.span(0)
324-
name = m.group(1)
325-
if name.startswith(start) and name.endswith(end):
319+
320+
def repl(m):
321+
name = m[1]
322+
if name.startswith(start):
323+
if not name.endswith(end):
324+
return m[0]
326325
name = name[1:-1]
327326
try:
328327
if environ is None:
329328
value = os.fsencode(os.environ[os.fsdecode(name)])
330329
else:
331330
value = environ[name]
332331
except KeyError:
333-
i = j
332+
return m[0]
334333
else:
335-
tail = path[j:]
336-
path = path[:i] + value
337-
i = len(path)
338-
path += tail
339-
return path
334+
return value
335+
336+
return sub(repl, path)
340337

341338

342339
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.

Lib/test/test_genericpath.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import unittest
99
import warnings
10+
from test import support
1011
from test.support import is_emscripten
1112
from test.support import os_helper
1213
from test.support import warnings_helper
@@ -434,6 +435,19 @@ def check(value, expected):
434435
os.fsencode('$bar%s bar' % nonascii))
435436
check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
436437

438+
@support.requires_resource('cpu')
439+
def test_expandvars_large(self):
440+
expandvars = self.pathmodule.expandvars
441+
with os_helper.EnvironmentVarGuard() as env:
442+
env.clear()
443+
env["A"] = "B"
444+
n = 100_000
445+
self.assertEqual(expandvars('$A'*n), 'B'*n)
446+
self.assertEqual(expandvars('${A}'*n), 'B'*n)
447+
self.assertEqual(expandvars('$A!'*n), 'B!'*n)
448+
self.assertEqual(expandvars('${A}A'*n), 'BA'*n)
449+
self.assertEqual(expandvars('${'*10*n), '${'*10*n)
450+
437451
def test_abspath(self):
438452
self.assertIn("foo", self.pathmodule.abspath("foo"))
439453
with warnings.catch_warnings():

Lib/test/test_ntpath.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import unittest
77
import warnings
88
from ntpath import ALLOW_MISSING
9-
from test.support import os_helper
10-
from test.support import TestFailed, is_emscripten
9+
from test import support
10+
from test.support import os_helper, is_emscripten
1111
from test.support.os_helper import FakePath
1212
from test import test_genericpath
1313
from tempfile import TemporaryFile
@@ -57,7 +57,7 @@ def tester(fn, wantResult):
5757
fn = fn.replace("\\", "\\\\")
5858
gotResult = eval(fn)
5959
if wantResult != gotResult and _norm(wantResult) != _norm(gotResult):
60-
raise TestFailed("%s should return: %s but returned: %s" \
60+
raise support.TestFailed("%s should return: %s but returned: %s" \
6161
%(str(fn), str(wantResult), str(gotResult)))
6262

6363
# then with bytes
@@ -73,7 +73,7 @@ def tester(fn, wantResult):
7373
warnings.simplefilter("ignore", DeprecationWarning)
7474
gotResult = eval(fn)
7575
if _norm(wantResult) != _norm(gotResult):
76-
raise TestFailed("%s should return: %s but returned: %s" \
76+
raise support.TestFailed("%s should return: %s but returned: %s" \
7777
%(str(fn), str(wantResult), repr(gotResult)))
7878

7979

@@ -820,6 +820,19 @@ def check(value, expected):
820820
check('%spam%bar', '%sbar' % nonascii)
821821
check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
822822

823+
@support.requires_resource('cpu')
824+
def test_expandvars_large(self):
825+
expandvars = ntpath.expandvars
826+
with os_helper.EnvironmentVarGuard() as env:
827+
env.clear()
828+
env["A"] = "B"
829+
n = 100_000
830+
self.assertEqual(expandvars('%A%'*n), 'B'*n)
831+
self.assertEqual(expandvars('%A%A'*n), 'BA'*n)
832+
self.assertEqual(expandvars("''"*n + '%%'), "''"*n + '%')
833+
self.assertEqual(expandvars("%%"*n), "%"*n)
834+
self.assertEqual(expandvars("$$"*n), "$"*n)
835+
823836
def test_expanduser(self):
824837
tester('ntpath.expanduser("test")', 'test')
825838

@@ -1090,6 +1103,7 @@ def test_nt_helpers(self):
10901103
self.assertIsInstance(b_final_path, bytes)
10911104
self.assertGreater(len(b_final_path), 0)
10921105

1106+
10931107
class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
10941108
pathmodule = ntpath
10951109
attributes = ['relpath']
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix quadratic complexity in :func:`os.path.expandvars`.

0 commit comments

Comments
 (0)