[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 1/2] tools: use memcpy instead of strncpy in getBridge



Use memcpy in getBridge to prevent gcc warnings about truncated
strings. We know that we might truncate it, so the gcc warning
here is wrong.
Revert previous change changing buffer sizes as bigger buffers
are not needed.

Signed-off-by: Bertrand Marquis <bertrand.marquis@xxxxxxx>
---
Changes in v2:
 Use MIN between string length of de->d_name and resultLen to copy only
 the minimum size required and prevent crossing to from an unallocated
 space.
---
 tools/libs/stat/xenstat_linux.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/libs/stat/xenstat_linux.c b/tools/libs/stat/xenstat_linux.c
index d2ee6fda64..0ace03af1b 100644
--- a/tools/libs/stat/xenstat_linux.c
+++ b/tools/libs/stat/xenstat_linux.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <regex.h>
+#include <xen-tools/libs.h>
 
 #include "xenstat_priv.h"
 
@@ -78,7 +79,13 @@ static void getBridge(char *excludeName, char *result, 
size_t resultLen)
                                sprintf(tmp, "/sys/class/net/%s/bridge", 
de->d_name);
 
                                if (access(tmp, F_OK) == 0) {
-                                       strncpy(result, de->d_name, resultLen);
+                                       /*
+                                        * Do not use strncpy to prevent 
compiler warning with
+                                        * gcc >= 10.0
+                                        * If de->d_name is longer then 
resultLen we truncate it
+                                        */
+                                       memcpy(result, de->d_name, 
MIN(strnlen(de->d_name,
+                                                                       
sizeof(de->d_name)),resultLen - 1));
                                        result[resultLen - 1] = 0;
                                }
                }
@@ -264,7 +271,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[256] = { 0 }, 
devNoBridge[257] = { 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);
-- 
2.17.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.