1
0

proj: Correct parsing in python

This commit is contained in:
2025-04-19 18:07:36 +02:00
parent e83e1a31b8
commit 1e40a1b3c6
5 changed files with 47 additions and 23 deletions

View File

@@ -2355,9 +2355,9 @@ int sym(getaddrinfo)(const char *restrict node, const char *restrict service, co
}
const int ret = __real_getaddrinfo(node, service, hints, res);
if (res != NULL) {
msg("return %i:%s; res=%qi", ret, getaddrinfoerror(ret), *res);
msg("return %i:%s; errno %s; res=%qi", ret, getaddrinfoerror(ret), strerrorname_np(errno), *res);
} else {
msg("return %i:%s", ret, getaddrinfoerror(ret));
msg("return %i:%s; errno %s", ret, getaddrinfoerror(ret), strerrorname_np(errno));
}
return ret;
}

View File

@@ -109,9 +109,9 @@ class Handler(StreamRequestHandler):
def parse_arg(argument: str) -> tuple[any, int]:
if argument == '':
return None, 0
m = re.match(r'^\s*(\(nil\)|NULL|null|nullptr)\s*(,|$)', argument)
m = re.match(r'^\s*(\(nil\)|NULL|null|nullptr)\s*(,|]|}|$)', argument)
if m:
return 0, len(m.group(0))
return 0, len(m.group(0)) - (1 if m.group(2) in ('}', ']') else 0)
m = re.match(r'\s*"', argument)
if m:
idx = len(m.group(0)) - 1
@@ -120,17 +120,37 @@ class Handler(StreamRequestHandler):
if idx < len(argument) and argument[idx] == ',':
idx += 1
return s, idx
m = re.match(r'^\s*(.*?)([,:]|$)', argument)
m = re.match(r'\s*\[', argument)
if m:
idx = len(m.group(0))
s, i = Handler.parse_args(argument[idx:])
idx += i
if idx < len(argument) and argument[idx] == ',':
idx += 1
return s, idx
m = re.match(r'\s*\{', argument)
if m:
idx = len(m.group(0))
s, i = Handler.parse_args(argument[idx:], named=True)
idx += i
if idx < len(argument) and argument[idx] == ',':
idx += 1
return s, idx
m = re.match(r'^\s*(.*?)([,:\]}]|$)', argument)
a, e = m.group(1), m.group(2)
idx = len(m.group(0))
if a.startswith('0x'):
val = int(a[2:], 16)
elif a.startswith('0') and len(a) > 1:
val = int(a[1:], 8)
elif a in ('(nil)', 'NULL', 'null', 'nullptr'):
val = 0
else:
val = int(a, 10)
if e in (',', ''):
return val, idx
elif e in ('}', ']'):
return val, idx - 1
if argument[idx] == '[':
idx += 1
l, i = Handler.parse_args(argument[idx:])
@@ -155,18 +175,12 @@ class Handler(StreamRequestHandler):
idx += 1
return (val, s), idx
elif argument[idx] == '{':
m = re.match(r'^[^}]*}', argument[idx:])
if not m or not m.group(0).startswith('{') or not m.group(0).endswith('}'):
raise ValueError()
value = m.group(0)
idx += len(value)
idx += 1
l, i = Handler.parse_args(argument[idx:], named=True)
idx += i
if idx < len(argument) and argument[idx] == ',':
idx += 1
entries = {}
for e in [v.strip() for v in value[1:-1].split(',') if len(v.strip()) > 0]:
k, v = e.split(':', 1)
entries[k.strip()] = Handler.parse_arg(v)[0]
return (val, entries), idx
return (val, list(l)), idx
else:
m = re.match(r'[A-Z0-9_]+', argument[idx:])
if not m:
@@ -178,17 +192,27 @@ class Handler(StreamRequestHandler):
return (val, value), idx
@staticmethod
def parse_args(arguments: str) -> tuple[tuple, int]:
args = []
def parse_args(arguments: str, named: bool = False) -> tuple[tuple or dict, int]:
# FIXME parse dicts in lists, dicts in dicts, ...
print(named, arguments)
args = [] if not named else {}
idx = 0
name = None
while idx < len(arguments):
if arguments[idx] == ']':
if arguments[idx] == ']' and not named or arguments[idx] == '}' and named:
idx += 1
break
elif named:
m = re.match(r'^\s*([^:]+):', arguments[idx:])
idx += len(m.group(0))
name = m.group(1)
val, i = Handler.parse_arg(arguments[idx:])
args.append(val)
if named:
args[name] = val
else:
args.append(val)
idx += i
return tuple(args), idx
return tuple(args) if not named else args, idx
def handle_msg(self, msg: bytes):
timestamp, pid, tid, data = msg.rstrip(b'\n').split(b' ', 3)

View File

@@ -26,7 +26,7 @@ def main() -> None:
'INTERCEPT': 'unix:' + socket_name,
'INTERCEPT_VERBOSE': '1',
'INTERCEPT_FUNCTIONS': '*',
'INTERCEPT_LIBRARIES': '*,-/lib*,-/usr/lib*',
'INTERCEPT_LIBRARIES': ','.join(['*', '-/lib*', '-/usr/lib*']),
})

View File

@@ -26,7 +26,7 @@ def main() -> None:
'INTERCEPT': 'unix:' + socket_name,
'INTERCEPT_VERBOSE': '1',
'INTERCEPT_FUNCTIONS': ','.join(['malloc', 'calloc', 'realloc', 'reallocarray', 'free', 'getaddrinfo', 'freeaddrinfo']),
'INTERCEPT_LIBRARIES': '*,-/lib*,-/usr/lib*',
'INTERCEPT_LIBRARIES': ','.join(['*', '-/lib*', '-/usr/lib*']),
})

View File

@@ -37,7 +37,7 @@ def main() -> None:
'INTERCEPT': 'unix:' + socket_name,
'INTERCEPT_VERBOSE': '1',
'INTERCEPT_FUNCTIONS': '*',
'INTERCEPT_LIBRARIES': '*,-/lib*,-/usr/lib*',
'INTERCEPT_LIBRARIES': ','.join(['*', '-/lib*', '-/usr/lib*']),
})
for i, name in enumerate(ctx['call_sequence']):
errors = [r[1] for r in ctx['results'] if r[0] == i]