tree-wide: check more log message format in log_struct() and friends

This introduce LOG_ITEM() macro that checks arbitrary formats in
log_struct().
Then, drop _printf_ attribute from log_struct_internal(), as it does not
help so much, and compiler checked only the first format string.

Hopefully, this silences false-positive warnings by Coverity.
This commit is contained in:
Yu Watanabe
2025-03-19 01:56:48 +09:00
parent a0272b8356
commit 3cf6a3a3d4
53 changed files with 251 additions and 254 deletions
+11 -7
View File
@@ -1108,7 +1108,9 @@ int log_struct_internal(
iovec[n++] = IOVEC_MAKE_STRING(header);
va_start(ap, format);
DISABLE_WARNING_FORMAT_NONLITERAL;
r = log_format_iovec(iovec, iovec_len, &n, true, error, format, ap);
REENABLE_WARNING;
m = n;
if (r < 0)
fallback = true;
@@ -1145,7 +1147,9 @@ int log_struct_internal(
errno = ERRNO_VALUE(error);
va_copy(aq, ap);
DISABLE_WARNING_FORMAT_NONLITERAL;
(void) vsnprintf(buf, sizeof buf, format, aq);
REENABLE_WARNING;
va_end(aq);
if (startswith(buf, "MESSAGE=")) {
@@ -1649,9 +1653,9 @@ int log_syntax_internal(
level,
error,
file, line, func,
"MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
"CONFIG_FILE=%s", config_file,
"CONFIG_LINE=%u", config_line,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_ITEM("CONFIG_FILE=%s", config_file),
LOG_ITEM("CONFIG_LINE=%u", config_line),
LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
unit_fmt, unit,
NULL);
@@ -1660,8 +1664,8 @@ int log_syntax_internal(
level,
error,
file, line, func,
"MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
"CONFIG_FILE=%s", config_file,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_ITEM("CONFIG_FILE=%s", config_file),
LOG_MESSAGE("%s: %s", config_file, buffer),
unit_fmt, unit,
NULL);
@@ -1670,7 +1674,7 @@ int log_syntax_internal(
level,
error,
file, line, func,
"MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_MESSAGE("%s: %s", unit, buffer),
unit_fmt, unit,
NULL);
@@ -1679,7 +1683,7 @@ int log_syntax_internal(
level,
error,
file, line, func,
"MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_MESSAGE("%s", buffer),
NULL);
}
+6 -2
View File
@@ -153,7 +153,7 @@ int log_struct_internal(
const char *file,
int line,
const char *func,
const char *format, ...) _printf_(6,0) _sentinel_;
const char *format, ...) _sentinel_;
int log_oom_internal(
int level,
@@ -314,11 +314,15 @@ bool log_on_console(void) _pure_;
/* Do a fake formatting of the message string to let the scanner verify the arguments against the format
* message. The variable will never be set to true, but we don't tell the compiler that :) */
extern bool _log_message_dummy;
# define LOG_MESSAGE(fmt, ...) "MESSAGE=%.0d" fmt, (_log_message_dummy && printf(fmt, ##__VA_ARGS__)), ##__VA_ARGS__
# define LOG_ITEM(fmt, ...) "%.0d" fmt, (_log_message_dummy && printf(fmt, ##__VA_ARGS__)), ##__VA_ARGS__
# define LOG_MESSAGE(fmt, ...) LOG_ITEM("MESSAGE=" fmt, ##__VA_ARGS__)
#else
# define LOG_ITEM(fmt, ...) fmt, ##__VA_ARGS__
# define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
#endif
#define LOG_MESSAGE_ID(id) LOG_ITEM("MESSAGE_ID=" id)
void log_received_signal(int level, const struct signalfd_siginfo *si);
/* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
+2 -2
View File
@@ -1381,8 +1381,8 @@ void valgrind_summary_hack(void) {
if (pid < 0)
log_struct_errno(
LOG_EMERG, errno,
"MESSAGE_ID=" SD_MESSAGE_VALGRIND_HELPER_FORK_STR,
LOG_MESSAGE( "Failed to fork off valgrind helper: %m"));
LOG_MESSAGE_ID(SD_MESSAGE_VALGRIND_HELPER_FORK_STR),
LOG_MESSAGE("Failed to fork off valgrind helper: %m"));
else if (pid == 0)
exit(EXIT_SUCCESS);
else {
+2 -2
View File
@@ -763,8 +763,8 @@ bool valid_user_group_name(const char *u, ValidUserFlags flags) {
if (FLAGS_SET(flags, VALID_USER_WARN) && !valid_user_group_name(u, 0))
log_struct(LOG_NOTICE,
LOG_MESSAGE("Accepting user/group name '%s', which does not match strict user/group name rules.", u),
"USER_GROUP_NAME=%s", u,
"MESSAGE_ID=" SD_MESSAGE_UNSAFE_USER_NAME_STR);
LOG_ITEM("USER_GROUP_NAME=%s", u),
LOG_MESSAGE_ID(SD_MESSAGE_UNSAFE_USER_NAME_STR));
/* Note that we make no restrictions on the length in relaxed mode! */
} else {