Skip to content

Commit 6b61425

Browse files
committed
Small tweak in string define parsing
1 parent 3a37ab0 commit 6b61425

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

dissect/cstruct/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def _parse_define(self) -> None:
170170
if value[-1] != quote:
171171
raise self._error("unterminated string literal", token=token)
172172

173-
# Remove the surrounding and any duplicate quotes
174-
value = "".join(ch for ch in value if ch != quote)
173+
# Remove the surrounding quotes and flatten escape sequences
174+
value = value[1:-1].encode().decode("unicode_escape")
175175
elif value[:2].lower() in ("b'", 'b"'):
176176
quote = value[1]
177177
if value[-1] != quote:

tests/test_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ def test_define(cs: cstruct) -> None:
228228
#define MULTILINE (1 + \
229229
2 + \
230230
3)
231+
#define QUOTES "\'\"a'b\""
232+
#define ESCAPE "\\'\\"a'b\\"\\n"
231233
#define FUNC(x) ( x == 0 )
232234
#define TERNARY(x) ( x ? 1 : 0 )
233235
"""
@@ -243,6 +245,8 @@ def test_define(cs: cstruct) -> None:
243245
assert cs.consts["NULLBYTES"] == b"ADCRYPT\00"
244246
assert cs.consts["ARBITRARYBYTES"] == b"\x00\x01\x02"
245247
assert cs.consts["MULTILINE"] == 6
248+
assert cs.consts["QUOTES"] == "'\"a'b\""
249+
assert cs.consts["ESCAPE"] == "'\"a'b\"\n"
246250
# We don't evaluate function-like macros yet, so they should be stored as their raw string representation
247251
assert cs.consts["FUNC"] == "(x) ( x == 0 )"
248252
assert cs.consts["TERNARY"] == "(x) ( x ? 1 : 0 )"

0 commit comments

Comments
 (0)