Skip to contentSkip to conversation

utils: ensure \0 is in place in nft_fprintf()

PATCH Applied Arturo Borrero Gonzalez 2 participants 4 messages Aug 25, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 utils: ensure \0 is in place in nft_fprintf()

We must make sure the buffer contains a \0 in the last position, to avoid printing trash by the last fprintf() call.

This is needed because if the internal snprintf callback prints nothing, no \0 exists in the buffer.

---
 src/utils.c |    4 ++++
 1 file changed, 4 insertions(+)
src/utils.c +4 -0
index 96c8bf2..8a7c521 100644
@@ -189,6 +189,8 @@ int nft_fprintf(FILE *fp, void *obj, uint32_t type, uint32_t flags,
 	size_t bufsiz = sizeof(_buf);
 	int ret;
 
+	buf[0] = '\0';
+
 	ret = snprintf_cb(buf, bufsiz, obj, type, flags);
 	if (ret < 0)
 		goto out;
@@ -200,6 +202,8 @@ int nft_fprintf(FILE *fp, void *obj, uint32_t type, uint32_t flags,
 		if (buf == NULL)
 			return -1;
 
+		buf[0] = '\0';
+
 		ret = snprintf_cb(buf, bufsiz, obj, type, flags);
 		if (ret < 0)
 			goto out;
#1 lore raw

PATCH 2/2 src: improve printing of XML/JSON event wrapper header/footer

Use the nft_fprintf() helper. The helper handles \0 properly.

Before this patch, we get trash in the output due to the buffer being printed without any \0.

---
 src/common.c |   34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
src/common.c +18 -16
index 3ceb811..a6f2508 100644
@@ -114,14 +114,15 @@ int nft_event_header_snprintf(char *buf, size_t size, uint32_t type,
 	return ret;
 }
 
-int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags)
+static int nft_event_header_fprintf_cb(char *buf, size_t size, void *unused,
+				       uint32_t type, uint32_t flags)
 {
-	char buf[64]; /* enough for the maximum string length above */
-
-	nft_event_header_snprintf(buf, sizeof(buf), type, flags);
-	buf[sizeof(buf) - 1] = '\0';
+	return nft_event_header_snprintf(buf, size, type, flags);
+}
 
-	return fprintf(fp, "%s", buf);
+int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags)
+{
+	return nft_fprintf(fp, NULL, type, flags, nft_event_header_fprintf_cb);
 }
 
 int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type,
@@ -140,6 +141,17 @@ int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type,
 	}
 }
 
+static int nft_event_footer_fprintf_cb(char *buf, size_t size, void *unused,
+				       uint32_t type, uint32_t flags)
+{
+	return nft_event_footer_snprintf(buf, size, type, flags);
+}
+
+int nft_event_footer_fprintf(FILE *fp, uint32_t type, uint32_t flags)
+{
+	return nft_fprintf(fp, NULL, type, flags, nft_event_footer_fprintf_cb);
+}
+
 static void nft_batch_build_hdr(char *buf, uint16_t type, uint32_t seq)
 {
 	struct nlmsghdr *nlh;
@@ -168,16 +180,6 @@ void nft_batch_end(char *buf, uint32_t seq)
 }
 EXPORT_SYMBOL(nft_batch_end);
 
-int nft_event_footer_fprintf(FILE *fp, uint32_t type, uint32_t flags)
-{
-	char buf[32]; /* enough for the maximum string length above */
-
-	nft_event_footer_snprintf(buf, sizeof(buf), type, flags);
-	buf[sizeof(buf) - 1] = '\0';
-
-	return fprintf(fp, "%s", buf);
-}
-
 int nft_batch_is_supported(void)
 {
 	struct mnl_socket *nl;
maintainer Applied
#2 lore raw

Re: [libnftnl PATCH 2/2] src: improve printing of XML/JSON event wrapper header/footer

Quoted 4 lines · Arturo Borrero Gonzalez

Use the nft_fprintf() helper. The helper handles \0 properly.

Before this patch, we get trash in the output due to the buffer being printed without any \0.

Applied, thanks.

I have replace "improve" by "fix" in the patch title.

#3 lore raw
In reply to Arturo Borrero Gonzalez
Quoted 2 lines · Arturo Borrero Gonzalez

We must make sure the buffer contains a \0 in the last position, to avoid printing trash by the last fprintf() call.

snprintf already guarantees that the string is nul-terminated if there is enough room to add \0.

        ret = snprintf_cb(buf, bufsiz, obj, type, flags);
        if (ret < 0)
                goto out;

        if (ret >= NFT_SNPRINTF_BUFSIZ) {
                bufsiz = ret + 1;

                buf = malloc(bufsiz);
                if (buf == NULL)
                        return -1;

                ret = snprintf_cb(buf, bufsiz, obj, type, flags);
                if (ret < 0)
                        goto out;
        }

        ret = fprintf(fp, "%s", buf);

I think we have guarantees that buf is always nul-terminated after the second try.

Patch 2/2 looks good to enough to me since it already resolves the printed "garbage" at the end of the output issue.