Skip to contentSkip to conversation

libxc: Fix buffer length for get_suspend_file

PATCH Superseded Ian Jackson 3 participants 7 messages Mar 18, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxc: Fix buffer length for get_suspend_file

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array parameter, as it is misleading.

Newer versions of gcc warn about the erroneous code:
xc_suspend.c:39:25: error: argument to 'sizeof' in 'snprintf' call
is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]

---
 tools/libxc/xc_suspend.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_suspend.c +1 -1
index 84ee139..798eaaa 100644
@@ -36,7 +36,7 @@
 
 static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
 {
-    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+    snprintf(buf, SUSPEND_FILE_BUFLEN, SUSPEND_LOCK_FILE, domid);
 }
 
 static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Ian Jackson

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array parameter, as it is misleading.

Did you actually do this bit?

Quoted 7 lines

Newer versions of gcc warn about the erroneous code:
xc_suspend.c:39:25: error: argument to 'sizeof' in 'snprintf' call
is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]

Quoted 18 lines
CC Julien Grall <[email protected]>
---
 tools/libxc/xc_suspend.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_suspend.c +1 -1
index 84ee139..798eaaa 100644
@@ -36,7 +36,7 @@
 
 static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
 {
-    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+    snprintf(buf, SUSPEND_FILE_BUFLEN, SUSPEND_LOCK_FILE, domid);
 }
 
 static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
#3 lore raw

PATCH v2 1/1 libxc: Fix buffer length for get_suspend_file

In reply to Ian Jackson

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array size, as it is misleading.

Newer versions of gcc warn about the erroneous code:
xc_suspend.c:39:25: error: argument to 'sizeof' in 'snprintf' call
is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]

--
v2: Actually change the declaration of buf.

---
 tools/libxc/xc_suspend.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxc/xc_suspend.c +2 -2
index 84ee139..200d381 100644
@@ -34,9 +34,9 @@
 
 #define SUSPEND_FILE_BUFLEN (sizeof(SUSPEND_LOCK_FILE) + 10)
 
-static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
+static void get_suspend_file(char buf[], int domid)
 {
-    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+    snprintf(buf, SUSPEND_FILE_BUFLEN, SUSPEND_LOCK_FILE, domid);
 }
 
 static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to Ian Campbell

Ian Campbell writes ("Re: [PATCH] libxc: Fix buffer length for get_suspend_file"):

Quoted 6 lines · Ian Jackson
Quoted 3 lines

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array parameter, as it is misleading.

Did you actually do this bit?

Yes, but apparently I didn't include it in the patch. See v2.

Thanks,
Ian.

Acked-by on v2 1/1
#4 lore raw
In reply to Ian Jackson
Quoted 8 lines · Ian Jackson

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array size, as it is misleading.

Quoted 7 lines

Newer versions of gcc warn about the erroneous code:
xc_suspend.c:39:25: error: argument to 'sizeof' in 'snprintf' call
is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]

Although I do wonder if it would be less error prone to push the size up to a parameter to get_suspend_file where it is (presumably) near to the declaration of the array.

Quoted 24 lines
CC Julien Grall <[email protected]>

--
v2: Actually change the declaration of buf.

---
 tools/libxc/xc_suspend.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxc/xc_suspend.c +2 -2
index 84ee139..200d381 100644
@@ -34,9 +34,9 @@
 
 #define SUSPEND_FILE_BUFLEN (sizeof(SUSPEND_LOCK_FILE) + 10)
 
-static void get_suspend_file(char buf[SUSPEND_FILE_BUFLEN], int domid)
+static void get_suspend_file(char buf[], int domid)
 {
-    snprintf(buf, sizeof(buf), SUSPEND_LOCK_FILE, domid);
+    snprintf(buf, SUSPEND_FILE_BUFLEN, SUSPEND_LOCK_FILE, domid);
 }
 
 static int lock_suspend_event(xc_interface *xch, int domid, int *lockfd)
Tested-by on v2 1/1
#6 lore raw
In reply to Ian Jackson

Hi Ian,

Quoted 13 lines · Ian Jackson

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

So the use of sizeof in xc_suspend.c:get_suspend_file was wrong. Instead, use the #define. Also get rid of the array size, as it is misleading.

Newer versions of gcc warn about the erroneous code:
xc_suspend.c:39:25: error: argument to 'sizeof' in 'snprintf' call
is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]

Quoted 4 lines

Great, I'm able to compile libxc with gcc 4.8:

Regards,

Signature · 1 line
Julien Grall
#5 lore raw
In reply to Ian Campbell

Ian Campbell writes ("Re: [PATCH v2] libxc: Fix buffer length for get_suspend_file"):

Quoted 4 lines · Ian Jackson
Quoted 3 lines

Declaring a formal parameter to have an array type doesn't result in the parameter actually having an array type. The type is "adjusted" to a pointer. (C99 6.9.1(7), 6.7.5.3.)

...

Quoted 1 line

Thanks, I've pushed it.

Quoted 3 lines

Although I do wonder if it would be less error prone to push the size up to a parameter to get_suspend_file where it is (presumably) near to the declaration of the array.

I don't think so: this one-line function only has two call sites. It doesn't seem sensible to me to lift any of its contents into the call sites in the name of clarity.

Ian.