aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnderson Toshiyuki Sasaki <ansasaki@redhat.com>2020-03-13 18:37:19 +0100
committerAndreas Schneider <asn@cryptomilk.org>2020-10-04 09:45:39 +0200
commita731966c628ee519a53eaf9eda66834d9800f8fa (patch)
treec1f9a7a6882815f688d30867cef37466590e06fb
parent9c114ac31a33217cf003bbb674c1aff7bb048917 (diff)
downloadcmocka-a731966c628ee519a53eaf9eda66834d9800f8fa.tar.gz
cmocka-a731966c628ee519a53eaf9eda66834d9800f8fa.tar.xz
cmocka-a731966c628ee519a53eaf9eda66834d9800f8fa.zip
cmocka: Add compiler attributes to non-returning functions
This introduces the CMOCKA_NORETURN macro which adds the __attribute__((noreturn)) attribute to non-returning functions. In Windows, __declspec(noreturn) is added instead. Functions that don't return but are not marked with the attribute can confuse static analysers, making them to report false positives. Add CMOCKA_NORETURN attribute to _fail(), _skip(), and to the internal exception_handler(). Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--include/cmocka.h12
-rw-r--r--src/cmocka.c11
2 files changed, 20 insertions, 3 deletions
diff --git a/include/cmocka.h b/include/cmocka.h
index 0aa557e..06cbfbf 100644
--- a/include/cmocka.h
+++ b/include/cmocka.h
@@ -172,6 +172,14 @@ cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
#define CMOCKA_DEPRECATED
#endif
+#if defined(__GNUC__)
+#define CMOCKA_NORETURN __attribute__ ((noreturn))
+#elif defined(_MSC_VER)
+#define CMOCKA_NORETURN __declspec(noreturn)
+#else
+#define CMOCKA_NORETURN
+#endif
+
#define WILL_RETURN_ALWAYS -1
#define WILL_RETURN_ONCE -2
@@ -2362,9 +2370,9 @@ void* _test_calloc(const size_t number_of_elements, const size_t size,
const char* file, const int line);
void _test_free(void* const ptr, const char* file, const int line);
-void _fail(const char * const file, const int line);
+CMOCKA_NORETURN void _fail(const char * const file, const int line);
-void _skip(const char * const file, const int line);
+CMOCKA_NORETURN void _skip(const char * const file, const int line);
int _run_test(
const char * const function_name, const UnitTestFunction Function,
diff --git a/src/cmocka.c b/src/cmocka.c
index 5863a27..4b3af05 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -416,6 +416,9 @@ void _skip(const char * const file, const int line)
cm_print_error(SOURCE_LOCATION_FORMAT ": Skipped!\n", file, line);
global_skip_test = 1;
exit_test(1);
+
+ /* Unreachable */
+ exit(-1);
}
/* Initialize a SourceLocation structure. */
@@ -2290,11 +2293,14 @@ void _fail(const char * const file, const int line) {
break;
}
exit_test(1);
+
+ /* Unreachable */
+ exit(-1);
}
#ifndef _WIN32
-static void exception_handler(int sig) {
+CMOCKA_NORETURN static void exception_handler(int sig) {
const char *sig_strerror = "";
#ifdef HAVE_STRSIGNAL
@@ -2304,6 +2310,9 @@ static void exception_handler(int sig) {
cm_print_error("Test failed with exception: %s(%d)",
sig_strerror, sig);
exit_test(1);
+
+ /* Unreachable */
+ exit(-1);
}
#else /* _WIN32 */