Skip to contentSkip to conversation

tools: fix several "format-truncation" warnings with GCC 7

PATCH Applied Zhongze Liu 3 participants 5 messages Jun 14, 2017 – Jun 20, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 tools: fix several "format-truncation" warnings with GCC 7

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.

---
CC: David Scott <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>

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(-)
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index 5e455519d4..f1b28db53a 100644
@@ -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)
tools/xenpmd/xenpmd.c +3 -3
index b3a31062aa..689c8fd670 100644
@@ -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 );
Signature · 7 lines
2.13.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Applied
#1 lore raw
Quoted 27 lines · Zhongze Liu

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

Quoted 7 lines

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

No functional changes.

Applied.

Julien, consider this for 4.9?

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw

Hi Wei,

Quoted 31 lines · Wei Liu
Quoted 30 lines · Zhongze Liu

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.

Quoted 8 lines
Quoted 4 lines

No functional changes.

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,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw
Quoted 39 lines · Julien Grall

Hi Wei,

Quoted 36 lines · Wei Liu
Quoted 34 lines · Zhongze Liu

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.

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines

Cheers,

Signature · 1 line
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 42 lines · Wei Liu
Quoted 41 lines · Julien Grall

Hi Wei,

Quoted 38 lines · Wei Liu
Quoted 27 lines · Zhongze Liu

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

Quoted 7 lines

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

No functional changes.

Applied.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

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.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel