[Libvir] more printf-style function checking

Jim Meyering jim at meyering.net
Mon Mar 19 14:04:54 UTC 2007


I had the first hunk of the following patch laying around,
but when I updated/compiled today, it triggered a warning:

virsh.c:3162: warning: format '%d' expects type 'int', but argument 6 has type 's
ize_t'

I've fixed it by using %lu, which is more likely to be the same
width as size_t that %d, and by casting strlen's return val to
the new, expected type.

2007-03-19  Jim Meyering  <jim at meyering.net>

	Avoid printf-style format mismatch.
	* src/virsh.c (vshError): Use ATTRIBUTE_FORMAT.
	(_vshStrdup): Use %lu (not %d) for strlen, and cast the strlen
	return value to "unsigned long" to match the format.

Index: src/virsh.c
===================================================================
RCS file: /data/cvs/libvirt/src/virsh.c,v
retrieving revision 1.65
diff -u -p -r1.65 virsh.c
--- src/virsh.c	19 Mar 2007 09:46:13 -0000	1.65
+++ src/virsh.c	19 Mar 2007 14:03:10 -0000
@@ -179,8 +179,8 @@ typedef struct __vshControl {

 static vshCmdDef commands[];

-static void vshError(vshControl * ctl, int doexit, const char *format,
-                     ...);
+static void vshError(vshControl * ctl, int doexit, const char *format, ...)
+    ATTRIBUTE_FORMAT(printf, 3, 4);
 static int vshInit(vshControl * ctl);
 static int vshDeinit(vshControl * ctl);
 static void vshUsage(vshControl * ctl, const char *cmdname);
@@ -3158,8 +3158,8 @@ _vshStrdup(vshControl * ctl, const char

     if ((x = strdup(s)))
         return x;
-    vshError(ctl, TRUE, _("%s: %d: failed to allocate %d bytes"),
-             filename, line, strlen(s));
+    vshError(ctl, TRUE, _("%s: %d: failed to allocate %lu bytes"),
+             filename, line, (unsigned long)strlen(s));
     return NULL;
 }




More information about the libvir-list mailing list