Blob Blame History Raw
From 8cb078a2f9f69259325c10f479c198349ef01ef2 Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Wed, 6 Oct 2021 17:24:44 +0200
Subject: [PATCH] parser_json: Fix error reporting for invalid syntax

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1994141
Upstream Status: nftables commit 9fe5d1bc18cfa

commit 9fe5d1bc18cfaed2ecf717e3dd9a97ff5b0e183c
Author: Phil Sutter <phil@nwl.cc>
Date:   Wed Sep 1 16:41:44 2021 +0200

    parser_json: Fix error reporting for invalid syntax

    Errors emitted by the JSON parser caused BUG() in erec_print() due to
    input descriptor values being bogus.

    Due to lack of 'include' support, JSON parser uses a single input
    descriptor only and it lived inside the json_ctx object on stack of
    nft_parse_json_*() functions.

    By the time errors are printed though, that scope is not valid anymore.
    Move the static input descriptor object to avoid this.

    Fixes: 586ad210368b7 ("libnftables: Implement JSON parser")
    Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/parser_json.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/parser_json.c b/src/parser_json.c
index a069a89..ef4d4fb 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -44,7 +44,6 @@
 #define CTX_F_CONCAT	(1 << 8)	/* inside concat_expr */
 
 struct json_ctx {
-	struct input_descriptor indesc;
 	struct nft_ctx *nft;
 	struct list_head *msgs;
 	struct list_head *cmds;
@@ -107,11 +106,12 @@ static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root);
 /* parsing helpers */
 
 const struct location *int_loc = &internal_location;
+static struct input_descriptor json_indesc;
 
 static void json_lib_error(struct json_ctx *ctx, json_error_t *err)
 {
 	struct location loc = {
-		.indesc = &ctx->indesc,
+		.indesc = &json_indesc,
 		.line_offset = err->position - err->column,
 		.first_line = err->line,
 		.last_line = err->line,
@@ -3864,16 +3864,15 @@ int nft_parse_json_buffer(struct nft_ctx *nft, const char *buf,
 			  struct list_head *msgs, struct list_head *cmds)
 {
 	struct json_ctx ctx = {
-		.indesc = {
-			.type = INDESC_BUFFER,
-			.data = buf,
-		},
 		.nft = nft,
 		.msgs = msgs,
 		.cmds = cmds,
 	};
 	int ret;
 
+	json_indesc.type = INDESC_BUFFER;
+	json_indesc.data = buf;
+
 	parser_init(nft, nft->state, msgs, cmds, nft->top_scope);
 	nft->json_root = json_loads(buf, 0, NULL);
 	if (!nft->json_root)
@@ -3892,10 +3891,6 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
 			    struct list_head *msgs, struct list_head *cmds)
 {
 	struct json_ctx ctx = {
-		.indesc = {
-			.type = INDESC_FILE,
-			.name = filename,
-		},
 		.nft = nft,
 		.msgs = msgs,
 		.cmds = cmds,
@@ -3903,6 +3898,9 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
 	json_error_t err;
 	int ret;
 
+	json_indesc.type = INDESC_FILE;
+	json_indesc.name = filename;
+
 	parser_init(nft, nft->state, msgs, cmds, nft->top_scope);
 	nft->json_root = json_load_file(filename, 0, &err);
 	if (!nft->json_root)
-- 
2.31.1