summaryrefslogtreecommitdiffstats
path: root/src/expression.c
diff options
context:
space:
mode:
authorFlorian Westphal <fw@strlen.de>2020-02-22 22:12:02 +0100
committerFlorian Westphal <fw@strlen.de>2020-02-23 23:31:12 +0100
commit32bc5d4e1009192ed4d2a7d102f3c2abb3f2707c (patch)
treeef1072b3ef2d234829834a1bf2c5e6f97936ee56 /src/expression.c
parent58386df4ae6d6481ff16700df232551da00205aa (diff)
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 <fw@strlen.de>
Diffstat (limited to 'src/expression.c')
-rw-r--r--src/expression.c35
1 files changed, 15 insertions, 20 deletions
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);
}