[systemd-devel] [PATCH] Add more _printf_'s for format-nonliterals

Thomas H.P. Andersen phomes at gmail.com
Sat Dec 14 04:32:35 PST 2013


From: Thomas Hindoe Paaboel Andersen <phomes at gmail.com>

Clang is a bit more strict wrt format-nonliterals:
http://clang.llvm.org/docs/LanguageExtensions.html#format-string-checking

Adding these extra printf attributes also makes gcc able to find more
problems. E.g. this patch uncovers a format issue in udev-builtin-path_id.c

Some parts looked intetional about breaking the format-nonliteral check.
I added some supression for warnings there.
---
 src/core/job.c                         | 3 +++
 src/core/unit.c                        | 3 +++
 src/journal/journal-gatewayd.c         | 1 +
 src/journal/microhttpd-util.c          | 4 ++++
 src/libsystemd-bus/bus-error.c         | 2 ++
 src/libudev/libudev.c                  | 1 +
 src/test/test-libudev.c                | 1 +
 src/udev/accelerometer/accelerometer.c | 1 +
 src/udev/ata_id/ata_id.c               | 1 +
 src/udev/cdrom_id/cdrom_id.c           | 1 +
 src/udev/scsi_id/scsi_id.c             | 1 +
 src/udev/udev-builtin-kmod.c           | 1 +
 src/udev/udev-builtin-path_id.c        | 1 +
 13 files changed, 21 insertions(+)

diff --git a/src/core/job.c b/src/core/job.c
index ce97263..491c73d 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -610,6 +610,8 @@ _pure_ static const char *job_get_status_message_format_try_harder(Unit *u, JobT
         return NULL;
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
         const char *format;
 
@@ -676,6 +678,7 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) {
                         unit_status_printf(u, ANSI_HIGHLIGHT_ON " INFO " ANSI_HIGHLIGHT_OFF, "%s is not active.");
         }
 }
+#pragma GCC diagnostic pop
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
diff --git a/src/core/unit.c b/src/core/unit.c
index 6458c7f..160a346 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1125,6 +1125,8 @@ _pure_ static const char *unit_get_status_message_format_try_harder(Unit *u, Job
         return NULL;
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 static void unit_status_print_starting_stopping(Unit *u, JobType t) {
         const char *format;
 
@@ -1139,6 +1141,7 @@ static void unit_status_print_starting_stopping(Unit *u, JobType t) {
 
         unit_status_printf(u, "", format);
 }
+#pragma GCC diagnostic pop
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index 384137b..5c35d8e 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -131,6 +131,7 @@ static int respond_oom_internal(struct MHD_Connection *connection) {
 
 #define respond_oom(connection) log_oom(), respond_oom_internal(connection)
 
+_printf_(3,4)
 static int respond_error(
                 struct MHD_Connection *connection,
                 unsigned code,
diff --git a/src/journal/microhttpd-util.c b/src/journal/microhttpd-util.c
index 382087c..5675af4 100644
--- a/src/journal/microhttpd-util.c
+++ b/src/journal/microhttpd-util.c
@@ -27,6 +27,9 @@
 #include "macro.h"
 #include "util.h"
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+_printf_(2,0)
 void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
         _cleanup_free_ char *f;
         if (asprintf(&f, "microhttpd: %s", fmt) <= 0) {
@@ -35,3 +38,4 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
         }
         log_metav(LOG_INFO, NULL, 0, NULL, f, ap);
 }
+#pragma GCC diagnostic pop
diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c
index 25eaf0e..2d1e80d 100644
--- a/src/libsystemd-bus/bus-error.c
+++ b/src/libsystemd-bus/bus-error.c
@@ -250,6 +250,7 @@ finish:
         return -bus_error_name_to_errno(name);
 }
 
+_printf_(3,0)
 int bus_error_setfv(sd_bus_error *e, const char *name, const char *format, va_list ap) {
 
         if (!name)
@@ -463,6 +464,7 @@ _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) {
         return -error;
 }
 
+_printf_(3,0)
 int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_list ap) {
         int r;
 
diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c
index bc73a5a..e1ba82f 100644
--- a/src/libudev/libudev.c
+++ b/src/libudev/libudev.c
@@ -65,6 +65,7 @@ void udev_log(struct udev *udev,
         va_end(args);
 }
 
+_printf_(6,0)
 static void log_stderr(struct udev *udev,
                        int priority, const char *file, int line, const char *fn,
                        const char *format, va_list args)
diff --git a/src/test/test-libudev.c b/src/test/test-libudev.c
index 7ed4e14..a8c1ff0 100644
--- a/src/test/test-libudev.c
+++ b/src/test/test-libudev.c
@@ -34,6 +34,7 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
+_printf_(6,0)
 static void log_fn(struct udev *udev,
                    int priority, const char *file, int line, const char *fn,
                    const char *format, va_list args) {
diff --git a/src/udev/accelerometer/accelerometer.c b/src/udev/accelerometer/accelerometer.c
index e611b48..86966fb 100644
--- a/src/udev/accelerometer/accelerometer.c
+++ b/src/udev/accelerometer/accelerometer.c
@@ -71,6 +71,7 @@
 
 static int debug = 0;
 
+_printf_(6,0)
 static void log_fn(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
diff --git a/src/udev/ata_id/ata_id.c b/src/udev/ata_id/ata_id.c
index d375d43..28e9acb 100644
--- a/src/udev/ata_id/ata_id.c
+++ b/src/udev/ata_id/ata_id.c
@@ -405,6 +405,7 @@ out:
         return ret;
 }
 
+_printf_(6,0)
 static void log_fn(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c
index 1ad0459..09d2d69 100644
--- a/src/udev/cdrom_id/cdrom_id.c
+++ b/src/udev/cdrom_id/cdrom_id.c
@@ -39,6 +39,7 @@
 
 static bool debug;
 
+_printf_(6,0)
 static void log_fn(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c
index c2cc2ea..ae36b9e 100644
--- a/src/udev/scsi_id/scsi_id.c
+++ b/src/udev/scsi_id/scsi_id.c
@@ -67,6 +67,7 @@ static char model_enc_str[256];
 static char revision_str[16];
 static char type_str[16];
 
+_printf_(6,0)
 static void log_fn(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
diff --git a/src/udev/udev-builtin-kmod.c b/src/udev/udev-builtin-kmod.c
index fc28121..63dbf30 100644
--- a/src/udev/udev-builtin-kmod.c
+++ b/src/udev/udev-builtin-kmod.c
@@ -64,6 +64,7 @@ static int load_module(struct udev *udev, const char *alias)
         return err;
 }
 
+_printf_(6,0)
 static void udev_kmod_log(void *data, int priority, const char *file, int line,
                           const char *fn, const char *format, va_list args)
 {
diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c
index 763f3c3..7476330 100644
--- a/src/udev/udev-builtin-path_id.c
+++ b/src/udev/udev-builtin-path_id.c
@@ -32,6 +32,7 @@
 
 #include "udev.h"
 
+_printf_(2,3)
 static int path_prepend(char **path, const char *fmt, ...)
 {
         va_list va;
-- 
1.8.4.2



More information about the systemd-devel mailing list