Skip to content

bpo-34689: Prevent sysconfig._parse_makefile from expanding $${variables} #20439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,21 @@ def _parse_makefile(filename, vars=None):
while len(variables) > 0:
for name in tuple(variables):
value = notdone[name]
m1 = _findvar1_rx.search(value)
m2 = _findvar2_rx.search(value)
if m1 and m2:
m = m1 if m1.start() < m2.start() else m2
else:
m = m1 if m1 else m2
m = None
offset = 0
for substr in value.split('$$'):
m1 = _findvar1_rx.search(substr)
m2 = _findvar2_rx.search(substr)
if m1 and m2:
m = m1 if m1.start() < m2.start() else m2
else:
m = m1 if m1 else m2
if m is not None:
break

# Add 2 to account for the $$ which were removed by split('$$')
offset += 2 + len(substr)

if m is not None:
n = m.group(1)
found = True
Expand Down Expand Up @@ -292,11 +301,13 @@ def _parse_makefile(filename, vars=None):
done[n] = item = ""

if found:
after = value[m.end():]
value = value[:m.start()] + item + after
if "$" in after:
after = value[offset + m.end():]
value = value[:offset + m.start()] + \
item.replace('$', '$$') + after
if "$" in after.replace('$$', ''):
notdone[name] = value
else:
value = value.replace('$$', '$')
try:
value = int(value)
except ValueError:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ def test_parse_makefile(self):
print("var5=dollar$$5", file=makefile)
print("var6=${var3}/lib/python3.5/config-$(VAR2)$(var5)"
"-x86_64-linux-gnu", file=makefile)
print("var7=$${ORIGIN}${var5}", file=makefile)
vars = sysconfig._parse_makefile(TESTFN)
self.assertEqual(vars, {
'var1': 'ab42',
Expand All @@ -414,6 +415,7 @@ def test_parse_makefile(self):
'var4': '$/invalid',
'var5': 'dollar$5',
'var6': '42/lib/python3.5/config-b42dollar$5-x86_64-linux-gnu',
'var7': '${ORIGIN}dollar$5',
})


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Split strings prior to parsing in `sysconfig._parse_makefile` so it doesn't
expand variables formatted as ``$${variable}``.