All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge
@ 2020-10-07 13:57 Bertrand Marquis
  2020-10-07 13:57 ` [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning Bertrand Marquis
  2020-10-14 10:58 ` [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
  0 siblings, 2 replies; 6+ messages in thread
From: Bertrand Marquis @ 2020-10-07 13:57 UTC (permalink / raw)
  To: xen-devel; +Cc: jgross, Ian Jackson, Wei Liu

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@arm.com>
---
Changes in v3:
 Do a memset 0 on destination buffer and use MIN between string length
 and resultLen - 1.
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 | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/tools/libs/stat/xenstat_linux.c b/tools/libs/stat/xenstat_linux.c
index d2ee6fda64..e0d242e1bc 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,8 +79,14 @@ 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);
-					result[resultLen - 1] = 0;
+					/*
+					 * Do not use strncpy to prevent compiler warning with
+					 * gcc >= 10.0
+					 * If de->d_name is longer then resultLen we truncate it
+					 */
+					memset(result, 0, resultLen);
+					memcpy(result, de->d_name, MIN(strnlen(de->d_name,
+									NAME_MAX),resultLen - 1));
 				}
 		}
 	}
@@ -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



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning
  2020-10-07 13:57 [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
@ 2020-10-07 13:57 ` Bertrand Marquis
  2020-10-08 12:57   ` Wei Liu
  2020-10-14 10:58 ` [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
  1 sibling, 1 reply; 6+ messages in thread
From: Bertrand Marquis @ 2020-10-07 13:57 UTC (permalink / raw)
  To: xen-devel; +Cc: jgross, Ian Jackson, Wei Liu

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

Signed-off-by: Bertrand Marquis <bertrand.marquis@arm.com>
---
Changes in v3: Rebase
---
 tools/libs/light/libxl_mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/libs/light/libxl_mem.c b/tools/libs/light/libxl_mem.c
index e52a9624ea..c739d00f39 100644
--- a/tools/libs/light/libxl_mem.c
+++ b/tools/libs/light/libxl_mem.c
@@ -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);
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning
  2020-10-07 13:57 ` [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning Bertrand Marquis
@ 2020-10-08 12:57   ` Wei Liu
  0 siblings, 0 replies; 6+ messages in thread
From: Wei Liu @ 2020-10-08 12:57 UTC (permalink / raw)
  To: Bertrand Marquis; +Cc: xen-devel, jgross, Ian Jackson, Wei Liu

On Wed, Oct 07, 2020 at 02:57:02PM +0100, Bertrand Marquis wrote:
> Fix gcc10 compilation warning about uninitialized variable by setting
> it to 0.
> 
> Signed-off-by: Bertrand Marquis <bertrand.marquis@arm.com>

Applied.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge
  2020-10-07 13:57 [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
  2020-10-07 13:57 ` [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning Bertrand Marquis
@ 2020-10-14 10:58 ` Bertrand Marquis
  2020-10-14 15:47   ` Wei Liu
  1 sibling, 1 reply; 6+ messages in thread
From: Bertrand Marquis @ 2020-10-14 10:58 UTC (permalink / raw)
  To: open list:X86; +Cc: jgross, Ian Jackson, Wei Liu

Hi,

Could this be reviewed so that gcc10 issues are fixed ?

Thanks
Bertrand

> On 7 Oct 2020, at 14:57, Bertrand Marquis <bertrand.marquis@arm.com> wrote:
> 
> 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@arm.com>
> ---
> Changes in v3:
> Do a memset 0 on destination buffer and use MIN between string length
> and resultLen - 1.
> 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 | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/libs/stat/xenstat_linux.c b/tools/libs/stat/xenstat_linux.c
> index d2ee6fda64..e0d242e1bc 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,8 +79,14 @@ 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);
> -					result[resultLen - 1] = 0;
> +					/*
> +					 * Do not use strncpy to prevent compiler warning with
> +					 * gcc >= 10.0
> +					 * If de->d_name is longer then resultLen we truncate it
> +					 */
> +					memset(result, 0, resultLen);
> +					memcpy(result, de->d_name, MIN(strnlen(de->d_name,
> +									NAME_MAX),resultLen - 1));
> 				}
> 		}
> 	}
> @@ -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
> 
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge
  2020-10-14 10:58 ` [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
@ 2020-10-14 15:47   ` Wei Liu
  2020-10-14 15:54     ` Bertrand Marquis
  0 siblings, 1 reply; 6+ messages in thread
From: Wei Liu @ 2020-10-14 15:47 UTC (permalink / raw)
  To: Bertrand Marquis; +Cc: open list:X86, jgross, Ian Jackson, Wei Liu

On Wed, Oct 14, 2020 at 10:58:33AM +0000, Bertrand Marquis wrote:
> Hi,
> 
> Could this be reviewed so that gcc10 issues are fixed ?

I think Juergen's comments have been addressed.

Acked-by: Wei Liu <wl@xen.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge
  2020-10-14 15:47   ` Wei Liu
@ 2020-10-14 15:54     ` Bertrand Marquis
  0 siblings, 0 replies; 6+ messages in thread
From: Bertrand Marquis @ 2020-10-14 15:54 UTC (permalink / raw)
  To: Wei Liu; +Cc: open list:X86, jgross, Ian Jackson



> On 14 Oct 2020, at 16:47, Wei Liu <wl@xen.org> wrote:
> 
> On Wed, Oct 14, 2020 at 10:58:33AM +0000, Bertrand Marquis wrote:
>> Hi,
>> 
>> Could this be reviewed so that gcc10 issues are fixed ?
> 
> I think Juergen's comments have been addressed.
> 
> Acked-by: Wei Liu <wl@xen.org>

Thanks :-)


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-10-14 15:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-07 13:57 [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
2020-10-07 13:57 ` [PATCH v3 2/2] tool/libs/light: Fix libxenlight gcc warning Bertrand Marquis
2020-10-08 12:57   ` Wei Liu
2020-10-14 10:58 ` [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge Bertrand Marquis
2020-10-14 15:47   ` Wei Liu
2020-10-14 15:54     ` Bertrand Marquis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.