All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7
@ 2017-06-14  1:11 Zhongze Liu
  2017-06-19 11:39 ` Wei Liu
  0 siblings, 1 reply; 5+ messages in thread
From: Zhongze Liu @ 2017-06-14  1:11 UTC (permalink / raw)
  To: xen-devel; +Cc: Wei Liu, Ian Jackson, Zhongze Liu, David Scott

GCC 7.1.1 complains that several buffers passed to snprintf() in xenpmd
and tools/ocmal/xc are too small to hold the largest possible resulting string,
which is calculated by adding up the maximum length of all the substrings.

The warnings are treated as errors by -Werror, and goes like this (abbreviated):

xenpmd.c:94:36: error: ‘%s’ directive output may be truncated writing up to
255 bytes into a region of size 13 [-Werror=format-truncation=]
     #define BATTERY_INFO_FILE_PATH "/proc/acpi/battery/%s/info"
                                    ^
xenpmd.c:113:13: note: ‘snprintf’ output between 25 and 280 bytes into a
destination of size 32

xenpmd.c:95:37: error: ‘%s’ directive output may be truncated writing up to
255 bytes into a region of size 13 [-Werror=format-truncation=]
     #define BATTERY_STATE_FILE_PATH "/proc/acpi/battery/%s/state"
                                     ^
xenpmd.c:116:13: note: ‘snprintf’ output between 26 and 281 bytes into a
destination of size 32

xenctrl_stubs.c:65:15: error: ‘%s’ directive output may be truncated writing
up to 1023 bytes into a region of size 252 [-Werror=format-truncation=]
      "%d: %s: %s", error->code,
               ^~
xenctrl_stubs.c:64:4: note: ‘snprintf’ output 5 or more bytes (assuming 1028)
into a destination of size 256

Enlarge the size of these buffers as suggested by the complier
(and slightly rounded) to fix the warnings.

No functional changes.

Signed-off-by: Zhongze Liu <blackskygg@gmail.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
CC: David Scott <dave@recoil.org>
CC: Ian Jackson <ian.jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>

Changed since v2:
  * Decrease the size of the file_name[] buffer properly to save more space.
  * Explain how the new sizes of the buffers are chosen in the commit message.
  * Include part of the error messages in the commit message.

Changed since v1:
  * Changed the word "error" to "warning" in title and description
  * Enlarge the buffer instead of replacing snprintf() with asprintf()
---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 2 +-
 tools/xenpmd/xenpmd.c               | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c
index 5e455519d4..f1b28db53a 100644
--- a/tools/ocaml/libs/xc/xenctrl_stubs.c
+++ b/tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -54,7 +54,7 @@
 
 static void Noreturn failwith_xc(xc_interface *xch)
 {
-	char error_str[256];
+	char error_str[1028];
 	if (xch) {
 		const xc_error *error = xc_get_last_error(xch);
 		if (error->code == XC_ERROR_NONE)
diff --git a/tools/xenpmd/xenpmd.c b/tools/xenpmd/xenpmd.c
index b3a31062aa..689c8fd670 100644
--- a/tools/xenpmd/xenpmd.c
+++ b/tools/xenpmd/xenpmd.c
@@ -100,7 +100,7 @@ FILE *get_next_battery_file(DIR *battery_dir,
 {
     FILE *file = 0;
     struct dirent *dir_entries;
-    char file_name[32];
+    char file_name[284];
     
     do 
     {
@@ -110,10 +110,10 @@ FILE *get_next_battery_file(DIR *battery_dir,
         if ( strlen(dir_entries->d_name) < 4 )
             continue;
         if ( battery_info_type == BIF ) 
-            snprintf(file_name, 32, BATTERY_INFO_FILE_PATH,
+            snprintf(file_name, sizeof(file_name), BATTERY_INFO_FILE_PATH,
                      dir_entries->d_name);
         else 
-            snprintf(file_name, 32, BATTERY_STATE_FILE_PATH,
+            snprintf(file_name, sizeof(file_name), BATTERY_STATE_FILE_PATH,
                      dir_entries->d_name);
         file = fopen(file_name, "r");
     } while ( !file );
-- 
2.13.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

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

* Re: [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7
  2017-06-14  1:11 [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7 Zhongze Liu
@ 2017-06-19 11:39 ` Wei Liu
  2017-06-20 10:58   ` Julien Grall
  0 siblings, 1 reply; 5+ messages in thread
From: Wei Liu @ 2017-06-19 11:39 UTC (permalink / raw)
  To: Zhongze Liu; +Cc: xen-devel, Julien Grall, Ian Jackson, Wei Liu, David Scott

On Wed, Jun 14, 2017 at 09:11:48AM +0800, Zhongze Liu wrote:
> GCC 7.1.1 complains that several buffers passed to snprintf() in xenpmd
> and tools/ocmal/xc are too small to hold the largest possible resulting string,
> which is calculated by adding up the maximum length of all the substrings.
> 
> The warnings are treated as errors by -Werror, and goes like this (abbreviated):
> 
> xenpmd.c:94:36: error: ‘%s’ directive output may be truncated writing up to
> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>      #define BATTERY_INFO_FILE_PATH "/proc/acpi/battery/%s/info"
>                                     ^
> xenpmd.c:113:13: note: ‘snprintf’ output between 25 and 280 bytes into a
> destination of size 32
> 
> xenpmd.c:95:37: error: ‘%s’ directive output may be truncated writing up to
> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>      #define BATTERY_STATE_FILE_PATH "/proc/acpi/battery/%s/state"
>                                      ^
> xenpmd.c:116:13: note: ‘snprintf’ output between 26 and 281 bytes into a
> destination of size 32
> 
> xenctrl_stubs.c:65:15: error: ‘%s’ directive output may be truncated writing
> up to 1023 bytes into a region of size 252 [-Werror=format-truncation=]
>       "%d: %s: %s", error->code,
>                ^~
> xenctrl_stubs.c:64:4: note: ‘snprintf’ output 5 or more bytes (assuming 1028)
> into a destination of size 256
> 
> Enlarge the size of these buffers as suggested by the complier
> (and slightly rounded) to fix the warnings.
> 
> No functional changes.
> 
> Signed-off-by: Zhongze Liu <blackskygg@gmail.com>
> Acked-by: Wei Liu <wei.liu2@citrix.com>

Applied.

Julien, consider this for 4.9?

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

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

* Re: [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7
  2017-06-19 11:39 ` Wei Liu
@ 2017-06-20 10:58   ` Julien Grall
  2017-06-20 11:02     ` Wei Liu
  0 siblings, 1 reply; 5+ messages in thread
From: Julien Grall @ 2017-06-20 10:58 UTC (permalink / raw)
  To: Wei Liu, Zhongze Liu; +Cc: xen-devel, Ian Jackson, David Scott

Hi Wei,

On 06/19/2017 12:39 PM, Wei Liu wrote:
> On Wed, Jun 14, 2017 at 09:11:48AM +0800, Zhongze Liu wrote:
>> GCC 7.1.1 complains that several buffers passed to snprintf() in xenpmd
>> and tools/ocmal/xc are too small to hold the largest possible resulting string,
>> which is calculated by adding up the maximum length of all the substrings.
>>
>> The warnings are treated as errors by -Werror, and goes like this (abbreviated):
>>
>> xenpmd.c:94:36: error: ‘%s’ directive output may be truncated writing up to
>> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>>       #define BATTERY_INFO_FILE_PATH "/proc/acpi/battery/%s/info"
>>                                      ^
>> xenpmd.c:113:13: note: ‘snprintf’ output between 25 and 280 bytes into a
>> destination of size 32
>>
>> xenpmd.c:95:37: error: ‘%s’ directive output may be truncated writing up to
>> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>>       #define BATTERY_STATE_FILE_PATH "/proc/acpi/battery/%s/state"
>>                                       ^
>> xenpmd.c:116:13: note: ‘snprintf’ output between 26 and 281 bytes into a
>> destination of size 32
>>
>> xenctrl_stubs.c:65:15: error: ‘%s’ directive output may be truncated writing
>> up to 1023 bytes into a region of size 252 [-Werror=format-truncation=]
>>        "%d: %s: %s", error->code,
>>                 ^~
>> xenctrl_stubs.c:64:4: note: ‘snprintf’ output 5 or more bytes (assuming 1028)
>> into a destination of size 256
>>
>> Enlarge the size of these buffers as suggested by the complier
>> (and slightly rounded) to fix the warnings.
>>
>> No functional changes.
>>
>> Signed-off-by: Zhongze Liu <blackskygg@gmail.com>
>> Acked-by: Wei Liu <wei.liu2@citrix.com>
> 
> Applied.
> 
> Julien, consider this for 4.9?

Looking quickly at the patch, it sounds a bit strange to allocate 1028 
bytes on the stack. It is more than 1/8 of default stack in Linux (8K). 
Are we sure the stack will never explode?

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

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

* Re: [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7
  2017-06-20 10:58   ` Julien Grall
@ 2017-06-20 11:02     ` Wei Liu
  2017-06-20 11:06       ` Julien Grall
  0 siblings, 1 reply; 5+ messages in thread
From: Wei Liu @ 2017-06-20 11:02 UTC (permalink / raw)
  To: Julien Grall; +Cc: Ian Jackson, xen-devel, Wei Liu, Zhongze Liu, David Scott

On Tue, Jun 20, 2017 at 11:58:53AM +0100, Julien Grall wrote:
> Hi Wei,
> 
> On 06/19/2017 12:39 PM, Wei Liu wrote:
> > On Wed, Jun 14, 2017 at 09:11:48AM +0800, Zhongze Liu wrote:
> > > GCC 7.1.1 complains that several buffers passed to snprintf() in xenpmd
> > > and tools/ocmal/xc are too small to hold the largest possible resulting string,
> > > which is calculated by adding up the maximum length of all the substrings.
> > > 
> > > The warnings are treated as errors by -Werror, and goes like this (abbreviated):
> > > 
> > > xenpmd.c:94:36: error: ‘%s’ directive output may be truncated writing up to
> > > 255 bytes into a region of size 13 [-Werror=format-truncation=]
> > >       #define BATTERY_INFO_FILE_PATH "/proc/acpi/battery/%s/info"
> > >                                      ^
> > > xenpmd.c:113:13: note: ‘snprintf’ output between 25 and 280 bytes into a
> > > destination of size 32
> > > 
> > > xenpmd.c:95:37: error: ‘%s’ directive output may be truncated writing up to
> > > 255 bytes into a region of size 13 [-Werror=format-truncation=]
> > >       #define BATTERY_STATE_FILE_PATH "/proc/acpi/battery/%s/state"
> > >                                       ^
> > > xenpmd.c:116:13: note: ‘snprintf’ output between 26 and 281 bytes into a
> > > destination of size 32
> > > 
> > > xenctrl_stubs.c:65:15: error: ‘%s’ directive output may be truncated writing
> > > up to 1023 bytes into a region of size 252 [-Werror=format-truncation=]
> > >        "%d: %s: %s", error->code,
> > >                 ^~
> > > xenctrl_stubs.c:64:4: note: ‘snprintf’ output 5 or more bytes (assuming 1028)
> > > into a destination of size 256
> > > 
> > > Enlarge the size of these buffers as suggested by the complier
> > > (and slightly rounded) to fix the warnings.
> > > 
> > > No functional changes.
> > > 
> > > Signed-off-by: Zhongze Liu <blackskygg@gmail.com>
> > > Acked-by: Wei Liu <wei.liu2@citrix.com>
> > 
> > Applied.
> > 
> > Julien, consider this for 4.9?
> 
> Looking quickly at the patch, it sounds a bit strange to allocate 1028 bytes
> on the stack. It is more than 1/8 of default stack in Linux (8K). Are we
> sure the stack will never explode?
> 

The default stack size is 8M, not 8K so we it be fine.

> Cheers,
> 
> -- 
> Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

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

* Re: [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7
  2017-06-20 11:02     ` Wei Liu
@ 2017-06-20 11:06       ` Julien Grall
  0 siblings, 0 replies; 5+ messages in thread
From: Julien Grall @ 2017-06-20 11:06 UTC (permalink / raw)
  To: Wei Liu; +Cc: xen-devel, Ian Jackson, Zhongze Liu, David Scott

On 06/20/2017 12:02 PM, Wei Liu wrote:
> On Tue, Jun 20, 2017 at 11:58:53AM +0100, Julien Grall wrote:
>> Hi Wei,
>>
>> On 06/19/2017 12:39 PM, Wei Liu wrote:
>>> On Wed, Jun 14, 2017 at 09:11:48AM +0800, Zhongze Liu wrote:
>>>> GCC 7.1.1 complains that several buffers passed to snprintf() in xenpmd
>>>> and tools/ocmal/xc are too small to hold the largest possible resulting string,
>>>> which is calculated by adding up the maximum length of all the substrings.
>>>>
>>>> The warnings are treated as errors by -Werror, and goes like this (abbreviated):
>>>>
>>>> xenpmd.c:94:36: error: ‘%s’ directive output may be truncated writing up to
>>>> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>>>>        #define BATTERY_INFO_FILE_PATH "/proc/acpi/battery/%s/info"
>>>>                                       ^
>>>> xenpmd.c:113:13: note: ‘snprintf’ output between 25 and 280 bytes into a
>>>> destination of size 32
>>>>
>>>> xenpmd.c:95:37: error: ‘%s’ directive output may be truncated writing up to
>>>> 255 bytes into a region of size 13 [-Werror=format-truncation=]
>>>>        #define BATTERY_STATE_FILE_PATH "/proc/acpi/battery/%s/state"
>>>>                                        ^
>>>> xenpmd.c:116:13: note: ‘snprintf’ output between 26 and 281 bytes into a
>>>> destination of size 32
>>>>
>>>> xenctrl_stubs.c:65:15: error: ‘%s’ directive output may be truncated writing
>>>> up to 1023 bytes into a region of size 252 [-Werror=format-truncation=]
>>>>         "%d: %s: %s", error->code,
>>>>                  ^~
>>>> xenctrl_stubs.c:64:4: note: ‘snprintf’ output 5 or more bytes (assuming 1028)
>>>> into a destination of size 256
>>>>
>>>> Enlarge the size of these buffers as suggested by the complier
>>>> (and slightly rounded) to fix the warnings.
>>>>
>>>> No functional changes.
>>>>
>>>> Signed-off-by: Zhongze Liu <blackskygg@gmail.com>
>>>> Acked-by: Wei Liu <wei.liu2@citrix.com>
>>>
>>> Applied.
>>>
>>> Julien, consider this for 4.9?
>>
>> Looking quickly at the patch, it sounds a bit strange to allocate 1028 bytes
>> on the stack. It is more than 1/8 of default stack in Linux (8K). Are we
>> sure the stack will never explode?
>>
> 
> The default stack size is 8M, not 8K so we it be fine.

Sorry I misread the unit.

Release-acked-by: Julien Grall <julien.grall@arm.com>

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

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

end of thread, other threads:[~2017-06-20 11:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-14  1:11 [PATCH v3] tools: fix several "format-truncation" warnings with GCC 7 Zhongze Liu
2017-06-19 11:39 ` Wei Liu
2017-06-20 10:58   ` Julien Grall
2017-06-20 11:02     ` Wei Liu
2017-06-20 11:06       ` Julien Grall

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.