Skip to contentSkip to conversation

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

PATCH Superseded Zhongze Liu 3 participants 5 messages Jun 12, 2017 – Jun 13, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

enlarge the size of these buffers to fix the warnings.

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

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..df96f2683f 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[512];
     
     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
#2 lore raw
In reply to Zhongze Liu

Zhongze Liu writes ("[PATCH v2] tools: fix several "format-truncation" warnings with GCC 7"):

Quoted 5 lines

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.

enlarge the size of these buffers to fix the warnings.

This is probably a reasonable approach. But, I can't see where you got the new maximum sizes. Forgive me if I missed a mail, but I think at least the commit message should explain how each of the new sizes is calculated.

Thanks,
Ian.

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

#3 lore raw
Quoted 11 lines · Ian Jackson

Zhongze Liu writes ("[PATCH v2] tools: fix several "format-truncation" warnings with GCC 7"):

Quoted 5 lines

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.

enlarge the size of these buffers to fix the warnings.

This is probably a reasonable approach. But, I can't see where you got the new maximum sizes. Forgive me if I missed a mail, but I think at least the commit message should explain how each of the new sizes is calculated.

Zhongze, please summarise what we discussed on IRC, especially the part we figured out the various buffer sizes, in the commit message.

Ideally you should include the exact error messages in the commit message, too.

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

#4 lore raw

Hi Wei & Ian,

2017-06-13 18:52 GMT+08:00 Wei Liu <[email protected]>:

Quoted 6 lines · Ian Jackson
Quoted 5 lines

Zhongze Liu writes ("[PATCH v2] tools: fix several "format-truncation" warnings with GCC 7"):

Quoted 4 lines

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.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

enlarge the size of these buffers to fix the warnings.

This is probably a reasonable approach. But, I can't see where you got the new maximum sizes. Forgive me if I missed a mail, but I think at least the commit message should explain how each of the new sizes is calculated.

OK, I'll add that.

Quoted 6 lines

Zhongze, please summarise what we discussed on IRC, especially the part we figured out the various buffer sizes, in the commit message.

Ideally you should include the exact error messages in the commit message, too.

Will do.

Cheers,

Zhongze Liu

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