]> The Tcpdump Group git mirrors - tcpdump/commitdiff
HNCP: refine snprintf() buffers sizing
authorDenis Ovsienko <[email protected]>
Thu, 20 Jul 2017 19:58:06 +0000 (20:58 +0100)
committerDenis Ovsienko <[email protected]>
Thu, 20 Jul 2017 20:04:56 +0000 (21:04 +0100)
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

index 87ee8bbb9232e18d40cdca6efba4750e5bd5d7e2..32adafa9c0dbd03a28ae920356a0cb95b561d3fd 100644 (file)
@@ -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),