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

Rosen Penev rosenp at gmail.com
Sat Dec 7 13:50:41 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>
---
 v4: Use a proper named define
 v3: Moved define to top of file.
 v2: Fixed extra warning from clang 10.
 cli.c    |  1 +
 libuci.c | 15 ++++++++-------
 2 files changed, 9 insertions(+), 7 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..2e46456 100644
--- a/libuci.c
+++ b/libuci.c
@@ -40,6 +40,12 @@ static const char *uci_errstr[] = {
 #include "uci_internal.h"
 #include "list.c"
 
+#define UCI_ERR_FORMAT \
+	"%s%s" /* prefix */ \
+	"%s%s" /* function */ \
+	"%s" /* error */ \
+	"%s" /* details */
+
 __private const char *uci_confdir = UCI_CONFDIR;
 __private const char *uci_savedir = UCI_SAVEDIR;
 
@@ -142,11 +148,6 @@ 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 */
 
 	error_info[0] = 0;
 
@@ -169,7 +170,7 @@ uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
 		break;
 	}
 	if (dest) {
-		err = asprintf(dest, format,
+		err = asprintf(dest, UCI_ERR_FORMAT,
 			(prefix ? prefix : ""), (prefix ? ": " : ""),
 			(ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
 			uci_errstr[err],
@@ -178,7 +179,7 @@ uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix)
 			*dest = NULL;
 	} else {
 		strcat(error_info, "\n");
-		fprintf(stderr, format,
+		fprintf(stderr, UCI_ERR_FORMAT,
 			(prefix ? prefix : ""), (prefix ? ": " : ""),
 			(ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""),
 			uci_errstr[err],
-- 
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