xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Julien Grall <julien@xen.org>
To: Bertrand Marquis <bertrand.marquis@arm.com>,
	xen-devel@lists.xenproject.org
Cc: Ian Jackson <iwj@xenproject.org>, Wei Liu <wl@xen.org>
Subject: Re: [PATCH] tools/xenmpd: Fix gcc10 snprintf warning
Date: Wed, 14 Oct 2020 12:04:47 +0100	[thread overview]
Message-ID: <74625fd9-f2a3-14be-714a-3cfb705434cc@xen.org> (raw)
In-Reply-To: <0ade4264c537819c3dd45179fcea2723df66b045.1602672245.git.bertrand.marquis@arm.com>

Hi,

On 14/10/2020 11:47, Bertrand Marquis wrote:
> Add a check for snprintf return code and ignore the entry if we get an
> error. This should in fact never happen and is more a trick to make gcc
> happy and prevent compilation errors.
> 
> This is solving the gcc warning:
> xenpmd.c:92:37: error: '%s' directive output may be truncated writing
> between 4 and 2147483645 bytes into a region of size 271
> [-Werror=format-truncation=]

IIRC, this is only affecting GCC when building for Arm32 *and* when the 
optimizer is enabled. If so, it would be good to add more details in the 
commit message.

I would also suggest to link to the bug reported on Debian.

Cheers,

> 
> Signed-off-by: Bertrand Marquis <bertrand.marquis@arm.com>
> ---
>   tools/xenpmd/xenpmd.c | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/xenpmd/xenpmd.c b/tools/xenpmd/xenpmd.c
> index 35fd1c931a..12b82cf43e 100644
> --- a/tools/xenpmd/xenpmd.c
> +++ b/tools/xenpmd/xenpmd.c
> @@ -102,6 +102,7 @@ FILE *get_next_battery_file(DIR *battery_dir,
>       FILE *file = 0;
>       struct dirent *dir_entries;
>       char file_name[284];
> +    int ret;
>       
>       do
>       {
> @@ -111,11 +112,15 @@ FILE *get_next_battery_file(DIR *battery_dir,
>           if ( strlen(dir_entries->d_name) < 4 )
>               continue;
>           if ( battery_info_type == BIF )
> -            snprintf(file_name, sizeof(file_name), BATTERY_INFO_FILE_PATH,
> +            ret = snprintf(file_name, sizeof(file_name), BATTERY_INFO_FILE_PATH,
>                        dir_entries->d_name);
>           else
> -            snprintf(file_name, sizeof(file_name), BATTERY_STATE_FILE_PATH,
> +            ret = snprintf(file_name, sizeof(file_name), BATTERY_STATE_FILE_PATH,
>                        dir_entries->d_name);
> +        /* This should not happen but is needed to pass gcc checks */
> +        if (ret < 0)
> +            continue;
> +        file_name[sizeof(file_name) - 1] = '\0';
>           file = fopen(file_name, "r");
>       } while ( !file );
>   
> 

Cheers,

-- 
Julien Grall


  reply	other threads:[~2020-10-14 11:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-14 10:47 [PATCH] tools/xenmpd: Fix gcc10 snprintf warning Bertrand Marquis
2020-10-14 11:04 ` Julien Grall [this message]
2020-10-14 11:58   ` Bertrand Marquis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=74625fd9-f2a3-14be-714a-3cfb705434cc@xen.org \
    --to=julien@xen.org \
    --cc=bertrand.marquis@arm.com \
    --cc=iwj@xenproject.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).