Skip to content

Commit 97ad8e0

Browse files
Merge pull request #72 from wnienhaus/fix_uppercase_expressions
Allow uppercase hex chars in expressions
2 parents 19d4b98 + 4f591dd commit 97ad8e0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Diff for: esp32_ulp/util.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,23 @@ def validate_expression(param):
4949
for token in split_tokens(param):
5050
state = 0
5151
for c in token:
52-
if c not in ' \t+-*/%()<>&|~x0123456789abcdef':
52+
if c not in ' \t+-*/%()<>&|~xX0123456789abcdefABCDEF':
5353
return False
5454

5555
# the following allows hex digits a-f after 0x but not otherwise
5656
if state == 0:
57-
if c in 'abcdef':
57+
if c in 'abcdefABCDEF':
5858
return False
5959
if c == '0':
6060
state = 1
6161
continue
6262

6363
if state == 1:
64-
state = 2 if c == 'x' else 0
64+
state = 2 if c in 'xX' else 0
6565
continue
6666

6767
if state == 2:
68-
if c not in '0123456789abcdef':
68+
if c not in '0123456789abcdefABCDEF':
6969
state = 0
7070
return True
7171

Diff for: tests/compat/expr.S

+8
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ exit:
4646
move r3, 0x1234 & ~2
4747
move r3, 42|4&0xf # 46 (4&0xf is evaluated first)
4848
move r3, (42|4)&0xf # 14 (42|4 is evaluated first)
49+
50+
# ---
51+
# test that expressions accept hex characters in either upper or lower case
52+
move r3, 0xaa - 1
53+
move r3, 0xBB - 1
54+
move r3, 0xCc - 1
55+
move r3, 0Xdd - 1
56+
move r3, 0XEF - 1

Diff for: tests/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def test_validate_expression():
4444
assert validate_expression('0x100 & ~2') is True
4545
assert validate_expression('0xabcdef') is True
4646
assert validate_expression('0x123def') is True
47+
assert validate_expression('0xABC') is True
48+
assert validate_expression('0xaBc') is True
49+
assert validate_expression('0Xabc') is True
50+
assert validate_expression('0X123ABC') is True
4751
assert validate_expression('2*3+4/5&6|7') is True
4852
assert validate_expression('(((((1+1) * 2') is True # valid characters, even if expression is not valid
4953

@@ -55,6 +59,7 @@ def test_validate_expression():
5559
assert validate_expression('123 ^ 4') is False # operator not supported for now
5660
assert validate_expression('evil()') is False
5761
assert validate_expression('def cafe()') is False # valid hex digits, but potentially dangerous code
62+
assert validate_expression('def CAFE()') is False
5863

5964

6065
@test

0 commit comments

Comments
 (0)