diff mbox series

[nft,v4,05/32] ct: support `NULL` symbol-tables when looking up labels

Message ID 20220404121410.188509-6-jeremy@azazel.net
State Changes Requested
Delegated to: Pablo Neira
Headers show
Series Extend values assignable to packet marks and payload fields | expand

Commit Message

Jeremy Sowden April 4, 2022, 12:13 p.m. UTC
If the symbol-table passed to `ct_label2str` is `NULL`, return `NULL`.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 src/ct.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Florian Westphal April 5, 2022, 11:15 a.m. UTC | #1
Jeremy Sowden <jeremy@azazel.net> wrote:
> If the symbol-table passed to `ct_label2str` is `NULL`, return `NULL`.

It would be nice to describe why, not what.
Does this fix a crash when the label file is missing?
Jeremy Sowden April 5, 2022, 3:29 p.m. UTC | #2
On 2022-04-05, at 13:15:45 +0200, Florian Westphal wrote:
> Jeremy Sowden <jeremy@azazel.net> wrote:
> > If the symbol-table passed to `ct_label2str` is `NULL`, return `NULL`.
> 
> It would be nice to describe why, not what.
> Does this fix a crash when the label file is missing?

I've been putting debug logging in various places while developing and
testing things.  Here's a function for dumping expressions to stderr:

  static inline void
  dbg_print_expr(const char *func, const char *name,
                 const struct expr *expr)
  {
    struct output_ctx *dbgctx = &(struct output_ctx) { .output_fp = stderr };

    nft_print(dbgctx, "%s: %s = (%s, %u, %s, %s) { ",
              func, name,
              expr_ops(expr)->name,
              expr->len,
              expr->dtype ? expr->dtype->name : "",
              byteorder_names[expr->byteorder]);
    expr_print(expr, dbgctx);
    nft_print(dbgctx, " }\n");
  }

There are two problems with this.  Firstly, the `byteorder_names` array
is defined in src/evaluate.c and so this function cannot be used
elsewhere.  Secondly, the symbol-tables in the output context are not
initialized, which results in crashes when trying to print symbolic
constants and CT labels.  Patch 3 fixes the former problem by moving the
`byteorder_names` array, and patches 4 & 5 fix the latter problem by
adding NULL checks before trying to dereference the symbol-tables.  They
seemed like small, low-impact changes that could be upstreamed, so that
I didn't have to carry them.

J.
diff mbox series

Patch

diff --git a/src/ct.c b/src/ct.c
index e246d3039240..8c9ae7b0e04a 100644
--- a/src/ct.c
+++ b/src/ct.c
@@ -148,10 +148,11 @@  const char *ct_label2str(const struct symbol_table *ct_label_tbl,
 {
 	const struct symbolic_constant *s;
 
-	for (s = ct_label_tbl->symbols; s->identifier; s++) {
-		if (value == s->value)
-			return s->identifier;
-	}
+	if (ct_label_tbl != NULL)
+		for (s = ct_label_tbl->symbols; s->identifier; s++) {
+			if (value == s->value)
+				return s->identifier;
+		}
 
 	return NULL;
 }