KUnit aborts the current thread when an assertion fails. Currently, this is done conditionally as part of the kunit_do_failed_assertion() function, but this hides the kunit_abort() call from the compiler (particularly if it's in another module). This, in turn, can lead to both suboptimal code generation (the compiler can't know if kunit_do_failed_assertion() will return), and to static analysis tools like smatch giving false positives.
Moving the kunit_abort() call into the macro should give the compiler and tools a better chance at understanding what's going on. Doing so requires exporting kunit_abort(), though it's recommended to continue to use assertions in lieu of aborting directly.
Suggested-by: Dan Carpenter dan.carpenter@linaro.org Signed-off-by: David Gow davidgow@google.com --- include/kunit/test.h | 4 ++++ lib/kunit/test.c | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h index 2f23d6efa505..6a35e3e2a1e5 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -481,6 +481,8 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); */ #define KUNIT_SUCCEED(test) do {} while (0)
+void __noreturn kunit_abort(struct kunit *test); + void kunit_do_failed_assertion(struct kunit *test, const struct kunit_loc *loc, enum kunit_assert_type type, @@ -498,6 +500,8 @@ void kunit_do_failed_assertion(struct kunit *test, assert_format, \ fmt, \ ##__VA_ARGS__); \ + if (assert_type == KUNIT_ASSERTION) \ + kunit_abort(test); \ } while (0)
diff --git a/lib/kunit/test.c b/lib/kunit/test.c index d3fb93a23ccc..3b350e50cab9 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -310,7 +310,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, string_stream_destroy(stream); }
-static void __noreturn kunit_abort(struct kunit *test) +void __noreturn kunit_abort(struct kunit *test) { kunit_try_catch_throw(&test->try_catch); /* Does not return. */
@@ -340,9 +340,6 @@ void kunit_do_failed_assertion(struct kunit *test, kunit_fail(test, loc, type, assert, assert_format, &message);
va_end(args); - - if (type == KUNIT_ASSERTION) - kunit_abort(test); } EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);