mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-09-13 22:42:29 +03:00
replace unneeded eval() call and support negative versionCodes
This commit is contained in:
parent
2f78e162e0
commit
cd22eceb68
2 changed files with 23 additions and 6 deletions
|
@ -3190,8 +3190,12 @@ def get_git_describe_link():
|
|||
|
||||
|
||||
def calculate_math_string(expr):
|
||||
ops = {ast.Add: operator.add, ast.Sub: operator.sub,
|
||||
ast.Mult: operator.mul}
|
||||
ops = {
|
||||
ast.Add: operator.add,
|
||||
ast.Mult: operator.mul,
|
||||
ast.Sub: operator.sub,
|
||||
ast.USub: operator.neg,
|
||||
}
|
||||
|
||||
def execute_ast(node):
|
||||
if isinstance(node, ast.Num): # <number>
|
||||
|
@ -3200,7 +3204,7 @@ def calculate_math_string(expr):
|
|||
return ops[type(node.op)](execute_ast(node.left),
|
||||
execute_ast(node.right))
|
||||
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
|
||||
return ops[type(node.op)](eval(node.operand))
|
||||
return ops[type(node.op)](ast.literal_eval(node.operand))
|
||||
else:
|
||||
raise SyntaxError(node)
|
||||
|
||||
|
|
|
@ -780,13 +780,26 @@ class CommonTest(unittest.TestCase):
|
|||
fdroidserver.common.parse_androidmanifests(paths, app))
|
||||
|
||||
def test_calculate_math_string(self):
|
||||
self.assertEqual(1234, fdroidserver.common.calculate_math_string('1234'))
|
||||
self.assertEqual(4, fdroidserver.common.calculate_math_string('(1+1)*2'))
|
||||
self.assertEqual(2, fdroidserver.common.calculate_math_string('(1-1)*2+3*1-1'))
|
||||
self.assertEqual(1234,
|
||||
fdroidserver.common.calculate_math_string('1234'))
|
||||
self.assertEqual((1 + 1) * 2,
|
||||
fdroidserver.common.calculate_math_string('(1 + 1) * 2'))
|
||||
self.assertEqual((1 - 1) * 2 + 3 * 1 - 1,
|
||||
fdroidserver.common.calculate_math_string('(1 - 1) * 2 + 3 * 1 - 1'))
|
||||
self.assertEqual(0 - 12345,
|
||||
fdroidserver.common.calculate_math_string('0 - 12345'))
|
||||
self.assertEqual(0xffff,
|
||||
fdroidserver.common.calculate_math_string('0xffff'))
|
||||
self.assertEqual(0xcafe * 123,
|
||||
fdroidserver.common.calculate_math_string('0xcafe * 123'))
|
||||
self.assertEqual(-1,
|
||||
fdroidserver.common.calculate_math_string('-1'))
|
||||
with self.assertRaises(SyntaxError):
|
||||
fdroidserver.common.calculate_math_string('__import__("urllib")')
|
||||
with self.assertRaises(SyntaxError):
|
||||
fdroidserver.common.calculate_math_string('self')
|
||||
with self.assertRaises(SyntaxError):
|
||||
fdroidserver.common.calculate_math_string('Ox9()')
|
||||
with self.assertRaises(SyntaxError):
|
||||
fdroidserver.common.calculate_math_string('1+1; print(1)')
|
||||
with self.assertRaises(SyntaxError):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue