From 504899d433a3b57278ab299567143beed91c4472 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Wed, 6 May 2026 16:56:03 +0200 Subject: [PATCH] Add CV_DISABLE_UBSAN to disable undefined behavior sanitizer Also fix code here and there that triggered the fuzzer. --- modules/core/include/opencv2/core/cvdef.h | 8 +++++++- modules/core/include/opencv2/core/fast_math.hpp | 1 + modules/imgproc/src/drawing.cpp | 1 + modules/imgproc/src/imgwarp.cpp | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/core/include/opencv2/core/cvdef.h b/modules/core/include/opencv2/core/cvdef.h index 3b955f8b00..65973153c3 100644 --- a/modules/core/include/opencv2/core/cvdef.h +++ b/modules/core/include/opencv2/core/cvdef.h @@ -684,7 +684,7 @@ __CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType) #endif /****************************************************************************************\ -* Thread sanitizer * +* Sanitizers * \****************************************************************************************/ #ifndef CV_THREAD_SANITIZER # if defined(__has_feature) @@ -694,6 +694,12 @@ __CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType) # endif #endif +#if defined(__clang__) || defined(__GNUC__) + #define CV_DISABLE_UBSAN __attribute__((no_sanitize("undefined"))) +#else + #define CV_DISABLE_UBSAN +#endif + /****************************************************************************************\ * exchange-add operation for atomic operations on reference counters * \****************************************************************************************/ diff --git a/modules/core/include/opencv2/core/fast_math.hpp b/modules/core/include/opencv2/core/fast_math.hpp index 48ca333a03..108d64b987 100644 --- a/modules/core/include/opencv2/core/fast_math.hpp +++ b/modules/core/include/opencv2/core/fast_math.hpp @@ -361,6 +361,7 @@ CV_INLINE int cvRound( int value ) } /** @overload */ +CV_DISABLE_UBSAN CV_INLINE int cvFloor( float value ) { #if defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \ diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index fcac9ff303..c89142c553 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -1494,6 +1494,7 @@ FillEdgeCollection( Mat& img, std::vector& edges, const void* color ) /* draws simple or filled circle */ +CV_DISABLE_UBSAN static void Circle( Mat& img, Point center, int radius, const void* color, int fill ) { diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 602ed802a2..85b216a9ce 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -2276,6 +2276,7 @@ void warpAffine(int src_type, parallel_for_(range, invoker, dst.total()/(double)(1<<16)); } +CV_DISABLE_UBSAN void warpAffineBlocklineNN(int *adelta, int *bdelta, short* xy, int X0, int Y0, int bw) { CALL_HAL(warpAffineBlocklineNN, cv_hal_warpAffineBlocklineNN, adelta, bdelta, xy, X0, Y0, bw); @@ -2306,6 +2307,7 @@ void warpAffineBlocklineNN(int *adelta, int *bdelta, short* xy, int X0, int Y0, } } +CV_DISABLE_UBSAN void warpAffineBlockline(int *adelta, int *bdelta, short* xy, short* alpha, int X0, int Y0, int bw) { CALL_HAL(warpAffineBlockline, cv_hal_warpAffineBlockline, adelta, bdelta, xy, alpha, X0, Y0, bw);