diff options
author | Alois Klink <alois@aloisklink.com> | 2023-01-30 15:33:53 +0000 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2023-08-01 11:12:07 +0200 |
commit | 323e87dacc7b573a83200342dfb19e26ca720721 (patch) | |
tree | 59dd0669ffc48ab4912127484208d3f612ad54b7 | |
parent | 6698413af74b2d953f1eda8fbb164e9cd4777287 (diff) | |
download | cmocka-323e87dacc7b573a83200342dfb19e26ca720721.zip cmocka-323e87dacc7b573a83200342dfb19e26ca720721.tar.gz cmocka-323e87dacc7b573a83200342dfb19e26ca720721.tar.bz2 |
include: avoid GCC -Wuninitialized in assert_ptr*
In some cases, using the `assert_ptr_*()` macros causes GCC to throw
a `-Wmaybe-unitialized` warning.
For example:
```c
void * my_ptr = malloc(1);
// throws a `-Wmaybe-unitialized` warning
assert_non_null(my_ptr);
```
This is because GCC assumes that a function accepting a `const void *`
(or other constant pointer type) tries to read the pointer value.
See [the `-Wmaybe-unitialized` docs][1].
We can tell GCC that the `_assert_ptr_equal`/`_assert_ptr_not_equal`
functions only try to read the pointer address, not the pointer value,
by using the [`access` function attribute][2].
Since CMocka supports C99, we can't use the ISO C23 attribute syntax.
However, we can use the IBM/GCC/Clang `__attribute__` syntax hidden
behind `#ifdef` guards, so that they're ignored on compilers that don't
support `__attribute__`.
[1]: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#Warning-Options
[2]: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes
on-behalf-of: @nqminds <info@nqminds.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r-- | include/cmocka.h | 24 | ||||
-rw-r--r-- | src/cmocka.c | 2 |
2 files changed, 26 insertions, 0 deletions
diff --git a/include/cmocka.h b/include/cmocka.h index 193af52..70dd603 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -152,6 +152,28 @@ int __stdcall IsDebuggerPresent(); #define CMOCKA_NORETURN #endif +/** + * @def CMOCKA_NO_ACCESS_ATTRIBUTE + * + * Function attribute that tells the compiler that we never access the value + * of a/b, just the pointer address. + * + * Without this, newer compilers like GCC-12 will print + * `-Wmaybe-uninitialized` warnings. + * + * @see + * https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes + */ +#ifdef __has_attribute +#if __has_attribute(access) +#define CMOCKA_NO_ACCESS_ATTRIBUTE \ + __attribute__((access(none, 1), access(none, 2))) +#endif +#endif +#ifndef CMOCKA_NO_ACCESS_ATTRIBUTE +#define CMOCKA_NO_ACCESS_ATTRIBUTE +#endif + #define WILL_RETURN_ALWAYS -1 #define WILL_RETURN_ONCE -2 @@ -2923,10 +2945,12 @@ void _assert_uint_not_equal(const uintmax_t a, const uintmax_t b, const char * const file, const int line); +CMOCKA_NO_ACCESS_ATTRIBUTE void _assert_ptr_equal(const void *a, const void *b, const char *const file, const int line); +CMOCKA_NO_ACCESS_ATTRIBUTE void _assert_ptr_not_equal(const void *a, const void *b, const char *const file, diff --git a/src/cmocka.c b/src/cmocka.c index 211c315..43652d6 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -1365,6 +1365,7 @@ static bool int_values_not_equal_display_error(const intmax_t left, /* Returns 1 if the specified pointers are equal. If the pointers are not equal * an error is displayed and 0 is returned. */ +CMOCKA_NO_ACCESS_ATTRIBUTE static bool ptr_values_equal_display_error(const void *left, const void *right) { const bool equal = left == right; @@ -1376,6 +1377,7 @@ static bool ptr_values_equal_display_error(const void *left, const void *right) /* Returns 1 if the specified pointers are equal. If the pointers are not equal * an error is displayed and 0 is returned. */ +CMOCKA_NO_ACCESS_ATTRIBUTE static bool ptr_values_not_equal_display_error(const void *left, const void *right) { |