From a769d69a9da88b9eb07c7adcbff69a4faf74a314 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Wed, 28 Jun 2017 16:26:55 +0300 Subject: [PATCH] Fixed several issues found by static analysis --- modules/calib3d/src/fisheye.cpp | 3 ++- modules/core/src/command_line_parser.cpp | 2 +- modules/core/src/copy.cpp | 3 ++- modules/core/src/datastructs.cpp | 1 + modules/core/src/matrix.cpp | 2 +- modules/core/src/persistence.cpp | 3 ++- modules/core/src/system.cpp | 11 ++++++---- modules/core/src/trace.cpp | 9 ++++++++ modules/dnn/src/dnn.cpp | 4 ++-- modules/dnn/src/layers/convolution_layer.cpp | 11 ++++++++-- modules/dnn/src/layers/elementwise_layers.cpp | 2 +- modules/dnn/src/layers/permute_layer.cpp | 2 +- modules/dnn/src/layers/prior_box_layer.cpp | 1 + modules/dnn/src/layers/recurrent_layers.cpp | 2 ++ modules/dnn/src/torch/THDiskFile.cpp | 2 ++ modules/dnn/src/torch/torch_importer.cpp | 2 +- modules/features2d/src/brisk.cpp | 1 + .../include/opencv2/flann/flann_base.hpp | 2 ++ modules/highgui/src/window_gtk.cpp | 4 ++++ modules/imgcodecs/src/bitstrm.cpp | 1 + modules/imgcodecs/src/grfmt_exr.cpp | 10 ++++----- modules/imgcodecs/src/grfmt_jpeg.cpp | 2 +- modules/imgproc/src/contours.cpp | 20 ++++++++++------- modules/imgproc/src/emd.cpp | 1 + modules/imgproc/src/filter.cpp | 22 +++++++------------ modules/imgproc/src/lsd.cpp | 3 +++ modules/imgproc/src/smooth.cpp | 2 ++ modules/imgproc/src/templmatch.cpp | 2 ++ modules/ml/src/tree.cpp | 1 + modules/objdetect/src/cascadedetect.cpp | 11 +++++----- modules/shape/src/emdL1.cpp | 2 ++ modules/superres/src/btv_l1.cpp | 22 ++++++++++--------- modules/superres/src/input_array_utility.cpp | 7 +++--- modules/video/src/lkpyramid.cpp | 1 + modules/videoio/src/cap_images.cpp | 5 +++-- 35 files changed, 113 insertions(+), 66 deletions(-) diff --git a/modules/calib3d/src/fisheye.cpp b/modules/calib3d/src/fisheye.cpp index f371a615aa..05bd37b74f 100644 --- a/modules/calib3d/src/fisheye.cpp +++ b/modules/calib3d/src/fisheye.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "fisheye.hpp" +#include namespace cv { namespace { @@ -760,7 +761,7 @@ double cv::fisheye::calibrate(InputArrayOfArrays objectPoints, InputArrayOfArray //-------------------------------Optimization - for(int iter = 0; ; ++iter) + for(int iter = 0; iter <= std::numeric_limits::max(); ++iter) { if ((criteria.type == 1 && iter >= criteria.maxCount) || (criteria.type == 2 && change <= criteria.epsilon) || diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index 4fc62238b8..2b67ce825c 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -276,10 +276,10 @@ CommandLineParser& CommandLineParser::operator = (const CommandLineParser& parse { if( this != &parser ) { + CV_XADD(&parser.impl->refcount, 1); if(CV_XADD(&impl->refcount, -1) == 1) delete impl; impl = parser.impl; - CV_XADD(&impl->refcount, 1); } return *this; } diff --git a/modules/core/src/copy.cpp b/modules/core/src/copy.cpp index 3494d002f2..ca34e7f297 100644 --- a/modules/core/src/copy.cpp +++ b/modules/core/src/copy.cpp @@ -283,8 +283,9 @@ void Mat::copyTo( OutputArray _dst ) const } _dst.create( dims, size.p, type() ); UMat dst = _dst.getUMat(); - + CV_Assert(dst.u != NULL); size_t i, sz[CV_MAX_DIM] = {0}, dstofs[CV_MAX_DIM], esz = elemSize(); + CV_Assert(dims >= 0 && dims < CV_MAX_DIM); for( i = 0; i < (size_t)dims; i++ ) sz[i] = size.p[i]; sz[dims-1] *= esz; diff --git a/modules/core/src/datastructs.cpp b/modules/core/src/datastructs.cpp index 2612de6c5d..d69565f4c2 100644 --- a/modules/core/src/datastructs.cpp +++ b/modules/core/src/datastructs.cpp @@ -2547,6 +2547,7 @@ cvSetAdd( CvSet* set, CvSetElem* element, CvSetElem** inserted_element ) CV_IMPL void cvSetRemove( CvSet* set, int index ) { + CV_Assert(set != NULL); CvSetElem* elem = cvGetSetElem( set, index ); if( elem ) cvSetRemoveByPtr( set, elem ); diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 724d54e55b..09a074b88c 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -1129,7 +1129,7 @@ Mat Mat::diag(const Mat& d) int Mat::checkVector(int _elemChannels, int _depth, bool _requireContinuous) const { - return (depth() == _depth || _depth <= 0) && + return data && (depth() == _depth || _depth <= 0) && (isContinuous() || !_requireContinuous) && ((dims == 2 && (((rows == 1 || cols == 1) && channels() == _elemChannels) || (cols == _elemChannels && channels() == 1))) || diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 6c947e2bfc..ca4615e7de 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -804,7 +804,7 @@ cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node, if( !map_node ) map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k ); - + CV_Assert(map_node != NULL); if( !CV_NODE_IS_MAP(map_node->tag) ) { if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) && @@ -6777,6 +6777,7 @@ cvLoad( const char* filename, CvMemStorage* memstorage, CvSeqReader reader; node = (CvFileNode*)cvGetSeqElem( (*fs)->roots, k ); + CV_Assert(node != NULL); if( !CV_NODE_IS_MAP( node->tag )) return 0; seq = node->data.seq; diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index db653abf11..509389443b 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1193,10 +1193,13 @@ Mutex::Mutex(const Mutex& m) Mutex& Mutex::operator = (const Mutex& m) { - CV_XADD(&m.impl->refcount, 1); - if( CV_XADD(&impl->refcount, -1) == 1 ) - delete impl; - impl = m.impl; + if (this != &m) + { + CV_XADD(&m.impl->refcount, 1); + if( CV_XADD(&impl->refcount, -1) == 1 ) + delete impl; + impl = m.impl; + } return *this; } diff --git a/modules/core/src/trace.cpp b/modules/core/src/trace.cpp index 6a71161455..d9153642d7 100644 --- a/modules/core/src/trace.cpp +++ b/modules/core/src/trace.cpp @@ -225,6 +225,11 @@ Region::LocationExtraData::LocationExtraData(const LocationStaticStorage& locati ittHandle_name = __itt_string_handle_create(location.name); ittHandle_filename = __itt_string_handle_create(location.filename); } + else + { + ittHandle_name = 0; + ittHandle_filename = 0; + } #endif } @@ -1019,6 +1024,10 @@ struct TraceArg::ExtraData // Consecutive calls to __itt_string_handle_create with the same name return the same value. ittHandle_name = __itt_string_handle_create(arg.name); } + else + { + ittHandle_name = 0; + } #endif } }; diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 236d089965..41951569f6 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -317,9 +317,9 @@ private: struct LayerData { - LayerData() {} + LayerData() : id(-1), flag(0) {} LayerData(int _id, const String &_name, const String &_type, LayerParams &_params) - : id(_id), name(_name), type(_type), params(_params) + : id(_id), name(_name), type(_type), params(_params), flag(0) { //add logging info params.name = name; diff --git a/modules/dnn/src/layers/convolution_layer.cpp b/modules/dnn/src/layers/convolution_layer.cpp index edda734ea6..5d632098e4 100644 --- a/modules/dnn/src/layers/convolution_layer.cpp +++ b/modules/dnn/src/layers/convolution_layer.cpp @@ -287,7 +287,10 @@ public: bool is1x1_; bool useAVX2; - ParallelConv() {} + ParallelConv() + : input_(0), weights_(0), output_(0), ngroups_(0), nstripes_(0), + is1x1_(false), useAVX2(false) + {} static void run( const Mat& input, Mat& output, const Mat& weights, const std::vector& biasvec, @@ -921,7 +924,11 @@ public: int nstripes; bool is1x1; - Col2ImInvoker() {} + Col2ImInvoker() + : data_col(0), biasvec(0), channels(0), height(0), width(0), + kernel_h(0), kernel_w(0), pad_h(0), pad_w(0), stride_h(0), stride_w(0), data_im(0), + height_col(0), width_col(0), nstripes(0), is1x1(0) + {} static void run(const float* data_col, int channels, int height, int width, diff --git a/modules/dnn/src/layers/elementwise_layers.cpp b/modules/dnn/src/layers/elementwise_layers.cpp index ebf9c70e6b..a706545df5 100644 --- a/modules/dnn/src/layers/elementwise_layers.cpp +++ b/modules/dnn/src/layers/elementwise_layers.cpp @@ -105,7 +105,7 @@ public: } }; - ElementWiseLayer(const Func &f=Func()) { func = f; } + ElementWiseLayer(const Func &f=Func()) : run_parallel(false) { func = f; } virtual bool supportBackend(int backendId) { diff --git a/modules/dnn/src/layers/permute_layer.cpp b/modules/dnn/src/layers/permute_layer.cpp index 7555369e69..75df87bcf5 100644 --- a/modules/dnn/src/layers/permute_layer.cpp +++ b/modules/dnn/src/layers/permute_layer.cpp @@ -83,10 +83,10 @@ public: } PermuteLayerImpl(const LayerParams ¶ms) + : _count(0), _needsPermute(false), _numAxes(0) { if (!params.has("order")) { - _needsPermute = false; return; } diff --git a/modules/dnn/src/layers/prior_box_layer.cpp b/modules/dnn/src/layers/prior_box_layer.cpp index 6ded76bae3..4faa005c63 100644 --- a/modules/dnn/src/layers/prior_box_layer.cpp +++ b/modules/dnn/src/layers/prior_box_layer.cpp @@ -160,6 +160,7 @@ public: } PriorBoxLayerImpl(const LayerParams ¶ms) + : _boxWidth(0), _boxHeight(0) { setParamsFrom(params); _minSize = getParameter(params, "min_size"); diff --git a/modules/dnn/src/layers/recurrent_layers.cpp b/modules/dnn/src/layers/recurrent_layers.cpp index 08d761013f..a05d20dce3 100644 --- a/modules/dnn/src/layers/recurrent_layers.cpp +++ b/modules/dnn/src/layers/recurrent_layers.cpp @@ -94,6 +94,7 @@ class LSTMLayerImpl : public LSTMLayer public: LSTMLayerImpl(const LayerParams& params) + : numTimeStamps(0), numSamples(0) { setParamsFrom(params); type = "LSTM"; @@ -307,6 +308,7 @@ class RNNLayerImpl : public RNNLayer public: RNNLayerImpl(const LayerParams& params) + : numX(0), numH(0), numO(0), numSamples(0), numTimestamps(0), numSamplesTotal(0), dtype(0) { setParamsFrom(params); type = "RNN"; diff --git a/modules/dnn/src/torch/THDiskFile.cpp b/modules/dnn/src/torch/THDiskFile.cpp index e7bc5d7ace..5ba064b921 100644 --- a/modules/dnn/src/torch/THDiskFile.cpp +++ b/modules/dnn/src/torch/THDiskFile.cpp @@ -556,6 +556,8 @@ static long THDiskFile_readString(THFile *self, const char *format, char **str_) total += TBRS_BSZ; p = (char*)THRealloc(p, total); } + if (p == NULL) + THError("read error: failed to allocate buffer"); if (fgets(p+pos, total-pos, dfself->handle) == NULL) /* eof? */ { if(pos == 0L) diff --git a/modules/dnn/src/torch/torch_importer.cpp b/modules/dnn/src/torch/torch_importer.cpp index 02e936d314..98c37dbde0 100644 --- a/modules/dnn/src/torch/torch_importer.cpp +++ b/modules/dnn/src/torch/torch_importer.cpp @@ -876,7 +876,7 @@ struct TorchImporter : public ::cv::dnn::Importer return mergeId; } else if (module->thName == "ConcatTable") { - int newId, splitId; + int newId = -1, splitId; LayerParams splitParams; splitId = net.addLayer(generateLayerName("torchSplit"), "Split", splitParams); diff --git a/modules/features2d/src/brisk.cpp b/modules/features2d/src/brisk.cpp index 982987a1cc..389650e809 100644 --- a/modules/features2d/src/brisk.cpp +++ b/modules/features2d/src/brisk.cpp @@ -720,6 +720,7 @@ BRISK_Impl::computeDescriptorsAndOrOrientation(InputArray _image, InputArray _ma const BriskLongPair* max = longPairs_ + noLongPairs_; for (BriskLongPair* iter = longPairs_; iter < max; ++iter) { + CV_Assert(iter->i < points_ && iter->j < points_); t1 = *(_values + iter->i); t2 = *(_values + iter->j); const int delta_t = (t1 - t2); diff --git a/modules/flann/include/opencv2/flann/flann_base.hpp b/modules/flann/include/opencv2/flann/flann_base.hpp index 98901afec1..be5e07f080 100644 --- a/modules/flann/include/opencv2/flann/flann_base.hpp +++ b/modules/flann/include/opencv2/flann/flann_base.hpp @@ -80,9 +80,11 @@ NNIndex* load_saved_index(const Matrix } IndexHeader header = load_header(fin); if (header.data_type != Datatype::type()) { + fclose(fin); throw FLANNException("Datatype of saved index is different than of the one to be created."); } if ((size_t(header.rows) != dataset.rows)||(size_t(header.cols) != dataset.cols)) { + fclose(fin); throw FLANNException("The index saved belongs to a different dataset"); } diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index 6e4da8b320..e97ea2fb9c 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -1617,6 +1617,10 @@ CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_nam if( pos > trackbar->maxval ) pos = trackbar->maxval; } + else + { + CV_ERROR( CV_StsNullPtr, "No trackbar found" ); + } CV_LOCK_MUTEX(); diff --git a/modules/imgcodecs/src/bitstrm.cpp b/modules/imgcodecs/src/bitstrm.cpp index c47744bb30..fdcbe75fdf 100644 --- a/modules/imgcodecs/src/bitstrm.cpp +++ b/modules/imgcodecs/src/bitstrm.cpp @@ -76,6 +76,7 @@ RBaseStream::RBaseStream() { m_start = m_end = m_current = 0; m_file = 0; + m_block_pos = 0; m_block_size = BS_DEF_BLOCK_SIZE; m_is_opened = false; m_allocated = false; diff --git a/modules/imgcodecs/src/grfmt_exr.cpp b/modules/imgcodecs/src/grfmt_exr.cpp index 877197d0be..0d2ae9fa7d 100644 --- a/modules/imgcodecs/src/grfmt_exr.cpp +++ b/modules/imgcodecs/src/grfmt_exr.cpp @@ -208,9 +208,12 @@ bool ExrDecoder::readData( Mat& img ) xstep = m_native_depth ? 4 : 1; + AutoBuffer copy_buffer; + if( !m_native_depth || (!color && m_iscolor )) { - buffer = (char *)new float[ m_width * 3 ]; + copy_buffer.allocate(sizeof(float) * m_width * 3); + buffer = copy_buffer; ystep = 0; } else @@ -388,11 +391,6 @@ bool ExrDecoder::readData( Mat& img ) close(); - if( !m_native_depth || (!color && m_iscolor )) - { - delete[] buffer; - } - return result; } diff --git a/modules/imgcodecs/src/grfmt_jpeg.cpp b/modules/imgcodecs/src/grfmt_jpeg.cpp index d83682889a..71ab0131a3 100644 --- a/modules/imgcodecs/src/grfmt_jpeg.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg.cpp @@ -364,7 +364,7 @@ int my_jpeg_load_dht (struct jpeg_decompress_struct *info, unsigned char *dht, if (index & 0x10) { - index -= 0x10; + index &= ~0x10; hufftbl = &ac_tables[index]; } else diff --git a/modules/imgproc/src/contours.cpp b/modules/imgproc/src/contours.cpp index dc8905cdf9..e05d1a935a 100644 --- a/modules/imgproc/src/contours.cpp +++ b/modules/imgproc/src/contours.cpp @@ -525,7 +525,7 @@ icvFetchContour( schar *ptr, int prev_s = -1, s, s_end; int method = _method - 1; - assert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); + CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); /* initialize local state */ CV_INIT_3X3_DELTAS( deltas, step, 1 ); @@ -557,6 +557,7 @@ icvFetchContour( schar *ptr, else { i3 = i0; + CV_Assert(i3 != NULL); prev_s = s ^ 4; /* follow border */ @@ -568,6 +569,7 @@ icvFetchContour( schar *ptr, while( s < MAX_SIZE - 1 ) { i4 = i3 + deltas[++s]; + CV_Assert(i4 != NULL); if( *i4 != 0 ) break; } @@ -615,7 +617,7 @@ icvFetchContour( schar *ptr, if( _method != CV_CHAIN_CODE ) cvBoundingRect( contour, 1 ); - assert( (writer.seq->total == 0 && writer.seq->first == 0) || + CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) || writer.seq->total > writer.seq->first->count || (writer.seq->first->prev == writer.seq->first && writer.seq->first->next == writer.seq->first) ); @@ -638,7 +640,7 @@ icvTraceContour( schar *ptr, int step, schar *stop_ptr, int is_hole ) CV_INIT_3X3_DELTAS( deltas, step, 1 ); memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] )); - assert( (*i0 & -2) != 0 ); + CV_DbgAssert( (*i0 & -2) != 0 ); s_end = s = is_hole ? 0 : 4; @@ -650,6 +652,7 @@ icvTraceContour( schar *ptr, int step, schar *stop_ptr, int is_hole ) while( *i1 == 0 && s != s_end ); i3 = i0; + CV_Assert(i3 != NULL); /* check single pixel domain */ if( s != s_end ) @@ -662,6 +665,7 @@ icvTraceContour( schar *ptr, int step, schar *stop_ptr, int is_hole ) while( s < MAX_SIZE - 1 ) { i4 = i3 + deltas[++s]; + CV_Assert(i4 != NULL); if( *i4 != 0 ) break; } @@ -693,8 +697,8 @@ icvFetchContourEx( schar* ptr, int prev_s = -1, s, s_end; int method = _method - 1; - assert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); - assert( 1 < nbd && nbd < 128 ); + CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); + CV_DbgAssert( 1 < nbd && nbd < 128 ); /* initialize local state */ CV_INIT_3X3_DELTAS( deltas, step, 1 ); @@ -799,7 +803,7 @@ icvFetchContourEx( schar* ptr, if( _method != CV_CHAIN_CODE ) ((CvContour*)contour)->rect = rect; - assert( (writer.seq->total == 0 && writer.seq->first == 0) || + CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) || writer.seq->total > writer.seq->first->count || (writer.seq->first->prev == writer.seq->first && writer.seq->first->next == writer.seq->first) ); @@ -882,7 +886,7 @@ icvFetchContourEx_32s( int* ptr, const int nbd0 = ccomp_val | new_flag; const int nbd1 = nbd0 | right_flag; - assert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); + CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE ); /* initialize local state */ CV_INIT_3X3_DELTAS( deltas, step, 1 ); @@ -984,7 +988,7 @@ icvFetchContourEx_32s( int* ptr, if( _method != CV_CHAIN_CODE ) ((CvContour*)contour)->rect = rect; - assert( (writer.seq->total == 0 && writer.seq->first == 0) || + CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) || writer.seq->total > writer.seq->first->count || (writer.seq->first->prev == writer.seq->first && writer.seq->first->next == writer.seq->first) ); diff --git a/modules/imgproc/src/emd.cpp b/modules/imgproc/src/emd.cpp index 7b62c16c6f..e5bc695729 100644 --- a/modules/imgproc/src/emd.cpp +++ b/modules/imgproc/src/emd.cpp @@ -773,6 +773,7 @@ icvNewSolution( CvEMDState * state ) } /* remove the leaving basic variable */ + CV_Assert(leave_x != NULL); i = leave_x->i; j = leave_x->j; state->is_x[i][j] = 0; diff --git a/modules/imgproc/src/filter.cpp b/modules/imgproc/src/filter.cpp index 5f25f428b4..0031cdd48e 100644 --- a/modules/imgproc/src/filter.cpp +++ b/modules/imgproc/src/filter.cpp @@ -70,16 +70,10 @@ BaseFilter::~BaseFilter() {} void BaseFilter::reset() {} FilterEngine::FilterEngine() + : srcType(-1), dstType(-1), bufType(-1), maxWidth(0), wholeSize(-1, -1), dx1(0), dx2(0), + rowBorderType(BORDER_REPLICATE), columnBorderType(BORDER_REPLICATE), + borderElemSize(0), bufStep(0), startY(0), startY0(0), endY(0), rowCount(0), dstY(0) { - srcType = dstType = bufType = -1; - rowBorderType = columnBorderType = BORDER_REPLICATE; - bufStep = startY = startY0 = endY = rowCount = dstY = 0; - maxWidth = 0; - - wholeSize = Size(-1,-1); - dx1 = 0; - borderElemSize = 0; - dx2 = 0; } @@ -89,12 +83,10 @@ FilterEngine::FilterEngine( const Ptr& _filter2D, int _srcType, int _dstType, int _bufType, int _rowBorderType, int _columnBorderType, const Scalar& _borderValue ) + : srcType(-1), dstType(-1), bufType(-1), maxWidth(0), wholeSize(-1, -1), dx1(0), dx2(0), + rowBorderType(BORDER_REPLICATE), columnBorderType(BORDER_REPLICATE), + borderElemSize(0), bufStep(0), startY(0), startY0(0), endY(0), rowCount(0), dstY(0) { - startY0 = 0; - endY = 0; - dstY = 0; - dx2 = 0; - rowCount = 0; init(_filter2D, _rowFilter, _columnFilter, _srcType, _dstType, _bufType, _rowBorderType, _columnBorderType, _borderValue); } @@ -194,6 +186,7 @@ int FilterEngine::start(const Size &_wholeSize, const Size &sz, const Point &ofs srcRow.resize(esz*(maxWidth + ksize.width - 1)); if( columnBorderType == BORDER_CONSTANT ) { + CV_Assert(constVal != NULL); constBorderRow.resize(getElemSize(bufType)*(maxWidth + ksize.width - 1 + VEC_ALIGN)); uchar *dst = alignPtr(&constBorderRow[0], VEC_ALIGN), *tdst; int n = (int)constBorderValue.size(), N; @@ -227,6 +220,7 @@ int FilterEngine::start(const Size &_wholeSize, const Size &sz, const Point &ofs { if( rowBorderType == BORDER_CONSTANT ) { + CV_Assert(constVal != NULL); int nr = isSeparable() ? 1 : (int)rows.size(); for( i = 0; i < nr; i++ ) { diff --git a/modules/imgproc/src/lsd.cpp b/modules/imgproc/src/lsd.cpp index 25da2b46d1..a8ea384ab8 100644 --- a/modules/imgproc/src/lsd.cpp +++ b/modules/imgproc/src/lsd.cpp @@ -1000,6 +1000,7 @@ double LineSegmentDetectorImpl::rect_nfa(const rect& rec) const } } } + CV_Assert(leftmost != NULL); leftmost->taken = true; // Find rightmost untaken point; @@ -1018,6 +1019,7 @@ double LineSegmentDetectorImpl::rect_nfa(const rect& rec) const } } } + CV_Assert(rightmost != NULL); rightmost->taken = true; // Find last untaken point; @@ -1036,6 +1038,7 @@ double LineSegmentDetectorImpl::rect_nfa(const rect& rec) const } } } + CV_Assert(tailp != NULL); tailp->taken = true; double flstep = (min_y->p.y != leftmost->p.y) ? diff --git a/modules/imgproc/src/smooth.cpp b/modules/imgproc/src/smooth.cpp index f4510a78bd..4b4d510f42 100644 --- a/modules/imgproc/src/smooth.cpp +++ b/modules/imgproc/src/smooth.cpp @@ -2507,6 +2507,7 @@ medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize ) h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2; + CV_Assert(cn > 0 && cn <= 4); size_t sstep = _src.step, dstep = _dst.step; Histogram CV_DECL_ALIGNED(16) H[4]; HT CV_DECL_ALIGNED(16) luc[4][16]; @@ -2712,6 +2713,7 @@ medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m ) int src_step = (int)_src.step, dst_step = (int)_dst.step; int cn = _src.channels(); const uchar* src_max = src + size.height*src_step; + CV_Assert(cn > 0 && cn <= 4); #define UPDATE_ACC01( pix, cn, op ) \ { \ diff --git a/modules/imgproc/src/templmatch.cpp b/modules/imgproc/src/templmatch.cpp index 2dc4910946..ce5c4d74cc 100644 --- a/modules/imgproc/src/templmatch.cpp +++ b/modules/imgproc/src/templmatch.cpp @@ -888,12 +888,14 @@ static void common_matchTemplate( Mat& img, Mat& templ, Mat& result, int method, templNorm = std::sqrt(templNorm); templNorm /= std::sqrt(invArea); // care of accuracy here + CV_Assert(sqsum.data != NULL); q0 = (double*)sqsum.data; q1 = q0 + templ.cols*cn; q2 = (double*)(sqsum.data + templ.rows*sqsum.step); q3 = q2 + templ.cols*cn; } + CV_Assert(sum.data != NULL); double* p0 = (double*)sum.data; double* p1 = p0 + templ.cols*cn; double* p2 = (double*)(sum.data + templ.rows*sum.step); diff --git a/modules/ml/src/tree.cpp b/modules/ml/src/tree.cpp index 59e100cda8..4faf9b3b5f 100644 --- a/modules/ml/src/tree.cpp +++ b/modules/ml/src/tree.cpp @@ -1442,6 +1442,7 @@ float DTreesImpl::predictTrees( const Range& range, const Mat& sample, int flags CV_Error( CV_StsBadArg, "one of input categorical variable is not an integer" ); + CV_Assert(cmap != NULL); while( a < b ) { c = (a + b) >> 1; diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 8bd5193ebd..76011bd458 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -316,7 +316,7 @@ protected: } }; //new grouping function with using meanshift -static void groupRectangles_meanshift(std::vector& rectList, double detectThreshold, std::vector* foundWeights, +static void groupRectangles_meanshift(std::vector& rectList, double detectThreshold, std::vector& foundWeights, std::vector& scales, Size winDetSize) { int detectionCount = (int)rectList.size(); @@ -326,14 +326,13 @@ static void groupRectangles_meanshift(std::vector& rectList, double detect for (int i=0; i < detectionCount; i++) { - hitWeights[i] = (*foundWeights)[i]; + hitWeights[i] = foundWeights[i]; hitCenter = (rectList[i].tl() + rectList[i].br())*(0.5); //center of rectangles hits[i] = Point3d(hitCenter.x, hitCenter.y, std::log(scales[i])); } rectList.clear(); - if (foundWeights) - foundWeights->clear(); + foundWeights.clear(); double logZ = std::log(1.3); Point3d smothing(8, 16, logZ); @@ -355,7 +354,7 @@ static void groupRectangles_meanshift(std::vector& rectList, double detect if (resultWeights[i] > detectThreshold) { rectList.push_back(resultRect); - foundWeights->push_back(resultWeights[i]); + foundWeights.push_back(resultWeights[i]); } } } @@ -387,7 +386,7 @@ void groupRectangles_meanshift(std::vector& rectList, std::vector& { CV_INSTRUMENT_REGION() - groupRectangles_meanshift(rectList, detectThreshold, &foundWeights, foundScales, winDetSize); + groupRectangles_meanshift(rectList, detectThreshold, foundWeights, foundScales, winDetSize); } diff --git a/modules/shape/src/emdL1.cpp b/modules/shape/src/emdL1.cpp index 7baa97f0b9..cfb71ff126 100644 --- a/modules/shape/src/emdL1.cpp +++ b/modules/shape/src/emdL1.cpp @@ -494,6 +494,7 @@ void EmdL1::initBVTree() } if(pNxtN != pCurN->pParent) { + CV_Assert(pNxtN != NULL); pNxtE = pNxtN->pChild; if(pNxtE && pNxtE->pChild==pCurN) // has connection { @@ -592,6 +593,7 @@ void EmdL1::findNewSolution() findLoopFromEnterBV(); // Modify flow values along the loop cvPEmdEdge pE = NULL; + CV_Assert(m_pLeave != NULL); float minFlow = m_pLeave->flow; int k; for(k=0; k& btvWeights); - static const func_t funcs[] = + CV_UNUSED(ubtvWeights); + if (_src.channels() == 1) { - 0, calcBtvRegularizationImpl, 0, calcBtvRegularizationImpl, 0 - }; - - const func_t func = funcs[_src.channels()]; - CV_Assert(func != 0); - func(_src, _dst, btvKernelSize, btvWeights); + calcBtvRegularizationImpl(_src, _dst, btvKernelSize, btvWeights); + } + else if (_src.channels() == 3) + { + calcBtvRegularizationImpl(_src, _dst, btvKernelSize, btvWeights); + } + else + { + CV_Error(Error::StsBadArg, "Unsupported number of channels in _src"); + } } class BTVL1_Base : public cv::superres::SuperResolution diff --git a/modules/superres/src/input_array_utility.cpp b/modules/superres/src/input_array_utility.cpp index b3ea4bb914..b73993bf7d 100644 --- a/modules/superres/src/input_array_utility.cpp +++ b/modules/superres/src/input_array_utility.cpp @@ -200,10 +200,11 @@ namespace void convertToDepth(InputArray src, OutputArray dst, int depth) { - CV_Assert( src.depth() <= CV_64F ); + const int sdepth = src.depth(); + CV_Assert( sdepth <= CV_64F ); CV_Assert( depth == CV_8U || depth == CV_32F ); - static const double maxVals[] = + static const double maxVals[CV_64F + 1] = { (double)std::numeric_limits::max(), (double)std::numeric_limits::max(), @@ -214,7 +215,7 @@ namespace 1.0, }; - const double scale = maxVals[depth] / maxVals[src.depth()]; + const double scale = maxVals[depth] / maxVals[sdepth]; switch (src.kind()) { diff --git a/modules/video/src/lkpyramid.cpp b/modules/video/src/lkpyramid.cpp index ca591a8fcc..e34803bfb6 100644 --- a/modules/video/src/lkpyramid.cpp +++ b/modules/video/src/lkpyramid.cpp @@ -654,6 +654,7 @@ void cv::detail::LKTrackerInvoker::operator()(const Range& range) const prevDelta = delta; } + CV_Assert(status != NULL); if( status[ptidx] && err && level == 0 && (flags & OPTFLOW_LK_GET_MIN_EIGENVALS) == 0 ) { Point2f nextPoint = nextPts[ptidx] - halfWin; diff --git a/modules/videoio/src/cap_images.cpp b/modules/videoio/src/cap_images.cpp index f87c5544d2..9eac7db84d 100644 --- a/modules/videoio/src/cap_images.cpp +++ b/modules/videoio/src/cap_images.cpp @@ -209,7 +209,7 @@ static char* icvExtractPattern(const char *filename, unsigned *offset) char *at = strchr(name, '%'); if(at) { - int dummy; + unsigned int dummy; if(sscanf(at + 1, "%ud", &dummy) != 1) return 0; name = strdup(filename); @@ -236,6 +236,7 @@ static char* icvExtractPattern(const char *filename, unsigned *offset) int size = (int)strlen(filename) + 20; name = (char *)malloc(size); + CV_Assert(name != NULL); strncpy(name, filename, at - filename); name[at - filename] = 0; @@ -245,7 +246,7 @@ static char* icvExtractPattern(const char *filename, unsigned *offset) char *extension; for(i = 0, extension = at; isdigit(at[i]); i++, extension++) ; - char places[10]; + char places[13] = {0}; sprintf(places, "%dd", i); strcat(name, places);