Skip to content

Commit 35a1ece

Browse files
committed
core: fix infinite recursion in compare
1 parent 82ec76c commit 35a1ece

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

modules/core/src/arithm.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,15 +1239,18 @@ void cv::compare(InputArray _src1, InputArray _src2, OutputArray _dst, int op)
12391239
|| !_src1.sameSize(_src2)
12401240
|| _src1.type() != _src2.type())
12411241
{
1242-
if (checkScalar(_src1, _src2.type(), _src1.kind(), _src2.kind()))
1242+
bool is_src1_scalar = checkScalar(_src1, _src2.type(), _src1.kind(), _src2.kind());
1243+
bool is_src2_scalar = checkScalar(_src2, _src1.type(), _src2.kind(), _src1.kind());
1244+
1245+
if (is_src1_scalar && !is_src2_scalar)
12431246
{
12441247
op = op == CMP_LT ? CMP_GT : op == CMP_LE ? CMP_GE :
12451248
op == CMP_GE ? CMP_LE : op == CMP_GT ? CMP_LT : op;
12461249
// src1 is a scalar; swap it with src2
12471250
compare(_src2, _src1, _dst, op);
12481251
return;
12491252
}
1250-
else if( !checkScalar(_src2, _src1.type(), _src2.kind(), _src1.kind()) )
1253+
else if( (is_src1_scalar && is_src2_scalar) || (!is_src1_scalar && !is_src2_scalar) )
12511254
CV_Error( CV_StsUnmatchedSizes,
12521255
"The operation is neither 'array op array' (where arrays have the same size and the same type), "
12531256
"nor 'array op scalar', nor 'scalar op array'" );

modules/core/test/test_arithm.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,3 +1922,13 @@ TEST(Compare, empty)
19221922
EXPECT_TRUE(dst1.empty());
19231923
EXPECT_TRUE(dst2.empty());
19241924
}
1925+
1926+
TEST(Compare, regression_8999)
1927+
{
1928+
Mat_<double> A(4,1); A << 1, 3, 2, 4;
1929+
Mat_<double> B(1,1); B << 2;
1930+
Mat C;
1931+
ASSERT_ANY_THROW({
1932+
compare(A, B, C, CMP_LT);
1933+
});
1934+
}

0 commit comments

Comments
 (0)