Skip to content

Commit 49bb04e

Browse files
committed
py/makeqstrdata: Fix rendering of qstrs that have non-printable ASCII.
The qstr data needs to be turned into a proper C string so non-ASCII chars must be properly escaped according to C rules.
1 parent 0c1de1c commit 49bb04e

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

py/makeqstrdata.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,15 @@ def parse_input_headers(infiles):
106106

107107
def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
108108
qhash = compute_hash(qstr, cfg_bytes_hash)
109-
# Calculate len of str, taking escapes into account
110-
qlen = len(qstr.replace("\\\\", "-").replace("\\", ""))
111-
qdata = qstr.replace('"', '\\"')
109+
if all(32 <= ord(c) <= 126 and c != '\\' for c in qstr):
110+
# qstr is all printable ASCII so render it as-is (for easier debugging)
111+
qlen = len(qstr)
112+
qdata = qstr
113+
else:
114+
# qstr contains non-printable codes so render entire thing as hex pairs
115+
qbytes = qstr.encode('utf8')
116+
qlen = len(qbytes)
117+
qdata = ''.join(('\\x%02x' % b) for b in qbytes)
112118
if qlen >= (1 << (8 * cfg_bytes_len)):
113119
print('qstr is too long:', qstr)
114120
assert False

0 commit comments

Comments
 (0)