Skip to content

Commit 1f23202

Browse files
committed
Issues found by static analysis (5th round)
1 parent e5aa213 commit 1f23202

File tree

10 files changed

+53
-49
lines changed

10 files changed

+53
-49
lines changed

modules/calib3d/src/calibinit.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,9 @@ icvGenerateQuads( CvCBQuad **out_quads, CvCBCorner **out_corners,
18361836
assert( src_contour->total == 4 );
18371837
for( i = 0; i < 4; i++ )
18381838
{
1839-
CvPoint2D32f pt = cvPointTo32f(*(CvPoint*)cvGetSeqElem(src_contour, i));
1839+
CvPoint * onePoint = (CvPoint*)cvGetSeqElem(src_contour, i);
1840+
CV_Assert(onePoint != NULL);
1841+
CvPoint2D32f pt = cvPointTo32f(*onePoint);
18401842
CvCBCorner* corner = &(*out_corners)[quad_count*4 + i];
18411843

18421844
memset( corner, 0, sizeof(*corner) );

modules/core/src/datastructs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@ cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_v
28822882
break;
28832883
}
28842884

2885-
assert( edge != 0 );
2885+
CV_Assert( edge != 0 );
28862886

28872887
next_edge = edge->next[ofs];
28882888
if( prev_edge )

modules/core/src/opencl/runtime/opencl_core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ static void* opencl_check_fn(int ID)
257257
const struct DynamicFnEntry* e = NULL;
258258
if (ID < CUSTOM_FUNCTION_ID)
259259
{
260-
assert(ID >= 0 && ID < (int)(sizeof(opencl_fn_list)/sizeof(opencl_fn_list[0])));
260+
CV_Assert(ID >= 0 && ID < (int)(sizeof(opencl_fn_list)/sizeof(opencl_fn_list[0])));
261261
e = opencl_fn_list[ID];
262262
}
263263
#ifdef HAVE_OPENCL_SVM
264264
else if (ID >= SVM_FUNCTION_ID_START && ID < SVM_FUNCTION_ID_END)
265265
{
266266
ID = ID - SVM_FUNCTION_ID_START;
267-
assert(ID >= 0 && ID < (int)(sizeof(opencl_svm_fn_list)/sizeof(opencl_svm_fn_list[0])));
267+
CV_Assert(ID >= 0 && ID < (int)(sizeof(opencl_svm_fn_list)/sizeof(opencl_svm_fn_list[0])));
268268
e = opencl_svm_fn_list[ID];
269269
}
270270
#endif

modules/imgproc/src/contours.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ cvFindNextContour( CvContourScanner scanner )
11791179
cur = cur->next;
11801180
}
11811181

1182-
assert( par_info != 0 );
1182+
CV_Assert( par_info != 0 );
11831183

11841184
/* if current contour is a hole and previous contour is a hole or
11851185
current contour is external and previous contour is external then

modules/imgproc/src/convhull.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,6 @@ CV_IMPL CvSeq*
404404
cvConvexHull2( const CvArr* array, void* hull_storage,
405405
int orientation, int return_points )
406406
{
407-
union { CvContour* c; CvSeq* s; } hull;
408-
hull.s = 0;
409-
410407
CvMat* mat = 0;
411408
CvContour contour_header;
412409
CvSeq hull_header;
@@ -427,7 +424,9 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
427424
ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
428425
}
429426

430-
if( CV_IS_STORAGE( hull_storage ))
427+
bool isStorage = isStorageOrMat(hull_storage);
428+
429+
if(isStorage)
431430
{
432431
if( return_points )
433432
{
@@ -445,9 +444,6 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
445444
}
446445
else
447446
{
448-
if( !CV_IS_MAT( hull_storage ))
449-
CV_Error(CV_StsBadArg, "Destination must be valid memory storage or matrix");
450-
451447
mat = (CvMat*)hull_storage;
452448

453449
if( (mat->cols != 1 && mat->rows != 1) || !CV_IS_MAT_CONT(mat->type))
@@ -473,10 +469,10 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
473469
int total = ptseq->total;
474470
if( total == 0 )
475471
{
476-
if( mat )
472+
if( !isStorage )
477473
CV_Error( CV_StsBadSize,
478474
"Point sequence can not be empty if the output is matrix" );
479-
return hull.s;
475+
return 0;
480476
}
481477

482478
cv::AutoBuffer<double> _ptbuf;
@@ -498,22 +494,18 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
498494
else
499495
cvSeqPushMulti(hullseq, h0.ptr(), (int)h0.total());
500496

501-
if( mat )
497+
if (isStorage)
498+
{
499+
return hullseq;
500+
}
501+
else
502502
{
503503
if( mat->rows > mat->cols )
504504
mat->rows = hullseq->total;
505505
else
506506
mat->cols = hullseq->total;
507+
return 0;
507508
}
508-
else
509-
{
510-
hull.s = hullseq;
511-
hull.c->rect = cvBoundingRect( ptseq,
512-
ptseq->header_size < (int)sizeof(CvContour) ||
513-
&ptseq->flags == &contour_header.flags );
514-
}
515-
516-
return hull.s;
517509
}
518510

519511

modules/imgproc/src/hough.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
894894
cv::Mat image = cv::cvarrToMat(src_image);
895895
std::vector<cv::Vec2f> l2;
896896
std::vector<cv::Vec4i> l4;
897-
CvSeq* result = 0;
898897

899898
CvMat* mat = 0;
900899
CvSeq* lines = 0;
@@ -921,11 +920,13 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
921920
elemSize = sizeof(int)*4;
922921
}
923922

924-
if( CV_IS_STORAGE( lineStorage ))
923+
bool isStorage = isStorageOrMat(lineStorage);
924+
925+
if( isStorage )
925926
{
926927
lines = cvCreateSeq( lineType, sizeof(CvSeq), elemSize, (CvMemStorage*)lineStorage );
927928
}
928-
else if( CV_IS_MAT( lineStorage ))
929+
else
929930
{
930931
mat = (CvMat*)lineStorage;
931932

@@ -942,8 +943,6 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
942943
linesMax = lines->total;
943944
cvClearSeq( lines );
944945
}
945-
else
946-
CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
947946

948947
iparam1 = cvRound(param1);
949948
iparam2 = cvRound(param2);
@@ -968,7 +967,7 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
968967

969968
int nlines = (int)(l2.size() + l4.size());
970969

971-
if( mat )
970+
if( !isStorage )
972971
{
973972
if( mat->cols > mat->rows )
974973
mat->cols = nlines;
@@ -981,20 +980,20 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
981980
cv::Mat lx = method == CV_HOUGH_STANDARD || method == CV_HOUGH_MULTI_SCALE ?
982981
cv::Mat(nlines, 1, CV_32FC2, &l2[0]) : cv::Mat(nlines, 1, CV_32SC4, &l4[0]);
983982

984-
if( mat )
983+
if (isStorage)
985984
{
986-
cv::Mat dst(nlines, 1, lx.type(), mat->data.ptr);
987-
lx.copyTo(dst);
985+
cvSeqPushMulti(lines, lx.ptr(), nlines);
988986
}
989987
else
990988
{
991-
cvSeqPushMulti(lines, lx.ptr(), nlines);
989+
cv::Mat dst(nlines, 1, lx.type(), mat->data.ptr);
990+
lx.copyTo(dst);
992991
}
993992
}
994993

995-
if( !mat )
996-
result = lines;
997-
return result;
994+
if( isStorage )
995+
return lines;
996+
return 0;
998997
}
999998

1000999

@@ -1227,8 +1226,6 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
12271226
double param1, double param2,
12281227
int min_radius, int max_radius )
12291228
{
1230-
CvSeq* result = 0;
1231-
12321229
CvMat stub, *img = (CvMat*)src_image;
12331230
CvMat* mat = 0;
12341231
CvSeq* circles = 0;
@@ -1255,12 +1252,14 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
12551252
else if( max_radius <= min_radius )
12561253
max_radius = min_radius + 2;
12571254

1258-
if( CV_IS_STORAGE( circle_storage ))
1255+
bool isStorage = isStorageOrMat(circle_storage);
1256+
1257+
if(isStorage)
12591258
{
12601259
circles = cvCreateSeq( CV_32FC3, sizeof(CvSeq),
12611260
sizeof(float)*3, (CvMemStorage*)circle_storage );
12621261
}
1263-
else if( CV_IS_MAT( circle_storage ))
1262+
else
12641263
{
12651264
mat = (CvMat*)circle_storage;
12661265

@@ -1274,8 +1273,6 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
12741273
circles_max = circles->total;
12751274
cvClearSeq( circles );
12761275
}
1277-
else
1278-
CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
12791276

12801277
switch( method )
12811278
{
@@ -1288,17 +1285,17 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
12881285
CV_Error( CV_StsBadArg, "Unrecognized method id" );
12891286
}
12901287

1291-
if( mat )
1288+
if (isStorage)
1289+
return circles;
1290+
else
12921291
{
12931292
if( mat->cols > mat->rows )
12941293
mat->cols = circles->total;
12951294
else
12961295
mat->rows = circles->total;
12971296
}
1298-
else
1299-
result = circles;
13001297

1301-
return result;
1298+
return 0;
13021299
}
13031300

13041301

modules/imgproc/src/precomp.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
/* helper tables */
7171
extern const uchar icvSaturate8u_cv[];
72-
#define CV_FAST_CAST_8U(t) (assert(-256 <= (t) && (t) <= 512), icvSaturate8u_cv[(t)+256])
72+
#define CV_FAST_CAST_8U(t) ( (-256 <= (t) && (t) <= 512) ? icvSaturate8u_cv[(t)+256] : 0 )
7373
#define CV_CALC_MIN_8U(a,b) (a) -= CV_FAST_CAST_8U((a) - (b))
7474
#define CV_CALC_MAX_8U(a,b) (a) += CV_FAST_CAST_8U((b) - (a))
7575

@@ -111,4 +111,15 @@ static inline IppiInterpolationType ippiGetInterpolation(int inter)
111111

112112
#include "opencv2/core/sse_utils.hpp"
113113

114+
inline bool isStorageOrMat(void * arr)
115+
{
116+
if (CV_IS_STORAGE( arr ))
117+
return true;
118+
else if (CV_IS_MAT( arr ))
119+
return false;
120+
else
121+
CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
122+
return false;
123+
}
124+
114125
#endif /*__OPENCV_CV_INTERNAL_H_*/

modules/imgproc/src/templmatch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
477477
integral(_image, image_sums, image_sqsums, CV_32F, CV_32F);
478478

479479
int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
480+
CV_Assert(cn >= 1 && cn <= 4);
480481

481482
ocl::Kernel k("matchTemplate_CCOEFF_NORMED", ocl::imgproc::match_template_oclsrc,
482483
format("-D CCOEFF_NORMED -D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));

modules/objdetect/src/haar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
18791879
sscanf( stage, "%d%n", &rects, &dl );
18801880
stage += dl;
18811881

1882-
CV_DbgAssert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
1882+
CV_Assert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
18831883

18841884
for( k = 0; k < rects; k++ )
18851885
{

modules/photo/src/calibrate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class CalibrateRobertsonImpl : public CalibrateRobertson
224224

225225
int channels = images[0].channels();
226226
int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
227+
CV_Assert(channels >= 1 && channels <= 3);
227228

228229
dst.create(LDR_SIZE, 1, CV_32FCC);
229230
Mat response = dst.getMat();

0 commit comments

Comments
 (0)