tools/xenstat: Fix -Wformat-truncation= issue
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 13 Aug 2019 13:46:00 +0000 (14:46 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 14 Aug 2019 11:04:20 +0000 (12:04 +0100)
Building with GCC 8.3 on Buster identifies:

  src/xenstat_linux.c: In function 'xenstat_collect_networks':
  src/xenstat_linux.c:307:32: warning: 'snprintf' output may be truncated before
  the last format character [-Wformat-truncation=]
    snprintf(devNoBridge, 16, "p%s", devBridge);
                                  ^
  src/xenstat_linux.c:307:2: note: 'snprintf' output between 2 and 17 bytes into
  a destination of size 16
    snprintf(devNoBridge, 16, "p%s", devBridge);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

devNoBridge[] needs one charater more than devBridge[], so allocate one byte
more.  Replace a raw 16 in the snprintf() call with a sizeof() expression
instead.

Finally, libxenstat, unlike most of the rest of the Xen, doesn't use -Werror
which is why this issue went unnoticed in CI.  Fix this.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Wei Liu <wl@xen.org>
tools/xenstat/libxenstat/Makefile
tools/xenstat/libxenstat/src/xenstat_linux.c

index 58f9d63de54c334fd35e75e0d1d6ba5fc109a65b..ea115ae0e61968745b278f6c5a6c74511002f90c 100644 (file)
@@ -31,7 +31,7 @@ OBJECTS-$(CONFIG_NetBSD) += src/xenstat_netbsd.o
 OBJECTS-$(CONFIG_FreeBSD) += src/xenstat_freebsd.o
 SONAME_FLAGS=-Wl,$(SONAME_LDFLAG) -Wl,libxenstat.so.$(MAJOR)
 
-CFLAGS+=-fPIC
+CFLAGS+=-fPIC -Werror
 CFLAGS+=-Isrc $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(CFLAGS_xeninclude) -include $(XEN_ROOT)/tools/config.h
 
 LDLIBS-y = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) -lyajl
index 9421ca43c81082d6ce14fa0321129320934a060d..7530349eeecff4eb864291b6b9afaec2442c7567 100644 (file)
@@ -264,7 +264,7 @@ int xenstat_collect_networks(xenstat_node * node)
 {
        /* Helper variables for parseNetDevLine() function defined above */
        int i;
-       char line[512] = { 0 }, iface[16] = { 0 }, devBridge[16] = { 0 }, devNoBridge[16] = { 0 };
+       char line[512] = { 0 }, iface[16] = { 0 }, devBridge[16] = { 0 }, devNoBridge[17] = { 0 };
        unsigned long long rxBytes, rxPackets, rxErrs, rxDrops, txBytes, txPackets, txErrs, txDrops;
 
        struct priv_data *priv = get_priv_data(node->handle);
@@ -304,7 +304,7 @@ int xenstat_collect_networks(xenstat_node * node)
 
        /* We get the bridge devices for use with bonding interface to get bonding interface stats */
        getBridge("vir", devBridge, sizeof(devBridge));
-       snprintf(devNoBridge, 16, "p%s", devBridge);
+       snprintf(devNoBridge, sizeof(devNoBridge), "p%s", devBridge);
 
        while (fgets(line, 512, priv->procnetdev)) {
                xenstat_domain *domain;