]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/log: assert that 0 is not passed as errno, except in test code
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 14 Apr 2021 13:55:16 +0000 (15:55 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 14 Apr 2021 15:07:06 +0000 (17:07 +0200)
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.

meson.build
src/basic/log.h
src/shared/bus-wait-for-jobs.c
src/test/test-bpf-foreign-programs.c

index 7d9f2d3d4c63da32784de22e9892bc79e2b8e615..8b4458d20578deaf4a0123d048fe4d4247ad6a69 100644 (file)
@@ -3318,11 +3318,15 @@ custom_target(
                    '} >@OUTPUT@'],
         build_by_default : true)
 
-# We intentionally do not do inline initializations with definitions for
-# a bunch of _cleanup_ variables in tests, to ensure valgrind is triggered.
-# This triggers a lot of maybe-uninitialized false positives when the
-# combination of -O2 and -flto is used. Suppress them.
-no_uninit = '-O2' in get_option('c_args') and '-flto=auto' in get_option('c_args') ? cc.first_supported_argument('-Wno-maybe-uninitialized') : []
+test_cflags = ['-DTEST_CODE=1']
+# We intentionally do not do inline initializations with definitions for a
+# bunch of _cleanup_ variables in tests, to ensure valgrind is triggered if we
+# use the variable unexpectedly. This triggers a lot of maybe-uninitialized
+# false positives when the combination of -O2 and -flto is used. Suppress them.
+if '-O2' in get_option('c_args') and '-flto=auto' in get_option('c_args')
+        test_cflags += cc.first_supported_argument('-Wno-maybe-uninitialized')
+endif
+
 foreach tuple : tests
         sources = tuple[0]
         link_with = tuple.length() > 1 and tuple[1].length() > 0 ? tuple[1] : [libshared]
@@ -3331,7 +3335,7 @@ foreach tuple : tests
         condition = tuple.length() > 4 ? tuple[4] : ''
         type = tuple.length() > 5 ? tuple[5] : ''
         defs = tuple.length() > 6 ? tuple[6] : []
-        defs += no_uninit
+        defs += test_cflags
         parallel = tuple.length() > 7 ? tuple[7] : true
         timeout = 30
 
@@ -3399,7 +3403,7 @@ exe = executable(
         'test-libudev-sym',
         test_libudev_sym_c,
         include_directories : libudev_includes,
-        c_args : ['-Wno-deprecated-declarations'] + no_uninit,
+        c_args : ['-Wno-deprecated-declarations'] + test_cflags,
         link_with : [libudev],
         build_by_default : want_tests != 'false',
         install : install_tests,
@@ -3412,7 +3416,7 @@ exe = executable(
         'test-libudev-static-sym',
         test_libudev_sym_c,
         include_directories : libudev_includes,
-        c_args : ['-Wno-deprecated-declarations'] + no_uninit,
+        c_args : ['-Wno-deprecated-declarations'] + test_cflags,
         link_with : [install_libudev_static],
         build_by_default : want_tests != 'false' and static_libudev_pic,
         install : install_tests and static_libudev_pic,
@@ -3453,7 +3457,7 @@ foreach tuple : fuzzers
                 include_directories : [incs, include_directories('src/fuzz')],
                 link_with : link_with,
                 dependencies : dependencies,
-                c_args : defs,
+                c_args : defs + test_cflags,
                 link_args: link_args,
                 install : false,
                 build_by_default : fuzz_tests or fuzzer_build)
index f7785b016b0f446ecf6b9a99b0ad84ebea3fd469..9a57fcd0f1c0434922aa4a47447e272b02406b2c 100644 (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__)
index e66c8beafab8124d3237477d26aae45efeb39811..8458fe8684770680d7ce198394dc0aa1c49739a7 100644 (file)
@@ -306,7 +306,8 @@ int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_ar
                         if (q < 0 && r == 0)
                                 r = q;
 
-                        log_debug_errno(q, "Got result %s/%m for job %s", d->result, d->name);
+                        log_full_errno_zerook(LOG_DEBUG, q,
+                                              "Got result %s/%m for job %s", d->result, d->name);
                 }
 
                 d->name = mfree(d->name);
index e7039240777da9408de29812c8f2ed1473b56fb7..666317e5209b81ed1d1c6239712cb3b418192a34 100644 (file)
@@ -293,7 +293,7 @@ int main(int argc, char *argv[]) {
 
         r = cg_all_unified();
         if (r <= 0)
-                return log_tests_skipped_errno(r, "Unified hierarchy is required, skipping.");
+                return log_tests_skipped("Unified hierarchy is required, skipping.");
 
         r = enter_cgroup_subroot(NULL);
         if (r == -ENOMEDIUM)