From 91a745adc5fa312e2235f944d30dcaee87ccd2aa Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 18 Dec 2025 10:26:04 +0100 Subject: [PATCH] Fix potential pointer overflow. It is undefined behavior in C++ to compute an adress out of the allocated zone (even when not dereferencing). https://en.cppreference.com/w/cpp/language/operator_arithmetic.html#Pointer_arithmetic --- modules/video/src/optflowgf.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/video/src/optflowgf.cpp b/modules/video/src/optflowgf.cpp index 02e878a577..7d654f747b 100644 --- a/modules/video/src/optflowgf.cpp +++ b/modules/video/src/optflowgf.cpp @@ -239,7 +239,6 @@ FarnebackUpdateMatrices( const Mat& _R0, const Mat& _R1, const Mat& _flow, Mat& #if 1 int x1 = cvFloor(fx), y1 = cvFloor(fy); - const float* ptr = R1 + y1*step1 + x1*5; float r2, r3, r4, r5, r6; fx -= x1; fy -= y1; @@ -247,6 +246,7 @@ FarnebackUpdateMatrices( const Mat& _R0, const Mat& _R1, const Mat& _flow, Mat& if( (unsigned)x1 < (unsigned)(width-1) && (unsigned)y1 < (unsigned)(height-1) ) { + const float* ptr = R1 + y1*step1 + x1*5; float a00 = (1.f-fx)*(1.f-fy), a01 = fx*(1.f-fy), a10 = (1.f-fx)*fy, a11 = fx*fy; @@ -262,12 +262,12 @@ FarnebackUpdateMatrices( const Mat& _R0, const Mat& _R1, const Mat& _flow, Mat& } #else int x1 = cvRound(fx), y1 = cvRound(fy); - const float* ptr = R1 + y1*step1 + x1*5; float r2, r3, r4, r5, r6; if( (unsigned)x1 < (unsigned)width && (unsigned)y1 < (unsigned)height ) { + const float* ptr = R1 + y1*step1 + x1*5; r2 = ptr[0]; r3 = ptr[1]; r4 = (R0[x*5+2] + ptr[2])*0.5f;