]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Optionally call printf on LOG_MESSAGE() arguments
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 10 May 2022 12:11:55 +0000 (14:11 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 11 May 2022 14:47:40 +0000 (16:47 +0200)
With an intentional mistake:

../src/login/logind-dbus.c: In function ‘bus_manager_log_shutdown’:
../src/login/logind-dbus.c:1542:39: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=]
 1542 |                           LOG_MESSAGE("%s %s", message),
      |                                       ^~~~~~~

meson.build
meson_options.txt
src/basic/log.h
src/core/unit.h

index 051eea9aac07cd54a0340a541bd1d14af32ffb67..46f062d5303acd930edb4f394444ea3516467afe 100644 (file)
@@ -33,6 +33,7 @@ conf.set_quoted('RELATIVE_SOURCE_PATH', relative_source_path)
 
 conf.set10('BUILD_MODE_DEVELOPER', get_option('mode') == 'developer',
            description : 'tailor build to development or release builds')
+conf.set10('LOG_MESSAGE_VERIFICATION', get_option('log-message-verification'))
 
 want_ossfuzz = get_option('oss-fuzz')
 want_libfuzzer = get_option('llvm-fuzz')
index 2a030ac28ec05c3f4f8c4a7ed18376385dff8f93..ff59120f77d0ae6420c3f0e4497314afef0ebadf 100644 (file)
@@ -470,6 +470,8 @@ option('fuzz-tests', type : 'boolean', value : 'false',
        description : 'run the fuzzer regression tests by default (with sanitizers)')
 option('install-tests', type : 'boolean', value : 'false',
        description : 'install test executables')
+option('log-message-verification', type : 'boolean', value : 'false',
+       description : 'do fake printf() calls to verify format strings')
 
 option('ok-color', type : 'combo',
        choices : ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan',
index 6f0d838de37587947466d28763293f99e5e69c76..b03ca3ef21c736eacbf4f7617a87e85170baf75a 100644 (file)
@@ -300,7 +300,13 @@ bool log_on_console(void) _pure_;
 
 /* Helper to wrap the main message in structured logging. The macro doesn't do much,
  * except to provide visual grouping of the format string and its arguments. */
-#define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
+#if LOG_MESSAGE_VERIFICATION
+/* Do a fake formatting of the message string to let the scanner verify the arguments
+ * against the format message. */
+#  define LOG_MESSAGE(fmt, ...) "MESSAGE=%.0d" fmt, printf(fmt, ##__VA_ARGS__), ##__VA_ARGS__
+#else
+#  define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
+#endif
 
 void log_received_signal(int level, const struct signalfd_siginfo *si);
 
index 733eeecd7f0d2f85783ecbc191a7a895b63f55de..20c511c108c49e424cb44d406bb1713b7be1d4c0 100644 (file)
@@ -1046,7 +1046,8 @@ Condition *unit_find_failed_condition(Unit *u);
 
 #define log_unit_struct_iovec(unit, level, iovec, n_iovec) log_unit_struct_iovec_errno(unit, level, 0, iovec, n_iovec)
 
-#define LOG_UNIT_MESSAGE(unit, fmt, ...) "MESSAGE=%s: " fmt, (unit)->id, ##__VA_ARGS__
+/* Like LOG_MESSAGE(), but with the unit name prefixed. */
+#define LOG_UNIT_MESSAGE(unit, fmt, ...) LOG_MESSAGE("%s: " fmt, (unit)->id, ##__VA_ARGS__)
 #define LOG_UNIT_ID(unit) (unit)->manager->unit_log_format_string, (unit)->id
 #define LOG_UNIT_INVOCATION_ID(unit) (unit)->manager->invocation_log_format_string, (unit)->invocation_id_string