A bug in function Mat::total() (Patch #2297)


Added by Shiqi Yu over 12 years ago. Updated over 12 years ago.


Status:Done Start date:2012-08-24
Priority:Normal Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:core
Target version:2.4.3
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

The function Mat::total() returns the number of elements in the matrix. But if the number of elements = 2^32, total() will return 0. If total() returns 0, create() function can not alloc memory.

The bug is cause by 32-bits integer overflow. Since Mat::rows and Mat::cols are all int types, rows * cols can cause overflow when they are great enough.

total() function should be: ( size_t type conversion added).

inline size_t Mat::total() const
{
    if( dims <= 2 )
        return (size_t)rows*cols;
    size_t p = 1;
    for( int i = 0; i < dims; i++ )
        p *= size[i];
    return p;
}


mat.hpp.patch - the patch file for modules/core/include/opencv2/core/mat.hpp (76 Bytes) Shiqi Yu, 2012-08-24 08:55 am


Associated revisions

Revision 0bd68a70
Added by Vadim Pisarevsky over 12 years ago

fixed #2297, #2300; fixed several warnings

Revision d534d04f
Added by Roman Donchenko about 11 years ago

Merge pull request #2297 from asmorkalov:ocv_testdata_install_perms_fix

History

Updated by Vadim Pisarevsky over 12 years ago

thanks! the patch was applied in our git repository, 0bd68a7

  • Status changed from Open to Done
  • Target version set to 2.4.3

Also available in: Atom PDF