Skip to contentSkip to conversation

utils: Don't return directly from SNPRINTF_BUFFER_SIZE

PATCH Applied Phil Sutter 2 participants 2 messages Aug 12, 2016 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 utils: Don't return directly from SNPRINTF_BUFFER_SIZE

From: Phil Sutter <[email protected]>

Apart from being a bad idea in general, the return statement contained in that macro in some cases leads to returning from functions without properly cleaning up, thereby causing memory leaks.

Instead, just sanitize the value in 'ret' to not harm further calls of snprintf() (as 'len' will eventually just become zero).

Cc Arturo Borrero <[email protected]>

---
This is actually v2 of "[libnftnl PATCH 2/7] ruleset: Prevent memleak in nftnl_ruleset_snprintf_*() functions" but since it's a completely different approach there was no use in keeping the old subject line.

---
 include/utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/utils.h +1 -1
index 21694b6ab1912..924df324aa603 100644
@@ -54,7 +54,7 @@ void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
 
 #define SNPRINTF_BUFFER_SIZE(ret, size, len, offset)	\
 	if (ret < 0)					\
-		return ret;				\
+		ret = 0;				\
 	offset += ret;					\
 	if (ret > len)					\
 		ret = len;				\
Signature · 1 line
2.8.2
maintainer Applied
#1 lore raw
Quoted 8 lines · Phil Sutter

From: Phil Sutter <[email protected]>

Apart from being a bad idea in general, the return statement contained in that macro in some cases leads to returning from functions without properly cleaning up, thereby causing memory leaks.

Instead, just sanitize the value in 'ret' to not harm further calls of snprintf() (as 'len' will eventually just become zero).

Applied, thanks.