Skip to content

Commit 43e0912

Browse files
committed
akaze: resolve issue with using of uninitialized memory
1 parent 6453300 commit 43e0912

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

modules/features2d/src/kaze/AKAZEFeatures.cpp

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -890,11 +890,11 @@ class SURF_Descriptor_Upright_64_Invoker : public ParallelLoopBody
890890
{
891891
for (int i = range.start; i < range.end; i++)
892892
{
893-
Get_SURF_Descriptor_Upright_64((*keypoints_)[i], descriptors_->ptr<float>(i));
893+
Get_SURF_Descriptor_Upright_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
894894
}
895895
}
896896

897-
void Get_SURF_Descriptor_Upright_64(const KeyPoint& kpt, float* desc) const;
897+
void Get_SURF_Descriptor_Upright_64(const KeyPoint& kpt, float* desc, int desc_size) const;
898898

899899
private:
900900
std::vector<KeyPoint>* keypoints_;
@@ -916,11 +916,11 @@ class SURF_Descriptor_64_Invoker : public ParallelLoopBody
916916
{
917917
for (int i = range.start; i < range.end; i++)
918918
{
919-
Get_SURF_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i));
919+
Get_SURF_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
920920
}
921921
}
922922

923-
void Get_SURF_Descriptor_64(const KeyPoint& kpt, float* desc) const;
923+
void Get_SURF_Descriptor_64(const KeyPoint& kpt, float* desc, int desc_size) const;
924924

925925
private:
926926
std::vector<KeyPoint>* keypoints_;
@@ -942,11 +942,11 @@ class MSURF_Upright_Descriptor_64_Invoker : public ParallelLoopBody
942942
{
943943
for (int i = range.start; i < range.end; i++)
944944
{
945-
Get_MSURF_Upright_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i));
945+
Get_MSURF_Upright_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
946946
}
947947
}
948948

949-
void Get_MSURF_Upright_Descriptor_64(const KeyPoint& kpt, float* desc) const;
949+
void Get_MSURF_Upright_Descriptor_64(const KeyPoint& kpt, float* desc, int desc_size) const;
950950

951951
private:
952952
std::vector<KeyPoint>* keypoints_;
@@ -968,11 +968,11 @@ class MSURF_Descriptor_64_Invoker : public ParallelLoopBody
968968
{
969969
for (int i = range.start; i < range.end; i++)
970970
{
971-
Get_MSURF_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i));
971+
Get_MSURF_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
972972
}
973973
}
974974

975-
void Get_MSURF_Descriptor_64(const KeyPoint& kpt, float* desc) const;
975+
void Get_MSURF_Descriptor_64(const KeyPoint& kpt, float* desc, int desc_size) const;
976976

977977
private:
978978
std::vector<KeyPoint>* keypoints_;
@@ -995,11 +995,11 @@ class Upright_MLDB_Full_Descriptor_Invoker : public ParallelLoopBody
995995
{
996996
for (int i = range.start; i < range.end; i++)
997997
{
998-
Get_Upright_MLDB_Full_Descriptor((*keypoints_)[i], descriptors_->ptr<unsigned char>(i));
998+
Get_Upright_MLDB_Full_Descriptor((*keypoints_)[i], descriptors_->ptr<unsigned char>(i), descriptors_->cols);
999999
}
10001000
}
10011001

1002-
void Get_Upright_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char* desc) const;
1002+
void Get_Upright_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char* desc, int desc_size) const;
10031003

10041004
private:
10051005
std::vector<KeyPoint>* keypoints_;
@@ -1030,11 +1030,11 @@ class Upright_MLDB_Descriptor_Subset_Invoker : public ParallelLoopBody
10301030
{
10311031
for (int i = range.start; i < range.end; i++)
10321032
{
1033-
Get_Upright_MLDB_Descriptor_Subset((*keypoints_)[i], descriptors_->ptr<unsigned char>(i));
1033+
Get_Upright_MLDB_Descriptor_Subset((*keypoints_)[i], descriptors_->ptr<unsigned char>(i), descriptors_->cols);
10341034
}
10351035
}
10361036

1037-
void Get_Upright_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char* desc) const;
1037+
void Get_Upright_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char* desc, int desc_size) const;
10381038

10391039
private:
10401040
std::vector<KeyPoint>* keypoints_;
@@ -1061,11 +1061,11 @@ class MLDB_Full_Descriptor_Invoker : public ParallelLoopBody
10611061
{
10621062
for (int i = range.start; i < range.end; i++)
10631063
{
1064-
Get_MLDB_Full_Descriptor((*keypoints_)[i], descriptors_->ptr<unsigned char>(i));
1064+
Get_MLDB_Full_Descriptor((*keypoints_)[i], descriptors_->ptr<unsigned char>(i), descriptors_->cols);
10651065
}
10661066
}
10671067

1068-
void Get_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char* desc) const;
1068+
void Get_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char* desc, int desc_size) const;
10691069
void MLDB_Fill_Values(float* values, int sample_step, int level,
10701070
float xf, float yf, float co, float si, float scale) const;
10711071
void MLDB_Binary_Comparisons(float* values, unsigned char* desc,
@@ -1100,11 +1100,11 @@ class MLDB_Descriptor_Subset_Invoker : public ParallelLoopBody
11001100
{
11011101
for (int i = range.start; i < range.end; i++)
11021102
{
1103-
Get_MLDB_Descriptor_Subset((*keypoints_)[i], descriptors_->ptr<unsigned char>(i));
1103+
Get_MLDB_Descriptor_Subset((*keypoints_)[i], descriptors_->ptr<unsigned char>(i), descriptors_->cols);
11041104
}
11051105
}
11061106

1107-
void Get_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char* desc) const;
1107+
void Get_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char* desc, int desc_size) const;
11081108

11091109
private:
11101110
std::vector<KeyPoint>* keypoints_;
@@ -1407,23 +1407,25 @@ void AKAZEFeatures::Compute_Keypoints_Orientation(std::vector<KeyPoint>& kpts) c
14071407
* from Agrawal et al., CenSurE: Center Surround Extremas for Realtime Feature Detection and Matching,
14081408
* ECCV 2008
14091409
*/
1410-
void MSURF_Upright_Descriptor_64_Invoker::Get_MSURF_Upright_Descriptor_64(const KeyPoint& kpt, float *desc) const {
1410+
void MSURF_Upright_Descriptor_64_Invoker::Get_MSURF_Upright_Descriptor_64(const KeyPoint& kpt, float *desc, int desc_size) const {
1411+
1412+
const int dsize = 64;
1413+
CV_Assert(desc_size == dsize);
14111414

14121415
float dx = 0.0, dy = 0.0, mdx = 0.0, mdy = 0.0, gauss_s1 = 0.0, gauss_s2 = 0.0;
14131416
float rx = 0.0, ry = 0.0, len = 0.0, xf = 0.0, yf = 0.0, ys = 0.0, xs = 0.0;
14141417
float sample_x = 0.0, sample_y = 0.0;
14151418
int x1 = 0, y1 = 0, sample_step = 0, pattern_size = 0;
14161419
int x2 = 0, y2 = 0, kx = 0, ky = 0, i = 0, j = 0, dcount = 0;
14171420
float fx = 0.0, fy = 0.0, ratio = 0.0, res1 = 0.0, res2 = 0.0, res3 = 0.0, res4 = 0.0;
1418-
int scale = 0, dsize = 0;
1421+
int scale = 0;
14191422

14201423
// Subregion centers for the 4x4 gaussian weighting
14211424
float cx = -0.5f, cy = 0.5f;
14221425

14231426
const std::vector<Evolution>& evolution = *evolution_;
14241427

14251428
// Set the descriptor size and the sample and pattern sizes
1426-
dsize = 64;
14271429
sample_step = 5;
14281430
pattern_size = 12;
14291431

@@ -1466,11 +1468,11 @@ void MSURF_Upright_Descriptor_64_Invoker::Get_MSURF_Upright_Descriptor_64(const
14661468
//Get the gaussian weighted x and y responses
14671469
gauss_s1 = gaussian(xs - sample_x, ys - sample_y, 2.50f*scale);
14681470

1469-
y1 = (int)(sample_y - .5);
1470-
x1 = (int)(sample_x - .5);
1471+
y1 = (int)(sample_y - .5f);
1472+
x1 = (int)(sample_x - .5f);
14711473

1472-
y2 = (int)(sample_y + .5);
1473-
x2 = (int)(sample_x + .5);
1474+
y2 = (int)(sample_y + .5f);
1475+
x2 = (int)(sample_x + .5f);
14741476

14751477
fx = sample_x - x1;
14761478
fy = sample_y - y1;
@@ -1514,6 +1516,8 @@ void MSURF_Upright_Descriptor_64_Invoker::Get_MSURF_Upright_Descriptor_64(const
15141516
i += 9;
15151517
}
15161518

1519+
CV_Assert(dcount == desc_size);
1520+
15171521
// convert to unit vector
15181522
len = sqrt(len);
15191523

@@ -1532,23 +1536,25 @@ void MSURF_Upright_Descriptor_64_Invoker::Get_MSURF_Upright_Descriptor_64(const
15321536
* from Agrawal et al., CenSurE: Center Surround Extremas for Realtime Feature Detection and Matching,
15331537
* ECCV 2008
15341538
*/
1535-
void MSURF_Descriptor_64_Invoker::Get_MSURF_Descriptor_64(const KeyPoint& kpt, float *desc) const {
1539+
void MSURF_Descriptor_64_Invoker::Get_MSURF_Descriptor_64(const KeyPoint& kpt, float *desc, int desc_size) const {
1540+
1541+
const int dsize = 64;
1542+
CV_Assert(desc_size == dsize);
15361543

15371544
float dx = 0.0, dy = 0.0, mdx = 0.0, mdy = 0.0, gauss_s1 = 0.0, gauss_s2 = 0.0;
15381545
float rx = 0.0, ry = 0.0, rrx = 0.0, rry = 0.0, len = 0.0, xf = 0.0, yf = 0.0, ys = 0.0, xs = 0.0;
15391546
float sample_x = 0.0, sample_y = 0.0, co = 0.0, si = 0.0, angle = 0.0;
15401547
float fx = 0.0, fy = 0.0, ratio = 0.0, res1 = 0.0, res2 = 0.0, res3 = 0.0, res4 = 0.0;
15411548
int x1 = 0, y1 = 0, x2 = 0, y2 = 0, sample_step = 0, pattern_size = 0;
15421549
int kx = 0, ky = 0, i = 0, j = 0, dcount = 0;
1543-
int scale = 0, dsize = 0;
1550+
int scale = 0;
15441551

15451552
// Subregion centers for the 4x4 gaussian weighting
15461553
float cx = -0.5f, cy = 0.5f;
15471554

15481555
const std::vector<Evolution>& evolution = *evolution_;
15491556

15501557
// Set the descriptor size and the sample and pattern sizes
1551-
dsize = 64;
15521558
sample_step = 5;
15531559
pattern_size = 12;
15541560

@@ -1652,6 +1658,8 @@ void MSURF_Descriptor_64_Invoker::Get_MSURF_Descriptor_64(const KeyPoint& kpt, f
16521658
i += 9;
16531659
}
16541660

1661+
CV_Assert(dcount == desc_size);
1662+
16551663
// convert to unit vector
16561664
len = sqrt(len);
16571665

@@ -1667,7 +1675,7 @@ void MSURF_Descriptor_64_Invoker::Get_MSURF_Descriptor_64(const KeyPoint& kpt, f
16671675
* @param kpt Input keypoint
16681676
* @param desc Descriptor vector
16691677
*/
1670-
void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char *desc) const {
1678+
void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char *desc, int desc_size) const {
16711679

16721680
float di = 0.0, dx = 0.0, dy = 0.0;
16731681
float ri = 0.0, rx = 0.0, ry = 0.0, xf = 0.0, yf = 0.0;
@@ -1703,6 +1711,8 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons
17031711
divUp(pattern_size, 2)
17041712
};
17051713

1714+
memset(desc, 0, desc_size);
1715+
17061716
// For the three grids
17071717
for (int z = 0; z < 3; z++) {
17081718
dcount2 = 0;
@@ -1754,15 +1764,16 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons
17541764
for (int k = 0; k < 3; ++k) {
17551765
if (*(valI + k) > *(valJ + k)) {
17561766
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1757-
} else {
1758-
desc[dcount1 / 8] &= ~(1 << (dcount1 % 8));
17591767
}
17601768
dcount1++;
17611769
}
17621770
}
17631771
}
17641772

17651773
} // for (int z = 0; z < 3; z++)
1774+
1775+
CV_Assert(dcount1 <= desc_size*8);
1776+
CV_Assert(divUp(dcount1, 8) == desc_size);
17661777
}
17671778

17681779
void MLDB_Full_Descriptor_Invoker::MLDB_Fill_Values(float* values, int sample_step, const int level,
@@ -1848,10 +1859,6 @@ void MLDB_Full_Descriptor_Invoker::MLDB_Binary_Comparisons(float* values, unsign
18481859
if (ival > ivalues[chan * j + pos]) {
18491860
desc[dpos >> 3] |= (1 << (dpos & 7));
18501861
}
1851-
else {
1852-
desc[dpos >> 3] &= ~(1 << (dpos & 7));
1853-
}
1854-
18551862
dpos++;
18561863
}
18571864
}
@@ -1865,7 +1872,7 @@ void MLDB_Full_Descriptor_Invoker::MLDB_Binary_Comparisons(float* values, unsign
18651872
* @param kpt Input keypoint
18661873
* @param desc Descriptor vector
18671874
*/
1868-
void MLDB_Full_Descriptor_Invoker::Get_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char *desc) const {
1875+
void MLDB_Full_Descriptor_Invoker::Get_MLDB_Full_Descriptor(const KeyPoint& kpt, unsigned char *desc, int desc_size) const {
18691876

18701877
const int max_channels = 3;
18711878
CV_Assert(options_->descriptor_channels <= max_channels);
@@ -1888,13 +1895,18 @@ void MLDB_Full_Descriptor_Invoker::Get_MLDB_Full_Descriptor(const KeyPoint& kpt,
18881895
float co = cos(angle);
18891896
float si = sin(angle);
18901897

1891-
int dpos = 0;
1892-
for(int lvl = 0; lvl < 3; lvl++) {
1898+
memset(desc, 0, desc_size);
18931899

1900+
int dpos = 0;
1901+
for(int lvl = 0; lvl < 3; lvl++)
1902+
{
18941903
int val_count = (lvl + 2) * (lvl + 2);
18951904
MLDB_Fill_Values(values, sample_step[lvl], kpt.class_id, xf, yf, co, si, scale);
18961905
MLDB_Binary_Comparisons(values, desc, val_count, dpos);
18971906
}
1907+
1908+
CV_Assert(dpos == 486);
1909+
CV_Assert(divUp(dpos, 8) == desc_size);
18981910
}
18991911

19001912
/* ************************************************************************* */
@@ -1905,7 +1917,7 @@ void MLDB_Full_Descriptor_Invoker::Get_MLDB_Full_Descriptor(const KeyPoint& kpt,
19051917
* @param kpt Input keypoint
19061918
* @param desc Descriptor vector
19071919
*/
1908-
void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char *desc) const {
1920+
void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char *desc, int desc_size) const {
19091921

19101922
float di = 0.f, dx = 0.f, dy = 0.f;
19111923
float rx = 0.f, ry = 0.f;
@@ -1995,11 +2007,12 @@ void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint&
19952007
// Do the comparisons
19962008
const int *comps = descriptorBits_.ptr<int>(0);
19972009

2010+
CV_Assert(divUp(descriptorBits_.rows, 8) == desc_size);
2011+
memset(desc, 0, desc_size);
2012+
19982013
for (int i = 0; i<descriptorBits_.rows; i++) {
19992014
if (values[comps[2 * i]] > values[comps[2 * i + 1]]) {
20002015
desc[i / 8] |= (1 << (i % 8));
2001-
} else {
2002-
desc[i / 8] &= ~(1 << (i % 8));
20032016
}
20042017
}
20052018
}
@@ -2012,7 +2025,7 @@ void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint&
20122025
* @param kpt Input keypoint
20132026
* @param desc Descriptor vector
20142027
*/
2015-
void Upright_MLDB_Descriptor_Subset_Invoker::Get_Upright_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char *desc) const {
2028+
void Upright_MLDB_Descriptor_Subset_Invoker::Get_Upright_MLDB_Descriptor_Subset(const KeyPoint& kpt, unsigned char *desc, int desc_size) const {
20162029

20172030
float di = 0.0f, dx = 0.0f, dy = 0.0f;
20182031
float rx = 0.0f, ry = 0.0f;
@@ -2090,11 +2103,12 @@ void Upright_MLDB_Descriptor_Subset_Invoker::Get_Upright_MLDB_Descriptor_Subset(
20902103
const float *vals = values.ptr<float>(0);
20912104
const int *comps = descriptorBits_.ptr<int>(0);
20922105

2106+
CV_Assert(divUp(descriptorBits_.rows, 8) == desc_size);
2107+
memset(desc, 0, desc_size);
2108+
20932109
for (int i = 0; i<descriptorBits_.rows; i++) {
20942110
if (vals[comps[2 * i]] > vals[comps[2 * i + 1]]) {
20952111
desc[i / 8] |= (1 << (i % 8));
2096-
} else {
2097-
desc[i / 8] &= ~(1 << (i % 8));
20982112
}
20992113
}
21002114
}

0 commit comments

Comments
 (0)