Skip to contentSkip to conversation

tools: solve gcc 10 compilation issues

PATCH Superseded Bertrand Marquis 3 participants 9 messages Oct 5, 2020 – Oct 6, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover tools: solve gcc 10 compilation issues

Solve various issues in tools when compiling using a gcc version 10.0 or greater.

Bertrand Marquis (2):
tools: use memcpy instead of strncpy in getBridge tool/libx: Fix libxenlight gcc warning

 tools/libs/stat/xenstat_linux.c | 9 +++++++--
 tools/libxl/libxl_mem.c         | 2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 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.

---
 tools/libs/stat/xenstat_linux.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +7 -2
index d2ee6fda64..1db35c604c 100644
@@ -78,7 +78,12 @@ 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, resultLen - 1);
 					result[resultLen - 1] = 0;
 				}
 		}
@@ -264,7 +269,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);
Signature · 1 line
2.17.1
#2 lore raw

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

Quoted 7 lines · Bertrand Marquis

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.

Juergen, are you happy with this change? I have not followed closely the discussion on #xendevel.

Quoted 34 lines
---
 tools/libs/stat/xenstat_linux.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +7 -2
index d2ee6fda64..1db35c604c 100644
@@ -78,7 +78,12 @@ 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, resultLen - 1);
 					result[resultLen - 1] = 0;
 				}
 		}
@@ -264,7 +269,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);
Signature · 1 line
2.17.1
#3 lore raw

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

In reply to Bertrand Marquis
Quoted 16 lines · Bertrand Marquis

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.

---
  tools/libs/stat/xenstat_linux.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +0 -0
index d2ee6fda64..1db35c604c 100644

tools/libs/stat/xenstat_linux.c static void getBridge(char *excludeName, char *result, size_t resultLen) · line 86

Quoted 10 lines
tools/libs/stat/xenstat_linux.c +6 -1
@@ -78,7 +78,12 @@ 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, resultLen - 1);

I think you want min(NAME_MAX, resultLen - 1) for the length.

Juergen

#4 lore raw

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

Quoted 22 lines · Jürgen Groß
Quoted 17 lines · Bertrand Marquis

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.

---
 tools/libs/stat/xenstat_linux.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +0 -1
index d2ee6fda64..1db35c604c 100644
@@ -78,7 +78,12 @@ 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);
Quoted 8 lines

tools/libs/stat/xenstat_linux.c static void getBridge(char *excludeName, char *result, size_t resultLen) · line 86

Quoted 6 lines
tools/libs/stat/xenstat_linux.c +6 -0
+					/*
+					 * 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, resultLen - 1);

I think you want min(NAME_MAX, resultLen - 1) for the length.

true, I will fix that and send a v2.

Cheers
Bertrand

#5 lore raw

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

Quoted 31 lines · Bertrand Marquis
Quoted 22 lines · Jürgen Groß
Quoted 17 lines · Bertrand Marquis

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.

---
  tools/libs/stat/xenstat_linux.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +0 -1
index d2ee6fda64..1db35c604c 100644
@@ -78,7 +78,12 @@ 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);
Quoted 8 lines

tools/libs/stat/xenstat_linux.c static void getBridge(char *excludeName, char *result, size_t resultLen) · line 86

Quoted 6 lines
tools/libs/stat/xenstat_linux.c +6 -0
+					/*
+					 * 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, resultLen - 1);

I think you want min(NAME_MAX, resultLen - 1) for the length.

Quoted 1 line

true, I will fix that and send a v2.

Hmm, maybe you should use

min(strnlen(de->d_name, NAME_MAX), resultLen - 1)

for the case that de->d_name is near the end of a page, as otherwise you could try to copy unallocated space.

Juergen

#6 lore raw

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

Hi Jurgen,

Quoted 31 lines · Jürgen Groß
Quoted 28 lines · Bertrand Marquis
Quoted 28 lines · Jürgen Groß
Quoted 14 lines · Bertrand Marquis

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.

---
 tools/libs/stat/xenstat_linux.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libs/stat/xenstat_linux.c +0 -0
index d2ee6fda64..1db35c604c 100644

tools/libs/stat/xenstat_linux.c static void getBridge(char *excludeName, char *result, size_t resultLen) · line 85

Quoted 9 lines
tools/libs/stat/xenstat_linux.c +6 -1
@@ -78,7 +78,12 @@ 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, resultLen - 1);

I think you want min(NAME_MAX, resultLen - 1) for the length.

Quoted 9 lines
Quoted 1 line

true, I will fix that and send a v2.

Hmm, maybe you should use

min(strnlen(de->d_name, NAME_MAX), resultLen - 1)

for the case that de->d_name is near the end of a page, as otherwise you could try to copy unallocated space.

Agree, I will do that.

Cheers
Bertrand

#7 lore raw

PATCH 2/2 tool/libx: Fix libxenlight gcc warning

In reply to Bertrand Marquis

Fix gcc10 compilation warning about uninitialized variable by setting it to 0.

---
 tools/libxl/libxl_mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_mem.c +1 -1
index e52a9624ea..c739d00f39 100644
@@ -562,7 +562,7 @@ out:
 
 int libxl_get_free_memory_0x040700(libxl_ctx *ctx, uint32_t *memkb)
 {
-    uint64_t my_memkb;
+    uint64_t my_memkb = 0;
     int rc;
 
     rc = libxl_get_free_memory(ctx, &my_memkb);
Signature · 1 line
2.17.1