Skip to content

Commit 8918dc4

Browse files
qmonnetAlexei Starovoitov
authored andcommitted
tools: bpftool: move "__printf()" attributes to header file
Some functions in bpftool have a "__printf()" format attributes to tell the compiler they should expect printf()-like arguments. But because these attributes are not used for the function prototypes in the header files, the compiler does not run the checks everywhere the functions are used, and some mistakes on format string and corresponding arguments slipped in over time. Let's move the __printf() attributes to the correct places. Note: We add guards around the definition of GCC_VERSION in tools/include/linux/compiler-gcc.h to prevent a conflict in jit_disasm.c on GCC_VERSION from headers pulled via libbfd. Fixes: c101189 ("tools: bpftool: fix -Wmissing declaration warnings") Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b0ead6d commit 8918dc4

5 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎tools/bpf/bpftool/common.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#define BPF_FS_MAGIC 0xcafe4a11
3030
#endif
3131

32-
void __printf(1, 2) p_err(const char *fmt, ...)
32+
void p_err(const char *fmt, ...)
3333
{
3434
va_list ap;
3535

@@ -47,7 +47,7 @@ void __printf(1, 2) p_err(const char *fmt, ...)
4747
va_end(ap);
4848
}
4949

50-
void __printf(1, 2) p_info(const char *fmt, ...)
50+
void p_info(const char *fmt, ...)
5151
{
5252
va_list ap;
5353

‎tools/bpf/bpftool/json_writer.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <malloc.h>
1616
#include <inttypes.h>
1717
#include <stdint.h>
18-
#include <linux/compiler.h>
1918

2019
#include "json_writer.h"
2120

@@ -153,16 +152,15 @@ void jsonw_name(json_writer_t *self, const char *name)
153152
putc(' ', self->out);
154153
}
155154

156-
void __printf(2, 0)
157-
jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
155+
void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
158156
{
159157
jsonw_eor(self);
160158
putc('"', self->out);
161159
vfprintf(self->out, fmt, ap);
162160
putc('"', self->out);
163161
}
164162

165-
void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...)
163+
void jsonw_printf(json_writer_t *self, const char *fmt, ...)
166164
{
167165
va_list ap;
168166

‎tools/bpf/bpftool/json_writer.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdbool.h>
1515
#include <stdint.h>
1616
#include <stdarg.h>
17+
#include <linux/compiler.h>
1718

1819
/* Opaque class structure */
1920
typedef struct json_writer json_writer_t;
@@ -30,8 +31,9 @@ void jsonw_pretty(json_writer_t *self, bool on);
3031
void jsonw_name(json_writer_t *self, const char *name);
3132

3233
/* Add value */
33-
void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap);
34-
void jsonw_printf(json_writer_t *self, const char *fmt, ...);
34+
void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
35+
va_list ap);
36+
void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
3537
void jsonw_string(json_writer_t *self, const char *value);
3638
void jsonw_bool(json_writer_t *self, bool value);
3739
void jsonw_float(json_writer_t *self, double number);

‎tools/bpf/bpftool/main.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ extern int bpf_flags;
9898
extern struct pinned_obj_table prog_table;
9999
extern struct pinned_obj_table map_table;
100100

101-
void p_err(const char *fmt, ...);
102-
void p_info(const char *fmt, ...);
101+
void __printf(1, 2) p_err(const char *fmt, ...);
102+
void __printf(1, 2) p_info(const char *fmt, ...);
103103

104104
bool is_prefix(const char *pfx, const char *str);
105105
int detect_common_prefix(const char *arg, ...);

‎tools/include/linux/compiler-gcc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
/*
77
* Common definitions for all gcc versions go here.
88
*/
9+
#ifndef GCC_VERSION
910
#define GCC_VERSION (__GNUC__ * 10000 \
1011
+ __GNUC_MINOR__ * 100 \
1112
+ __GNUC_PATCHLEVEL__)
13+
#endif
1214

1315
#if GCC_VERSION >= 70000 && !defined(__CHECKER__)
1416
# define __fallthrough __attribute__ ((fallthrough))

0 commit comments

Comments
 (0)