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

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


  • To: "open list:X86" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Bertrand Marquis <Bertrand.Marquis@xxxxxxx>
  • Date: Wed, 14 Oct 2020 10:58:33 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=u/z+3GxGiuGnOI9OlFaRm6nlrVxjp/eazqjpMJaIMXI=; b=hZfdTvNv7jxT96uFhqRw9K4TaMwYxxGWVV/8NUzteM0qXe08RI2PxJyOX2zEm/TjCeVGfIFB75o4IxaDKQbS9vEuYBWldcIoJ/WZ8/vIPOKXrLtQBkhADflstLDPXDCV0sU1S/bNK7jy+VRMjhydzXYeiC79+1ONRz0d5K/HAZ2RYI6r1VUdZpGalaVgyNbva4y547rvMOogR45NiEIqqsQmxOEoiOHFrH9Y5mzSfEMu1sU1lw7yfTZyl180c+h4Whg0hO+tBc2YvsAdYdo73fqx2NcuLU+vv6j0EqgC2lybetH3aF/1N7g0GUrsfol6IypBITRp0TO8ng/88MdATg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BtwDJWwZjFJXr/suui0z/3gPUsn5RZTlB94diPZOi44Q/aIg/D0j/L6lo0aQ4P/QbN47O7kNsC8/iZ8dS2vA5wDKRxdk8VzIY1KjYWWcwBW60Pi3ums24S/4xQ9hafFrXLdYwQUcKzt1gs+XpGscty3pu+qgsZ2rTbKv5Ds3eb2a2+ZRT5Uu8ze5Oi9y3joI408q3wThQ+zXm5fdCTBGBWAT5ORUeXabobzWhdceEctiB3MHsL2AxY/C3BGuy34bNfy3aSbS8+07RpYFP8oBi8e+06zbkzAT4kqxnZ6cl/rJFP3o6Z/2kUM3tAjSa/UkOtgMQPCVoSRMGxbzCMleQQ==
  • Authentication-results-original: lists.xenproject.org; dkim=none (message not signed) header.d=none;lists.xenproject.org; dmarc=none action=none header.from=arm.com;
  • Cc: "jgross@xxxxxxxx" <jgross@xxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Wed, 14 Oct 2020 10:59:17 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Nodisclaimer: true
  • Original-authentication-results: lists.xenproject.org; dkim=none (message not signed) header.d=none;lists.xenproject.org; dmarc=none action=none header.from=arm.com;
  • Thread-index: AQHWnLHiDj1hArywQEGBhk10fWLXEamW+IUA
  • Thread-topic: [PATCH v3 1/2] tools/libs/stat: use memcpy instead of strncpy in getBridge

Hi,

Could this be reviewed so that gcc10 issues are fixed ?

Thanks
Bertrand

> On 7 Oct 2020, at 14:57, Bertrand Marquis <bertrand.marquis@xxxxxxx> 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@xxxxxxx>
> ---
> 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
> 
> 




 


Rackspace

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