Skip to contentSkip to conversation

tools/xenstat: Fix -Wformat-truncation= issue

PATCH In review Andrew Cooper 2 participants 4 messages Aug 13, 2019 – Aug 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenstat: Fix -Wformat-truncation= issue

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.

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>

---
 tools/xenstat/libxenstat/Makefile            | 2 +-
 tools/xenstat/libxenstat/src/xenstat_linux.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/Makefile +1 -1
index 58f9d63de5..ea115ae0e6 100644
@@ -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
tools/xenstat/libxenstat/src/xenstat_linux.c +2 -2
index 9421ca43c8..7530349eee 100644
@@ -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;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

[Xen-devel] [PATCH 2/1] toos/xenstat: Fix -Wunused-function issue

When compiling xenstat with -Werror, Clang complains:

src/xenstat.c:134:34: error: unused function 'parse' [-Werror,-Wunused-function] static inline unsigned long long parse(char *s, char *match)

                                   ^

1 error generated.

Drop the function. It really is unused.

Spotted by Travis-CI.

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>

---
 tools/xenstat/libxenstat/src/xenstat.c | 14 --------------
 1 file changed, 14 deletions(-)
tools/xenstat/libxenstat/src/xenstat.c +0 -14
index bba143eb53..6f93d4e982 100644
@@ -131,20 +131,6 @@ void xenstat_uninit(xenstat_handle * handle)
 	}
 }
 
-static inline unsigned long long parse(char *s, char *match)
-{
-	char *s1 = strstr(s,match);
-	unsigned long long ret;
-
-	if ( s1 == NULL )
-		return 0LL;
-	s1 += 2;
-	if ( *s1++ != ':' )
-		return 0LL;
-	sscanf(s1,"%llu",&ret);
-	return ret;
-}
-
 xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags)
 {
 #define DOMAIN_CHUNK_SIZE 256
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by
#2 lore raw

Re: [Xen-devel] [PATCH 2/1] toos/xenstat: Fix -Wunused-function issue

Quoted 4 lines · Andrew Cooper

When compiling xenstat with -Werror, Clang complains:

src/xenstat.c:134:34: error: unused function 'parse' [-Werror,-Wunused-function] static inline unsigned long long parse(char *s, char *match)

Quoted 8 lines
                                   ^

1 error generated.

Drop the function. It really is unused.

Spotted by Travis-CI.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 12 lines · Andrew Cooper

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);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines

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.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel