Skip to contentSkip to conversation

libbpf: Ignore -Wformat-nonliteral warning

PATCH Applied Andrey Ignatov 3 participants 3 messages Apr 7, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Ignore -Wformat-nonliteral warning

vsprintf() in __base_pr() uses nonliteral format string and it breaks compilation for those who provide corresponding extra CFLAGS, e.g.:
https://github.com/libbpf/libbpf/issues/27

If libbpf is built with the flags from PR:

libbpf.c:68:26: error: format string is not a string literal [-Werror,-Wformat-nonliteral]

          return vfprintf(stderr, format, args);
                                  ^~~~~~

1 error generated.

Ignore this warning since the use case in libbpf.c is legit.

---
 tools/lib/bpf/libbpf.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index e1e4d35cf08e..1ebfe7943d53 100644
@@ -52,6 +52,11 @@
 #define BPF_FS_MAGIC		0xcafe4a11
 #endif
 
+/* vsprintf() in __base_pr() uses nonliteral format string. It may break
+ * compilation if user enables corresponding warning. Disable it explicitly.
+ */
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
 #define __printf(a, b)	__attribute__((format(printf, a, b)))
 
 static int __base_pr(enum libbpf_print_level level, const char *format,
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrey Ignatov

vsprintf() in __base_pr() uses nonliteral format string and it breaks compilation for those who provide corresponding extra CFLAGS, e.g.:
https://github.com/libbpf/libbpf/issues/27

If libbpf is built with the flags from PR:

libbpf.c:68:26: error: format string is not a string literal [-Werror,-Wformat-nonliteral]

Quoted 7 lines
           return vfprintf(stderr, format, args);
                                   ^~~~~~

1 error generated.

Ignore this warning since the use case in libbpf.c is legit.

Tested with clang compiler as well. It also resolved the same issue when using clang to build libbpf in libbpf repo.

Quoted 21 lines
---
  tools/lib/bpf/libbpf.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index e1e4d35cf08e..1ebfe7943d53 100644
@@ -52,6 +52,11 @@
  #define BPF_FS_MAGIC		0xcafe4a11
  #endif
 
+/* vsprintf() in __base_pr() uses nonliteral format string. It may break
+ * compilation if user enables corresponding warning. Disable it explicitly.
+ */
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
  #define __printf(a, b)	__attribute__((format(printf, a, b)))
 
  static int __base_pr(enum libbpf_print_level level, const char *format,
#2 lore raw
In reply to Andrey Ignatov
Quoted 8 lines · Andrey Ignatov

vsprintf() in __base_pr() uses nonliteral format string and it breaks compilation for those who provide corresponding extra CFLAGS, e.g.:
https://github.com/libbpf/libbpf/issues/27

If libbpf is built with the flags from PR:

libbpf.c:68:26: error: format string is not a string literal [-Werror,-Wformat-nonliteral]

Quoted 7 lines
          return vfprintf(stderr, format, args);
                                  ^~~~~~

1 error generated.

Ignore this warning since the use case in libbpf.c is legit.

Applied, Thanks