From 29e6f3cff93d8ad3539595aafd469a097a37500e Mon Sep 17 00:00:00 2001 From: Peter Saveliev Date: Tue, 19 Mar 2024 18:49:47 +0100 Subject: [PATCH] decoder: explicit matcher argument types --- pyroute2/decoder/loader.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/pyroute2/decoder/loader.py b/pyroute2/decoder/loader.py index d0cf8fbfb..e56d714d3 100644 --- a/pyroute2/decoder/loader.py +++ b/pyroute2/decoder/loader.py @@ -91,6 +91,8 @@ def f(packet_header, ll_header, raw, data_offset, stack): @staticmethod def ll_header(family): + if not isinstance(family, int) or family < 0 or family > 0xffff: + raise TypeError('family must be unsigned short integer') def f(packet_header, ll_header, raw, data_offset, stack): return ll_header.family == family @@ -98,6 +100,10 @@ def f(packet_header, ll_header, raw, data_offset, stack): @staticmethod def data(fmt, offset, value): + if not isinstance(fmt, str): + raise TypeError('format must be string') + if not isinstance(offset, int) or not isinstance(value, int): + raise TypeError('offset and value must be integers') def f(packet_header, ll_header, raw, data_offset, stack): o = data_offset + offset s = struct.calcsize(fmt) @@ -112,14 +118,15 @@ def __init__(self, script): self.filters = [] self.script = script self.shlex = shlex.shlex(instream=io.StringIO(script)) - op = None + self.shlex.wordchars += '-~' + postpone = None while True: token = self.get_token(ignore=',') if token == '': break method = getattr(MatchOps, token) if token in ('AND', 'OR'): - op = method + postpone = method continue kwarg = {} token = self.get_token(expect='{') @@ -129,15 +136,17 @@ def __init__(self, script): break self.get_token(expect='=') value = self.get_token() - try: + if value[0] in ['"', "'"]: + # string + value = value[1:-1] + else: + # int value = int(value) - except ValueError: - pass kwarg[token] = value self.filters.append(method(**kwarg)) - if op is not None: - self.filters.append(op()) - op = None + if postpone is not None: + self.filters.append(postpone()) + postpone = None def get_token(self, expect=None, ignore=None): token = self.shlex.get_token()