mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Since this module is used as a fallback in case no built-in modules
have been configured, string.atof() should not fail when "import re" fails (usually because pcre is not there). This opens up a tiny security hole: *if* an attacker can make "import re" fail, they can also make string.atof(arbitrary_string) evaluate the arbitrary string. Nothing to keep me awake at night...
This commit is contained in:
parent
e680546894
commit
90d62ab0a1
2 changed files with 14 additions and 6 deletions
|
@ -203,7 +203,11 @@ re = None
|
||||||
def atof(str):
|
def atof(str):
|
||||||
global re
|
global re
|
||||||
if re is None:
|
if re is None:
|
||||||
import re
|
# Don't fail if re doesn't exist -- just skip the syntax check
|
||||||
|
try:
|
||||||
|
import re
|
||||||
|
except ImportError:
|
||||||
|
re = 0
|
||||||
sign = ''
|
sign = ''
|
||||||
s = strip(str)
|
s = strip(str)
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
@ -212,10 +216,10 @@ def atof(str):
|
||||||
if not s:
|
if not s:
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
|
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
|
||||||
if not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
|
if re and not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
try:
|
try:
|
||||||
return float(eval(sign + s))
|
return float(eval(sign + s, {}))
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
|
|
||||||
|
|
|
@ -203,7 +203,11 @@ re = None
|
||||||
def atof(str):
|
def atof(str):
|
||||||
global re
|
global re
|
||||||
if re is None:
|
if re is None:
|
||||||
import re
|
# Don't fail if re doesn't exist -- just skip the syntax check
|
||||||
|
try:
|
||||||
|
import re
|
||||||
|
except ImportError:
|
||||||
|
re = 0
|
||||||
sign = ''
|
sign = ''
|
||||||
s = strip(str)
|
s = strip(str)
|
||||||
if s and s[0] in '+-':
|
if s and s[0] in '+-':
|
||||||
|
@ -212,10 +216,10 @@ def atof(str):
|
||||||
if not s:
|
if not s:
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
|
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
|
||||||
if not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
|
if re and not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
try:
|
try:
|
||||||
return float(eval(sign + s))
|
return float(eval(sign + s, {}))
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
raise ValueError, 'non-float argument to string.atof'
|
raise ValueError, 'non-float argument to string.atof'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue