basic/log: assert that 0 is not passed as errno, except in test code

Let's assert if we ever happen to pass 0 to one of the log functions.
With the preceding commit to return -EIO from log_*(), passing 0 wouldn't
affect the return value any more, but it is still most likely an error.
The unit test code is an exception: we fairly often pass the return value
to print it, before checking what it is. So let's assert that we're not
passing 0 in non-test code. As with the previous check for %m, this is only
done in developer mode. We are depending on external code setting
errno correctly for us, which might not always be true, and which we can't
test, so we shouldn't assert, but just handle this gracefully.

I did a bunch of greps to try to figure out if there are any places where
we're passing 0 on purpose, and couldn't find any.
The one place that failed in tests is adjusted.

About "zerook" in the name: I wanted the suffix to be unambiguous. It's a
single "word" because each of the words in log_full_errno is also meaningful,
and having one term use two words would be confusing.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-04-14 17:07:06 +02:00
parent 63275a7032
commit a626cb15c0
4 changed files with 32 additions and 14 deletions
+16 -3
View File
@@ -189,7 +189,7 @@ void log_assert_failed_return(
log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
/* Logging with level */
#define log_full_errno(level, error, ...) \
#define log_full_errno_zerook(level, error, ...) \
({ \
int _level = (level), _e = (error); \
_e = (log_get_max_level() >= LOG_PRI(_level)) \
@@ -198,17 +198,30 @@ void log_assert_failed_return(
_e < 0 ? _e : -EIO; \
})
#if BUILD_MODE_DEVELOPER && !defined(TEST_CODE)
# define ASSERT_NON_ZERO(x) assert((x) != 0)
#else
# define ASSERT_NON_ZERO(x)
#endif
#define log_full_errno(level, error, ...) \
({ \
int _error = (error); \
ASSERT_NON_ZERO(_error); \
log_full_errno_zerook(level, _error, __VA_ARGS__); \
})
#define log_full(level, fmt, ...) \
({ \
if (BUILD_MODE_DEVELOPER) \
assert(!strstr(fmt, "%m")); \
(void) log_full_errno((level), 0, fmt, ##__VA_ARGS__); \
(void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \
})
int log_emergency_level(void);
/* Normal logging */
#define log_debug(...) log_full_errno(LOG_DEBUG, 0, __VA_ARGS__)
#define log_debug(...) log_full_errno_zerook(LOG_DEBUG, 0, __VA_ARGS__)
#define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)