From a54e60a541f009639d596e1fecaeeab67683d258 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Date: Thu, 26 Sep 2013 00:13:08 +0200 Subject: src: snprintf: fix buffer lengths Use 'len' instead of 'size' since we need the remaining unused bytes in the buffer, not its total size. Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/chain.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/chain.c') diff --git a/src/chain.c b/src/chain.c index 8c0d804..874116a 100644 --- a/src/chain.c +++ b/src/chain.c @@ -753,7 +753,7 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) { int ret, len = size, offset = 0; - ret = snprintf(buf, size, + ret = snprintf(buf, len, "{ \"chain\": {" "\"name\": \"%s\"," "\"handle\": %"PRIu64"," @@ -768,7 +768,7 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) SNPRINTF_BUFFER_SIZE(ret, size, len, offset); if (c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM)) { - ret = snprintf(buf+offset, size, + ret = snprintf(buf+offset, len, ",\"type\": \"%s\"," "\"hooknum\": \"%s\"," "\"prio\": %d," @@ -778,9 +778,7 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } - ret = snprintf(buf+offset, size, - "}" - "}"); + ret = snprintf(buf+offset, len, "}}"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); return offset; @@ -790,14 +788,14 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) { int ret, len = size, offset = 0; - ret = snprintf(buf, size, "%s" + ret = snprintf(buf, len, "%s" "%"PRIu64"%"PRIu64"" "%"PRIu64"%s
", c->name, c->handle, c->bytes, c->packets, c->table); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); if (c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM)) { - ret = snprintf(buf+offset, size, + ret = snprintf(buf+offset, len, "%s" "%s" "%d" @@ -807,7 +805,7 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } - ret = snprintf(buf+offset, size, "%s
", + ret = snprintf(buf+offset, len, "%s
", nft_family2str(c->family)); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); @@ -819,12 +817,12 @@ static int nft_chain_snprintf_default(char *buf, size_t size, { int ret, len = size, offset = 0; - ret = snprintf(buf, size, "%s %s %s", - nft_family2str(c->family), c->table, c->name); + ret = snprintf(buf, len, "%s %s %s", + nft_family2str(c->family), c->table, c->name); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); if (c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM)) { - ret = snprintf(buf+offset, size, + ret = snprintf(buf+offset, len, " type %s hook %s prio %d policy %s use %d " "packets %"PRIu64" bytes %"PRIu64"", c->type, nft_hooknum2str(c->family, c->hooknum), -- cgit v1.2.3