Issues found by static analysis (5th round)
authorMaksim Shabunin <maksim.shabunin@gmail.com>
Sat, 1 Jul 2017 15:43:48 +0000 (18:43 +0300)
committerMaksim Shabunin <maksim.shabunin@gmail.com>
Sat, 1 Jul 2017 15:56:24 +0000 (18:56 +0300)
modules/calib3d/src/calibinit.cpp
modules/core/src/datastructs.cpp
modules/core/src/opencl/runtime/opencl_core.cpp
modules/imgproc/src/contours.cpp
modules/imgproc/src/convhull.cpp
modules/imgproc/src/hough.cpp
modules/imgproc/src/precomp.hpp
modules/imgproc/src/templmatch.cpp
modules/objdetect/src/haar.cpp
modules/photo/src/calibrate.cpp

index 059612d..c93415c 100644 (file)
@@ -1836,7 +1836,9 @@ icvGenerateQuads( CvCBQuad **out_quads, CvCBCorner **out_corners,
         assert( src_contour->total == 4 );
         for( i = 0; i < 4; i++ )
         {
-            CvPoint2D32f pt = cvPointTo32f(*(CvPoint*)cvGetSeqElem(src_contour, i));
+            CvPoint * onePoint = (CvPoint*)cvGetSeqElem(src_contour, i);
+            CV_Assert(onePoint != NULL);
+            CvPoint2D32f pt = cvPointTo32f(*onePoint);
             CvCBCorner* corner = &(*out_corners)[quad_count*4 + i];
 
             memset( corner, 0, sizeof(*corner) );
index d69565f..56528fc 100644 (file)
@@ -2882,7 +2882,7 @@ cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx, CvGraphVtx* end_v
             break;
     }
 
-    assert( edge != 0 );
+    CV_Assert( edge != 0 );
 
     next_edge = edge->next[ofs];
     if( prev_edge )
index dc3d0a4..5c46611 100644 (file)
@@ -257,14 +257,14 @@ static void* opencl_check_fn(int ID)
     const struct DynamicFnEntry* e = NULL;
     if (ID < CUSTOM_FUNCTION_ID)
     {
-        assert(ID >= 0 && ID < (int)(sizeof(opencl_fn_list)/sizeof(opencl_fn_list[0])));
+        CV_Assert(ID >= 0 && ID < (int)(sizeof(opencl_fn_list)/sizeof(opencl_fn_list[0])));
         e = opencl_fn_list[ID];
     }
 #ifdef HAVE_OPENCL_SVM
     else if (ID >= SVM_FUNCTION_ID_START && ID < SVM_FUNCTION_ID_END)
     {
         ID = ID - SVM_FUNCTION_ID_START;
-        assert(ID >= 0 && ID < (int)(sizeof(opencl_svm_fn_list)/sizeof(opencl_svm_fn_list[0])));
+        CV_Assert(ID >= 0 && ID < (int)(sizeof(opencl_svm_fn_list)/sizeof(opencl_svm_fn_list[0])));
         e = opencl_svm_fn_list[ID];
     }
 #endif
index 77af291..ea56051 100644 (file)
@@ -1179,7 +1179,7 @@ cvFindNextContour( CvContourScanner scanner )
                         cur = cur->next;
                     }
 
-                    assert( par_info != 0 );
+                    CV_Assert( par_info != 0 );
 
                     /* if current contour is a hole and previous contour is a hole or
                        current contour is external and previous contour is external then
index c3287b8..e56d3cb 100644 (file)
@@ -404,9 +404,6 @@ CV_IMPL CvSeq*
 cvConvexHull2( const CvArr* array, void* hull_storage,
                int orientation, int return_points )
 {
-    union { CvContour* c; CvSeq* s; } hull;
-    hull.s = 0;
-
     CvMat* mat = 0;
     CvContour contour_header;
     CvSeq hull_header;
@@ -427,7 +424,9 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
         ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
     }
 
-    if( CV_IS_STORAGE( hull_storage ))
+    bool isStorage = isStorageOrMat(hull_storage);
+
+    if(isStorage)
     {
         if( return_points )
         {
@@ -445,9 +444,6 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
     }
     else
     {
-        if( !CV_IS_MAT( hull_storage ))
-            CV_Error(CV_StsBadArg, "Destination must be valid memory storage or matrix");
-
         mat = (CvMat*)hull_storage;
 
         if( (mat->cols != 1 && mat->rows != 1) || !CV_IS_MAT_CONT(mat->type))
@@ -473,10 +469,10 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
     int total = ptseq->total;
     if( total == 0 )
     {
-        if( mat )
+        if( !isStorage )
             CV_Error( CV_StsBadSize,
                      "Point sequence can not be empty if the output is matrix" );
-        return hull.s;
+        return 0;
     }
 
     cv::AutoBuffer<double> _ptbuf;
@@ -498,22 +494,18 @@ cvConvexHull2( const CvArr* array, void* hull_storage,
     else
         cvSeqPushMulti(hullseq, h0.ptr(), (int)h0.total());
 
-    if( mat )
+    if (isStorage)
+    {
+        return hullseq;
+    }
+    else
     {
         if( mat->rows > mat->cols )
             mat->rows = hullseq->total;
         else
             mat->cols = hullseq->total;
+        return 0;
     }
-    else
-    {
-        hull.s = hullseq;
-        hull.c->rect = cvBoundingRect( ptseq,
-                                       ptseq->header_size < (int)sizeof(CvContour) ||
-                                       &ptseq->flags == &contour_header.flags );
-    }
-
-    return hull.s;
 }
 
 
index 6850499..665e9a3 100644 (file)
@@ -894,7 +894,6 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
     cv::Mat image = cv::cvarrToMat(src_image);
     std::vector<cv::Vec2f> l2;
     std::vector<cv::Vec4i> l4;
-    CvSeq* result = 0;
 
     CvMat* mat = 0;
     CvSeq* lines = 0;
@@ -921,11 +920,13 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
         elemSize = sizeof(int)*4;
     }
 
-    if( CV_IS_STORAGE( lineStorage ))
+    bool isStorage = isStorageOrMat(lineStorage);
+
+    if( isStorage )
     {
         lines = cvCreateSeq( lineType, sizeof(CvSeq), elemSize, (CvMemStorage*)lineStorage );
     }
-    else if( CV_IS_MAT( lineStorage ))
+    else
     {
         mat = (CvMat*)lineStorage;
 
@@ -942,8 +943,6 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
         linesMax = lines->total;
         cvClearSeq( lines );
     }
-    else
-        CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
 
     iparam1 = cvRound(param1);
     iparam2 = cvRound(param2);
@@ -968,7 +967,7 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
 
     int nlines = (int)(l2.size() + l4.size());
 
-    if( mat )
+    if( !isStorage )
     {
         if( mat->cols > mat->rows )
             mat->cols = nlines;
@@ -981,20 +980,20 @@ cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
         cv::Mat lx = method == CV_HOUGH_STANDARD || method == CV_HOUGH_MULTI_SCALE ?
             cv::Mat(nlines, 1, CV_32FC2, &l2[0]) : cv::Mat(nlines, 1, CV_32SC4, &l4[0]);
 
-        if( mat )
+        if (isStorage)
         {
-            cv::Mat dst(nlines, 1, lx.type(), mat->data.ptr);
-            lx.copyTo(dst);
+            cvSeqPushMulti(lines, lx.ptr(), nlines);
         }
         else
         {
-            cvSeqPushMulti(lines, lx.ptr(), nlines);
+            cv::Mat dst(nlines, 1, lx.type(), mat->data.ptr);
+            lx.copyTo(dst);
         }
     }
 
-    if( !mat )
-        result = lines;
-    return result;
+    if( isStorage )
+        return lines;
+    return 0;
 }
 
 
@@ -1227,8 +1226,6 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
                 double param1, double param2,
                 int min_radius, int max_radius )
 {
-    CvSeq* result = 0;
-
     CvMat stub, *img = (CvMat*)src_image;
     CvMat* mat = 0;
     CvSeq* circles = 0;
@@ -1255,12 +1252,14 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
     else if( max_radius <= min_radius )
         max_radius = min_radius + 2;
 
-    if( CV_IS_STORAGE( circle_storage ))
+    bool isStorage = isStorageOrMat(circle_storage);
+
+    if(isStorage)
     {
         circles = cvCreateSeq( CV_32FC3, sizeof(CvSeq),
             sizeof(float)*3, (CvMemStorage*)circle_storage );
     }
-    else if( CV_IS_MAT( circle_storage ))
+    else
     {
         mat = (CvMat*)circle_storage;
 
@@ -1274,8 +1273,6 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
         circles_max = circles->total;
         cvClearSeq( circles );
     }
-    else
-        CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
 
     switch( method )
     {
@@ -1288,17 +1285,17 @@ cvHoughCircles( CvArr* src_image, void* circle_storage,
         CV_Error( CV_StsBadArg, "Unrecognized method id" );
     }
 
-    if( mat )
+    if (isStorage)
+        return circles;
+    else
     {
         if( mat->cols > mat->rows )
             mat->cols = circles->total;
         else
             mat->rows = circles->total;
     }
-    else
-        result = circles;
 
-    return result;
+    return 0;
 }
 
 
index 40598a0..55241d9 100644 (file)
@@ -69,7 +69,7 @@
 
 /* helper tables */
 extern const uchar icvSaturate8u_cv[];
-#define CV_FAST_CAST_8U(t)  (assert(-256 <= (t) && (t) <= 512), icvSaturate8u_cv[(t)+256])
+#define CV_FAST_CAST_8U(t)  ( (-256 <= (t) && (t) <= 512) ? icvSaturate8u_cv[(t)+256] : 0 )
 #define CV_CALC_MIN_8U(a,b) (a) -= CV_FAST_CAST_8U((a) - (b))
 #define CV_CALC_MAX_8U(a,b) (a) += CV_FAST_CAST_8U((b) - (a))
 
@@ -111,4 +111,15 @@ static inline IppiInterpolationType ippiGetInterpolation(int inter)
 
 #include "opencv2/core/sse_utils.hpp"
 
+inline bool isStorageOrMat(void * arr)
+{
+    if (CV_IS_STORAGE( arr ))
+        return true;
+    else if (CV_IS_MAT( arr ))
+        return false;
+    else
+        CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );
+    return false;
+}
+
 #endif /*__OPENCV_CV_INTERNAL_H_*/
index ce5c4d7..302e26e 100644 (file)
@@ -477,6 +477,7 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou
     integral(_image, image_sums, image_sqsums, CV_32F, CV_32F);
 
     int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
+    CV_Assert(cn >= 1 && cn <= 4);
 
     ocl::Kernel k("matchTemplate_CCOEFF_NORMED", ocl::imgproc::match_template_oclsrc,
         format("-D CCOEFF_NORMED -D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
index cb24479..6b1c8b8 100644 (file)
@@ -1879,7 +1879,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
                 sscanf( stage, "%d%n", &rects, &dl );
                 stage += dl;
 
-                CV_DbgAssert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
+                CV_Assert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
 
                 for( k = 0; k < rects; k++ )
                 {
index a0e07c0..8aa96f5 100644 (file)
@@ -224,6 +224,7 @@ public:
 
         int channels = images[0].channels();
         int CV_32FCC = CV_MAKETYPE(CV_32F, channels);
+        CV_Assert(channels >= 1 && channels <= 3);
 
         dst.create(LDR_SIZE, 1, CV_32FCC);
         Mat response = dst.getMat();