* [PATCH] tools/xenmpd: Fix gcc10 snprintf warning
@ 2020-10-14 10:47 Bertrand Marquis
2020-10-14 11:04 ` Julien Grall
0 siblings, 1 reply; 3+ messages in thread
From: Bertrand Marquis @ 2020-10-14 10:47 UTC (permalink / raw)
To: xen-devel; +Cc: Ian Jackson, Wei Liu
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=]
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 );
--
2.17.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] tools/xenmpd: Fix gcc10 snprintf warning
2020-10-14 10:47 [PATCH] tools/xenmpd: Fix gcc10 snprintf warning Bertrand Marquis
@ 2020-10-14 11:04 ` Julien Grall
2020-10-14 11:58 ` Bertrand Marquis
0 siblings, 1 reply; 3+ messages in thread
From: Julien Grall @ 2020-10-14 11:04 UTC (permalink / raw)
To: Bertrand Marquis, xen-devel; +Cc: Ian Jackson, Wei Liu
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] tools/xenmpd: Fix gcc10 snprintf warning
2020-10-14 11:04 ` Julien Grall
@ 2020-10-14 11:58 ` Bertrand Marquis
0 siblings, 0 replies; 3+ messages in thread
From: Bertrand Marquis @ 2020-10-14 11:58 UTC (permalink / raw)
To: Julien Grall; +Cc: open list:X86, Ian Jackson, Wei Liu
Hi Julien,
> On 14 Oct 2020, at 12:04, Julien Grall <julien@xen.org> wrote:
>
> 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 can confirm this is the only build catching it on my side.
I will modify the commit message to say that the problem was encountered on arm32 build with the optimizer enabled.
>
> I would also suggest to link to the bug reported on Debian.
I will add it to the commit message.
Cheers
Bertrand
>
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-10-14 11:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-14 10:47 [PATCH] tools/xenmpd: Fix gcc10 snprintf warning Bertrand Marquis
2020-10-14 11:04 ` Julien Grall
2020-10-14 11:58 ` 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.