Skip to content

Commit be7c924

Browse files
committed
integer overflow fixed in getContinuousSize()
1 parent e5175db commit be7c924

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

modules/core/src/precomp.hpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,39 +127,46 @@ template<typename T> struct OpMax
127127
T operator ()(const T a, const T b) const { return std::max(a, b); }
128128
};
129129

130-
inline Size getContinuousSize( const Mat& m1, int widthScale=1 )
130+
inline Size getContinuousSize_(int flags, int cols, int rows, int widthScale)
131131
{
132-
return m1.isContinuous() ? Size(m1.cols*m1.rows*widthScale, 1) :
133-
Size(m1.cols*widthScale, m1.rows);
132+
int64 sz = (int64)cols * rows * widthScale;
133+
return (flags & Mat::CONTINUOUS_FLAG) != 0 &&
134+
(int)sz == sz ? Size((int)sz, 1) : Size(cols * widthScale, rows);
134135
}
135136

136-
inline Size getContinuousSize( const Mat& m1, const Mat& m2, int widthScale=1 )
137+
inline Size getContinuousSize(const Mat& m1, int widthScale = 1)
137138
{
138-
return (m1.flags & m2.flags & Mat::CONTINUOUS_FLAG) != 0 ?
139-
Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
139+
return getContinuousSize_(m1.flags,
140+
m1.cols, m1.rows, widthScale);
140141
}
141142

142-
inline Size getContinuousSize( const Mat& m1, const Mat& m2,
143-
const Mat& m3, int widthScale=1 )
143+
inline Size getContinuousSize(const Mat& m1, const Mat& m2, int widthScale = 1)
144144
{
145-
return (m1.flags & m2.flags & m3.flags & Mat::CONTINUOUS_FLAG) != 0 ?
146-
Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
145+
return getContinuousSize_(m1.flags & m2.flags,
146+
m1.cols, m1.rows, widthScale);
147147
}
148148

149-
inline Size getContinuousSize( const Mat& m1, const Mat& m2,
150-
const Mat& m3, const Mat& m4,
151-
int widthScale=1 )
149+
inline Size getContinuousSize(const Mat& m1, const Mat& m2,
150+
const Mat& m3, int widthScale = 1)
152151
{
153-
return (m1.flags & m2.flags & m3.flags & m4.flags & Mat::CONTINUOUS_FLAG) != 0 ?
154-
Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
152+
return getContinuousSize_(m1.flags & m2.flags & m3.flags,
153+
m1.cols, m1.rows, widthScale);
155154
}
156155

157-
inline Size getContinuousSize( const Mat& m1, const Mat& m2,
158-
const Mat& m3, const Mat& m4,
159-
const Mat& m5, int widthScale=1 )
156+
inline Size getContinuousSize(const Mat& m1, const Mat& m2,
157+
const Mat& m3, const Mat& m4,
158+
int widthScale = 1)
160159
{
161-
return (m1.flags & m2.flags & m3.flags & m4.flags & m5.flags & Mat::CONTINUOUS_FLAG) != 0 ?
162-
Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
160+
return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags,
161+
m1.cols, m1.rows, widthScale);
162+
}
163+
164+
inline Size getContinuousSize(const Mat& m1, const Mat& m2,
165+
const Mat& m3, const Mat& m4,
166+
const Mat& m5, int widthScale = 1)
167+
{
168+
return getContinuousSize_(m1.flags & m2.flags & m3.flags & m4.flags & m5.flags,
169+
m1.cols, m1.rows, widthScale);
163170
}
164171

165172
struct NoVec

0 commit comments

Comments
 (0)