From 11fcafcf6928d793411ae3dba9af324e3d027bb2 Mon Sep 17 00:00:00 2001 From: Denis Ovsienko Date: Thu, 20 Jul 2017 20:58:06 +0100 Subject: [PATCH] HNCP: refine snprintf() buffers sizing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This squelches a warning in format_256(), make a similar change to format_nid() while at it. $ gcc --version gcc (GCC) 7.1.1 20170622 (Red Hat 7.1.1-3) ./print-hncp.c: In function ‘format_256’: ./print-hncp.c:175:26: warning: ‘%016lx’ directive output truncated writing 16 bytes into a region of size 12 [-Wformat-truncation=] snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, ^~~~~~ ./print-hncp.c:175:41: note: format string is defined here snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, ./print-hncp.c:175:26: note: using the range [0, 18446744073709551615] for directive argument snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, ^~~~~~ ./print-hncp.c:175:26: note: using the range [0, 18446744073709551615] for directive argument ./print-hncp.c:175:26: note: using the range [0, 18446744073709551615] for directive argument ./print-hncp.c:175:5: note: ‘snprintf’ output 65 bytes into a destination of size 28 snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EXTRACT_64BITS(data), ~~~~~~~~~~~~~~~~~~~~~ EXTRACT_64BITS(data + 8), ~~~~~~~~~~~~~~~~~~~~~~~~~ EXTRACT_64BITS(data + 16), ~~~~~~~~~~~~~~~~~~~~~~~~~~ EXTRACT_64BITS(data + 24) ~~~~~~~~~~~~~~~~~~~~~~~~~ ); ~ --- print-hncp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/print-hncp.c b/print-hncp.c index 87ee8bbb..32adafa9 100644 --- a/print-hncp.c +++ b/print-hncp.c @@ -158,10 +158,10 @@ is_ipv4_mapped_address(const u_char *addr) static const char * format_nid(const u_char *data) { - static char buf[4][11+5]; + static char buf[4][sizeof("01:01:01:01")]; static int i = 0; i = (i + 1) % 4; - snprintf(buf[i], 16, "%02x:%02x:%02x:%02x", + snprintf(buf[i], sizeof(buf[i]), "%02x:%02x:%02x:%02x", data[0], data[1], data[2], data[3]); return buf[i]; } @@ -169,10 +169,10 @@ format_nid(const u_char *data) static const char * format_256(const u_char *data) { - static char buf[4][64+5]; + static char buf[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")]; static int i = 0; i = (i + 1) % 4; - snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, + snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, EXTRACT_64BITS(data), EXTRACT_64BITS(data + 8), EXTRACT_64BITS(data + 16), -- 2.39.5