Blob Blame History Raw
From 48c743edbd41470149a5ac58c350b47cb80fb9eb Mon Sep 17 00:00:00 2001
From: Phil Sutter <phil@nwl.cc>
Date: Wed, 24 Oct 2018 21:14:37 +0200
Subject: [PATCH] json: Work around segfault when encountering xt stmt

When trying to convert an xt stmt into JSON, print() callback was
called. Though the code in src/xt.c does not respect output_fp,
therefore buffer wasn't filled as expected making libjansson to puke:

| # nft -j list ruleset
| warning: stmt ops xt have no json callback
| nft: json.c:169: stmt_print_json: Assertion `__out' failed.
| Aborted (core dumped)

Avoid this by detecting xt stmt ops and returning a stub.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
(cherry picked from commit b849b0dfd9f3aecff5617bc60d5852ef36c3d494)

Conflicts:
	doc/libnftables-json.adoc
-> Patches adding this man page are missing entirely.

Signed-off-by: Phil Sutter <psutter@redhat.com>
---
 src/json.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/json.c b/src/json.c
index 98581a3c2a3e4..f74afd5a7292e 100644
--- a/src/json.c
+++ b/src/json.c
@@ -165,6 +165,12 @@ static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
 	char buf[1024];
 	FILE *fp;
 
+	/* XXX: Can't be supported at this point:
+	 * xt_stmt_xlate() ignores output_fp.
+	 */
+	if (stmt->ops->type == STMT_XT)
+		return json_pack("{s:n}", "xt");
+
 	if (stmt->ops->json)
 		return stmt->ops->json(stmt, octx);
 
-- 
2.19.0