[OpenWrt-Devel] [PATCHv2] uci: Fix Wformat-nonliteral warning

Rosen Penev rosenp at gmail.com
Sat Nov 30 16:25:25 EST 2019


A const char * variable is being passed as a format string. Unfortunately,
this is not correct.

A constant expression needs to be passed so that GCC can determine the
types of the format properly.

Also fixed a different warning that needs a printf attribute.

Fixes:

error: format not a string literal, argument types not checked
[-Werror=format-nonliteral]
  176 |    error_info);
      |    ^~~~~~~~~~
error: format not a string literal, argument types not checked
[-Werror=format-nonliteral]
  185 |    error_info);

cli.c:196:19: error: format string is not a string literal
[-Werror=format-nonliteral]
           vfprintf(stderr, fmt, ap);

Signed-off-by: Rosen Penev <rosenp at gmail.com>
---
 v2: Fixed extra warning from clang 10.
 cli.c    |  1 +
 libuci.c | 10 +++++-----
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/cli.c b/cli.c
index 8970f4f..6ba97ea 100644
--- a/cli.c
+++ b/cli.c
@@ -185,6 +185,7 @@ static void cli_perror(void)
 	uci_perror(ctx, appname);
 }
 
+__attribute__((format(printf, 1, 2)))
 static void cli_error(const char *fmt, ...)
 {
 	va_list ap;
diff --git a/libuci.c b/libuci.c
index a9e70e8..96da49b 100644
--- a/libuci.c
+++ b/libuci.c
@@ -142,11 +142,11 @@ uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
 {
 	static char error_info[128];
 	int err;
-	const char *format =
-		"%s%s" /* prefix */
-		"%s%s" /* function */
-		"%s" /* error */
-		"%s"; /* details */
+#define	format \
+		"%s%s" /* prefix */ \
+		"%s%s" /* function */ \
+		"%s" /* error */ \
+		"%s" /* details */
 
 	error_info[0] = 0;
 
-- 
2.23.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list