From a31a87f8943aa4029ff0f23a6dc46f5d0c895e8b Mon Sep 17 00:00:00 2001 From: Ankit Meel Date: Thu, 1 Oct 2020 23:51:01 +0530 Subject: T81340: UBSan: load of value .. not valid for GPU enum type The underlying type of the enum cannot be fixed here due to its usage in C code. All the values possible in the width of the underlying type are not valid for an enum. Only 0 to (2*max - 1) if all enumerators are unsigned. So the macro asks for the biggest value among the //listed// ones. If any enumerator C is set to say `A|B`, then C would be the maximum. (2*max-1) is used as the mask. The warnings (for each enum modified in this commit): GPU_vertex_buffer.h:43:1: runtime error: load of value 4294967291 which is not a valid value for type 'GPUVertBufStatus' https://github.com/llvm/llvm-project/commit/1c2c9867 Ref T81340 Reviewed By: fclem Differential Revision: https://developer.blender.org/D9067 --- source/blender/blenlib/BLI_utildefines.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source/blender/blenlib/BLI_utildefines.h') diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 326015e8d80..40d24f07c25 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -782,7 +782,8 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size); /* Useful to port C code using enums to C++ where enums are strongly typed. * To use after the enum declaration. */ -# define ENUM_OPERATORS(_enum_type) \ +/* If any enumerator `C` is set to say `A|B`, then `C` would be the max enum value. */ +# define ENUM_OPERATORS(_enum_type, _max_enum_value) \ inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \ { \ return static_cast<_enum_type>(static_cast(a) | b); \ @@ -793,7 +794,7 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size); } \ inline constexpr _enum_type operator~(_enum_type a) \ { \ - return static_cast<_enum_type>(~static_cast(a)); \ + return static_cast<_enum_type>(~static_cast(a) & (2 * _max_enum_value - 1)); \ } \ inline _enum_type &operator|=(_enum_type &a, _enum_type b) \ { \ @@ -806,7 +807,7 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size); #else /* Output nothing. */ -# define ENUM_OPERATORS(_type) +# define ENUM_OPERATORS(_type, _max) #endif /** \} */ -- cgit v1.2.3