From 32bc5d4e1009192ed4d2a7d102f3c2abb3f2707c Mon Sep 17 00:00:00 2001 From: Florian Westphal Date: Sat, 22 Feb 2020 22:12:02 +0100 Subject: expression: use common code for expr_ops/expr_ops_by_type Useless duplication. Also, this avoids bloating expr_ops_by_type() when it needs to cope with more expressions. Signed-off-by: Florian Westphal --- src/expression.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'src/expression.c') diff --git a/src/expression.c b/src/expression.c index cb11cda4..a2694f4a 100644 --- a/src/expression.c +++ b/src/expression.c @@ -1185,9 +1185,9 @@ void range_expr_value_high(mpz_t rop, const struct expr *expr) } } -const struct expr_ops *expr_ops(const struct expr *e) +static const struct expr_ops *__expr_ops_by_type(enum expr_types etype) { - switch (e->etype) { + switch (etype) { case EXPR_INVALID: BUG("Invalid expression ops requested"); break; @@ -1220,26 +1220,21 @@ const struct expr_ops *expr_ops(const struct expr *e) case EXPR_XFRM: return &xfrm_expr_ops; } - BUG("Unknown expression type %d\n", e->etype); + BUG("Unknown expression type %d\n", etype); } -const struct expr_ops *expr_ops_by_type(enum expr_types etype) +const struct expr_ops *expr_ops(const struct expr *e) { - switch (etype) { - case EXPR_PAYLOAD: return &payload_expr_ops; - case EXPR_EXTHDR: return &exthdr_expr_ops; - case EXPR_META: return &meta_expr_ops; - case EXPR_SOCKET: return &socket_expr_ops; - case EXPR_OSF: return &osf_expr_ops; - case EXPR_CT: return &ct_expr_ops; - case EXPR_NUMGEN: return &numgen_expr_ops; - case EXPR_HASH: return &hash_expr_ops; - case EXPR_RT: return &rt_expr_ops; - case EXPR_FIB: return &fib_expr_ops; - case EXPR_XFRM: return &xfrm_expr_ops; - default: - break; - } + return __expr_ops_by_type(e->etype); +} - BUG("Unknown expression type %d\n", etype); +const struct expr_ops *expr_ops_by_type(uint32_t value) +{ + /* value might come from unreliable source, such as "udata" + * annotation of set keys. Avoid BUG() assertion. + */ + if (value == EXPR_INVALID || value > EXPR_MAX) + return NULL; + + return __expr_ops_by_type(value); } -- cgit v1.2.3