xenstore: Check format printf
cover xenstore: Check format printf
From: Julien Grall <[email protected]>
This patch is a follow-up to Norbert's series [1].
The first patch will define PRINTF_ATTRIBUTE the same way everywhere. The second patch will check the format printf on a few more calls.
Both patches are candidate for 4.15. They only affects the build system and therefore would consider them low-risk.
Cheers,
[1] https://lore.kernel.org/xen-devel/[email protected]/
Julien Grall (2):
tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h tools/xenstore: Check the format printf for xprintf() and
barf{,_perror}()tools/xenstore/talloc.h | 15 ++------------- tools/xenstore/tdb.h | 6 ++---- tools/xenstore/utils.h | 10 +++++++--- 3 files changed, 11 insertions(+), 20 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/2 tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h
From: Julien Grall <[email protected]>
At the moment PRINTF_ATTRIBUTE() is defined in two places:
- tdb.h: Defined as a NOP
- talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format
Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.
Only implement PRINTF_ATTRIBUTE() once in utils.h and drop the conditional check for GCC < 3.0.
--- tools/xenstore/talloc.h | 15 ++------------- tools/xenstore/tdb.h | 6 ++---- tools/xenstore/utils.h | 2 ++ 3 files changed, 6 insertions(+), 17 deletions(-)
tools/xenstore/talloc.h +2 -13
@@ -26,6 +26,8 @@#include <sys/types.h> +#include "utils.h" +/* this is only needed for compatibility with the old talloc */ typedef void TALLOC_CTX ;
@@ -84,19 +86,6 @@ typedef void TALLOC_CTX;#define talloc_destroy (ctx) talloc_free(ctx)#endif -#ifndef PRINTF_ATTRIBUTE -#if (__GNUC__ >=3 ) -/** Use gcc attribute to check printf fns. a1 is the 1-based index of -* the parameter containing the format, and a2 the index of the first -* argument. Note that some gcc 2.x versions don't handle this -* properly **/ -#define PRINTF_ATTRIBUTE (a1, a2) __attribute__ ((format (__printf__, a1, a2))) -#else -#define PRINTF_ATTRIBUTE (a1, a2) -#endif -#endif - -/* The following definitions come from talloc.c */ void *_talloc (const void *context,size_t size);void talloc_set_destructor (const void *ptr,int (*destructor)(void *));
tools/xenstore/tdb.h +2 -4
@@ -1,6 +1,8 @@#ifndef __TDB_H__#define __TDB_H__ +#include "utils.h" +/* Unix SMB/CIFS implementation.
@@ -84,10 +86,6 @@ struct tdb_traverse_lock {uint32_t hash; }; -#ifndef PRINTF_ATTRIBUTE -#define PRINTF_ATTRIBUTE (a,b) -#endif -/* this is the context structure that is returned from a db open */ typedef struct tdb_context { char *name; /* the name of the database */
tools/xenstore/utils.h +2 -0
@@ -27,6 +27,8 @@ static inline bool strends(const char *a, const char *b)*/ const char *dump_state_align (FILE *fp); +#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2))) +void barf (const char *fmt, ...) __attribute__((noreturn));void barf_perror (const char *fmt, ...) __attribute__((noreturn));
Signature · 1 line
2.17.1
Re: [PATCH for-4.15 1/2] tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h
Quoted 7 lines · Julien Grall
From: Julien Grall <[email protected]>
At the moment
PRINTF_ATTRIBUTE()is defined in two places:
-tdb.h: Defined as a NOP
-talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format
Quoted 7 lines
Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.
Only implement
PRINTF_ATTRIBUTE()once inutils.hand drop the conditional check for GCC < 3.0.Signed-off-by Julien Grall <[email protected]>
Juergen
PATCH 1/2 tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h
In reply to Julien GrallJulien Grall writes ("[PATCH for-4.15 1/2] tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h"):
Quoted 7 lines
From: Julien Grall <[email protected]>
At the moment
PRINTF_ATTRIBUTE()is defined in two places:
-tdb.h: Defined as a NOP
-talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format
Quoted 5 lines
Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.
Only implement
PRINTF_ATTRIBUTE()once inutils.hand drop the conditional check for GCC < 3.0.
PATCH 2/2 tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
In reply to Julien GrallFrom: Julien Grall <[email protected]>
Allow GCC to analyze the format printf for xprintf() and barf{,_perror}().
Take the opportunity to define __noreturn to make the prototype for barf{,_perror})() easier to read.
--- tools/xenstore/utils.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstore/utils.h +5 -3
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2))) -void barf (const char *fmt, ...) __attribute__((noreturn)); -void barf_perror (const char *fmt, ...) __attribute__((noreturn)); +#define __noreturn __attribute__((noreturn)) -extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2); #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
Signature · 1 line
2.17.1
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Quoted 9 lines · Julien Grall
From: Julien Grall <[email protected]>
Allow GCC to analyze the format printf for
xprintf()and barf{,_perror}().Take the opportunity to define
__noreturnto make the prototype for barf{,_perror})() easier to read.Signed-off-by Julien Grall <[email protected]>
But I would prefer, if ...
Quoted 11 lines
--- tools/xenstore/utils.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/xenstore/utils.h +0
-0@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2)))
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 10 lines
tools/xenstore/utils.h +5
-3-void barf (const char *fmt, ...) __attribute__((noreturn)); -void barf_perror (const char *fmt, ...) __attribute__((noreturn)); +#define __noreturn __attribute__((noreturn)) -extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
... the extern here would be dropped.
Juergen
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Jürgen Groß writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):
Quoted 8 lines · Julien Grall
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 5 lines
tools/xenstore/utils.h +4
-1-extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);... the extern here would be dropped.
With my RM hat on I don't have an opinion on that and my R-A can stand.
With my maintainer hat on I agree with Jürgen's style opinion - it's nicer without the "extern", but I'm also happy with the patch as is.
Ian.
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Hi Ian,
Quoted 10 lines · Ian Jackson
Jürgen Groß writes ("Re: [PATCH for-4.15 2/2]
tools/xenstore: Check the format printf forxprintf()and barf{,_perror}()"):Quoted 8 lines · Julien Grall
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 5 lines
tools/xenstore/utils.h +4
-1-extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);... the extern here would be dropped.
Quoted 5 lines
With my RM hat on I don't have an opinion on that and my R-A can stand.
With my maintainer hat on I agree with Jürgen's style opinion - it's nicer without the "extern", but I'm also happy with the patch as is.
I agree with Juergen's style opinion. I will do the modification on commit. I will also update the last sentence of the commit to:
"
Take the opportunity to:
* remove extern from the function prototype for consistency
* define __noreturn to make the prototype for
barf{,_perror})() easier to read.
"
Cheers,
Quoted 2 lines
Ian.
Signature · 1 line
Julien Grall
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
In reply to Jürgen GroßQuoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall
From: Julien Grall <[email protected]>
tools/xenstore/utils.h +1
-2@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2))) -void barf (const char *fmt, ...) __attribute__((noreturn)); -void barf_perror (const char *fmt, ...) __attribute__((noreturn)); +#define __noreturn __attribute__((noreturn))
Quoted 7 lines
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 5 lines
tools/xenstore/utils.h +4
-1-extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);... the extern here would be dropped.
But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.
Jan
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Hi Jan,
Quoted 21 lines · Jan Beulich
Quoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall
From: Julien Grall <[email protected]>
tools/xenstore/utils.h +1
-2@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2))) -void barf (const char *fmt, ...) __attribute__((noreturn)); -void barf_perror (const char *fmt, ...) __attribute__((noreturn)); +#define __noreturn __attribute__((noreturn))Quoted 7 lines
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 5 lines
tools/xenstore/utils.h +4
-1-extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);... the extern here would be dropped.
Quoted 2 lines
But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.
Urgh, you are right. Actually, the extern was added recently by Anthony:
dacdbf7088d6a3705a9831e73991c2b14c519a65 ("tools/xenstore: mark variable in header as extern")
I completely forgot it despite I needed to backport the patch to our downstream Xen.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):
Quoted 7 lines
Urgh, you are right. Actually, the extern was added recently by Anthony:
dacdbf7088d6a3705a9831e73991c2b14c519a65("tools/xenstore: mark variable in header as extern")I completely forgot it despite I needed to backport the patch to our downstream Xen.
How horrible.
Maybe we could add a comment to the code, next to the declaration, about this crazy situation.
Ian.
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Hi Ian,
Quoted 6 lines · Ian Jackson
Julien Grall writes ("Re: [PATCH for-4.15 2/2]
tools/xenstore: Check the format printf forxprintf()and barf{,_perror}()"):Quoted 5 lines
Urgh, you are right. Actually, the extern was added recently by Anthony:
dacdbf7088d6a3705a9831e73991c2b14c519a65("tools/xenstore: mark variable in header as extern")
Quoted 7 lines
Quoted 2 lines
I completely forgot it despite I needed to backport the patch to our downstream Xen.
How horrible.
Maybe we could add a comment to the code, next to the declaration, about this crazy situation.
Would the following comment work for you?
/* Function pointer as xprintf() can be configured at runtime. */
I can fold it in my patch while committing.
Cheers,
Quoted 3 lines
Ian.
Signature · 1 line
Julien Grall
Ian Jackson
Reviewed-byRe: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):
Quoted 5 lines
Would the following comment work for you?
/* Function pointer as
xprintf()can be configured at runtime. */I can fold it in my patch while committing.
Sure, thanks. FTAOD
to that comment addition.
Ian.
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
Hi Ian,
Quoted 5 lines · Ian Jackson
Julien Grall writes ("Re: [PATCH for-4.15 2/2]
tools/xenstore: Check the format printf forxprintf()and barf{,_perror}()"):Quoted 4 lines
Would the following comment work for you?
/* Function pointer as
xprintf()can be configured at runtime. */
Quoted 8 lines
Quoted 1 line
I can fold it in my patch while committing.
Sure, thanks. FTAOD
Reviewed-by Ian Jackson <[email protected]>Release-Acked-by Ian Jackson <[email protected]>to that comment addition.
Thanks! I have committed the two patches.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
In reply to Jan BeulichQuoted 13 lines · Jan Beulich
Quoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall
From: Julien Grall <[email protected]>
tools/xenstore/utils.h +1
-2@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);#define PRINTF_ATTRIBUTE (a1, a2) __attribute__((format (printf, a1, a2))) -void barf (const char *fmt, ...) __attribute__((noreturn)); -void barf_perror (const char *fmt, ...) __attribute__((noreturn)); +#define __noreturn __attribute__((noreturn))
Quoted 8 lines
Quoted 6 lines
tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37
Quoted 5 lines
tools/xenstore/utils.h +4
-1-extern void (*xprintf)(const char *fmt, ...); +void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); +void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2); + +extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);... the extern here would be dropped.
But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.
Correct, and some of the containers in Gitlab CI really ought to notice because GCC 9(? IIRC) defaulted to -fno-common.
~Andrew
PATCH 2/2 tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()
In reply to Julien GrallJulien Grall writes ("[PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):
Quoted 7 lines
From: Julien Grall <[email protected]>
Allow GCC to analyze the format printf for
xprintf()and barf{,_perror}().Take the opportunity to define
__noreturnto make the prototype for barf{,_perror})() easier to read.