From c6c8130db4319613a91dd07bbb845f6c33c5f79f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Christian=20G=C3=B6ttsche?= Date: Fri, 5 Aug 2022 17:40:42 +0200 Subject: [PATCH] Use function format attribute where applicable Allow the compiler to verify the format string against the supplied arguments. chage.c:239:51: warning: format not a string literal, format string not checked [-Wformat-nonliteral] 239 | (void) strftime (buf, sizeof buf, format, tp); | ^~~~~~ --- lib/defines.h | 2 ++ lib/selinux.c | 2 +- lib/semanage.c | 1 + libmisc/copydir.c | 3 ++- src/chage.c | 10 +--------- 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/defines.h b/lib/defines.h index 55b42bee5..4a2b90c9e 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -320,8 +320,10 @@ extern char *strerror (); /* To be used for verified unused parameters */ #if defined(__GNUC__) && !defined(__STRICT_ANSI__) # define unused __attribute__((unused)) +# define format_attr(type, index, check) __attribute__((format (type, index, check))) #else # define unused +# define format_attr(type, index, check) #endif /* ! Arguments evaluated twice ! */ diff --git a/lib/selinux.c b/lib/selinux.c index f97b1fe5b..ad639bd33 100644 --- a/lib/selinux.c +++ b/lib/selinux.c @@ -109,7 +109,7 @@ int reset_selinux_file_context (void) /* * Log callback for libselinux internal error reporting. */ -__attribute__((__format__ (printf, 2, 3))) +format_attr(printf, 2, 3) static int selinux_log_cb (int type, const char *fmt, ...) { va_list ap; char *buf; diff --git a/lib/semanage.c b/lib/semanage.c index 12401608a..54f996238 100644 --- a/lib/semanage.c +++ b/lib/semanage.c @@ -27,6 +27,7 @@ #endif +format_attr(printf, 3, 4) static void semanage_error_callback (unused void *varg, semanage_handle_t *handle, const char *fmt, ...) diff --git a/libmisc/copydir.c b/libmisc/copydir.c index a5f1b3dad..2929151db 100644 --- a/libmisc/copydir.c +++ b/libmisc/copydir.c @@ -91,7 +91,8 @@ static int fchown_if_needed (int fdst, const struct stat *statp, /* * error_acl - format the error messages for the ACL and EQ libraries. */ -static void error_acl (struct error_context *ctx, const char *fmt, ...) +format_attr(printf, 2, 3) +static void error_acl (unused struct error_context *ctx, const char *fmt, ...) { va_list ap; FILE *shadow_logfd = log_get_logfd(); diff --git a/src/chage.c b/src/chage.c index ced3a3e8a..8cf677942 100644 --- a/src/chage.c +++ b/src/chage.c @@ -223,20 +223,12 @@ static void print_date (time_t date) { struct tm *tp; char buf[80]; - char format[80]; - - if (iflg) { - (void) snprintf (format, 80, "%%Y-%%m-%%d"); - } - else { - (void) snprintf (format, 80, "%%b %%d, %%Y"); - } tp = gmtime (&date); if (NULL == tp) { (void) printf ("time_t: %lu\n", (unsigned long)date); } else { - (void) strftime (buf, sizeof buf, format, tp); + (void) strftime (buf, sizeof buf, iflg ? "%%Y-%%m-%%d" : "%%b %%d, %%Y", tp); (void) puts (buf); } } -- 2.47.2