Skip to content

Commit b237266

Browse files
committed
fortify-headers: fix -Werror=format-nonliteral in fortify/stdio.h
Some applications might activate -Werror=format-nonliteral when building their application. This breaks fortify headers build. Tell GCC to ignore such warnings for this code. This fixes the libubox and ucode build: ``` /include/fortify/stdio.h: In function 'snprintf': /include/fortify/stdio.h:101:9: error: format not a string literal, argument types not checked [-Werror=format-nonliteral] 101 | return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack()); | ^~~~~~ /include/fortify/stdio.h: In function 'sprintf': /include/fortify/stdio.h:110:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral] 110 | __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack()); | ^~~ /include/fortify/stdio.h:114:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral] 114 | __r = __orig_sprintf(__s, __f, __builtin_va_arg_pack()); | ^~~ cc1: all warnings being treated as errors ninja: build stopped: subcommand failed. ``` Link: openwrt/openwrt#22042 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
1 parent 0f4e4a4 commit b237266

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
From 793d85b802e8b1a179134056894249c5a0270c72 Mon Sep 17 00:00:00 2001
2+
From: Hauke Mehrtens <hauke@hauke-m.de>
3+
Date: Sun, 15 Feb 2026 19:03:50 +0100
4+
Subject: stdio.h: ignore -Wformat-nonliteral in snprintf and sprintf
5+
6+
Some applications might activate -Werror=format-nonliteral when building
7+
their application. This breaks fortify headers build. Tell GCC to ignore
8+
such warnings for this code.
9+
10+
This fixes the libubox and ucode build:
11+
```
12+
/include/fortify/stdio.h: In function 'snprintf':
13+
/include/fortify/stdio.h:101:9: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
14+
101 | return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack());
15+
| ^~~~~~
16+
/include/fortify/stdio.h: In function 'sprintf':
17+
/include/fortify/stdio.h:110:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
18+
110 | __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack());
19+
| ^~~
20+
/include/fortify/stdio.h:114:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
21+
114 | __r = __orig_sprintf(__s, __f, __builtin_va_arg_pack());
22+
| ^~~
23+
cc1: all warnings being treated as errors
24+
ninja: build stopped: subcommand failed.
25+
```
26+
27+
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
28+
---
29+
include/stdio.h | 6 ++++++
30+
1 file changed, 6 insertions(+)
31+
32+
--- a/include/stdio.h
33+
+++ b/include/stdio.h
34+
@@ -98,7 +98,10 @@ _FORTIFY_FN(snprintf) int snprintf(char
35+
36+
if (__n > __b)
37+
__builtin_trap();
38+
+#pragma GCC diagnostic push
39+
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
40+
return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack());
41+
+#pragma GCC diagnostic pop
42+
}
43+
44+
_FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...)
45+
@@ -106,6 +109,8 @@ _FORTIFY_FN(sprintf) int sprintf(char *_
46+
size_t __b = __builtin_object_size(__s, 0);
47+
int __r;
48+
49+
+#pragma GCC diagnostic push
50+
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
51+
if (__b != (size_t)-1) {
52+
__r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack());
53+
if (__r != -1 && (size_t)__r >= __b)
54+
@@ -114,6 +119,7 @@ _FORTIFY_FN(sprintf) int sprintf(char *_
55+
__r = __orig_sprintf(__s, __f, __builtin_va_arg_pack());
56+
}
57+
return __r;
58+
+#pragma GCC diagnostic pop
59+
}
60+
61+
#ifdef __cplusplus

0 commit comments

Comments
 (0)