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>
This commit is contained in:
parent
6698413af7
commit
323e87dacc
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue