From 9201ca1af19375bbd4102c4c7fbdd8d713ded95c Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Sat, 17 May 2025 08:59:08 +0200 Subject: [PATCH] Merge pull request #27321 from vrabaud:norm Make sure to not access outside normDiffTabMake sure to not access outside normDiffTab #27321 If the norm is outside the array (e.g. Hamming), memory is read outside of the array, which does not matter because the invalid pointer is not used oustide of the function (e.g. the Hamming path is taken) but it triggers the sanitizer. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/core/src/norm.dispatch.cpp | 2 +- modules/core/src/norm.simd.hpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/core/src/norm.dispatch.cpp b/modules/core/src/norm.dispatch.cpp index 9f02df47e7..6999e55cdf 100644 --- a/modules/core/src/norm.dispatch.cpp +++ b/modules/core/src/norm.dispatch.cpp @@ -571,7 +571,7 @@ double norm( InputArray _src1, InputArray _src2, int normType, InputArray _mask ((normType == NORM_HAMMING || normType == NORM_HAMMING2) && src1.type() == CV_8U) ); NormDiffFunc func = getNormDiffFunc(normType >> 1, depth == CV_16F ? CV_32F : depth); - CV_Assert( func != 0 ); + CV_Assert( (normType >> 1) >= 3 || func != 0 ); if( src1.isContinuous() && src2.isContinuous() && mask.empty() ) { diff --git a/modules/core/src/norm.simd.hpp b/modules/core/src/norm.simd.hpp index 05227dde90..68bd21258e 100644 --- a/modules/core/src/norm.simd.hpp +++ b/modules/core/src/norm.simd.hpp @@ -1350,6 +1350,7 @@ NormDiffFunc getNormDiffFunc(int normType, int depth) (NormDiffFunc)normDiffL2_64f, 0 } }; + if (normType >= 3 || normType < 0) return nullptr; return normDiffTab[normType][depth]; }