Ask Your Question
0

EXC_BAD_ACCESS (heap buffer overflow) when using .at function [closed]

asked 2018-02-12 06:15:47 -0500

Mengu gravatar image

hi all

i am using OpenCV 3.4.0 with C++ on Mac OS X. I am trying to access a matrice on a Mat instance like the following:

cv::Mat overlay2 = cv::imread(getAssetsPath() + "overlay.png");

for (int i = 0; i < overlay2.rows; ++i) {
    for (int j = 0; j < overlay2.cols; ++j) {
        std::cout << i << "x" << j << std::endl;
        auto vec = overlay2.at<cv::Vec4f>(i, j);
        std::cout << vec << std::endl;
    }
}

this is causing me a heap buffer overflow error"

READ of size 4 at 0x00010bed0800 thread T0
    #0 0x10003a203 in cv::Matx<float, 4, 1>::Matx(float const*) matx.hpp:665
    #1 0x10003a08b in cv::Vec<float, 4>::Vec(cv::Vec<float, 4> const&) matx.hpp:1030
    #2 0x10002aa22 in cv::Vec<float, 4>::Vec(cv::Vec<float, 4> const&) matx.hpp:1030
    #3 0x100028c6a in OpenCVImage::appendOverlay(OpenCVImage) Image.cpp:32
    #4 0x100067434 in testOverlay() main.cpp:45
    #5 0x10007762a in main main.cpp:136
    #6 0x7fff6b15a114 in start (libdyld.dylib:x86_64+0x1114)

0x00010bed0800 is located 0 bytes to the right of 7077888-byte region [0x00010b810800,0x00010bed0800)
allocated by thread T0 here:
    #0 0x1001da830 in wrap_posix_memalign (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x59830)
    #1 0x101388d20 in cv::fastMalloc(unsigned long) (libopencv_core.3.4.dylib:x86_64+0x2d20)
    #2 0x1014bbc56 in cv::StdMatAllocator::allocate(int, int const*, int, void*, unsigned long*, int, cv::UMatUsageFlags) const (libopencv_core.3.4.dylib:x86_64+0x135c56)
    #3 0x10148fece in cv::Mat::create(int, int const*, int) (libopencv_core.3.4.dylib:x86_64+0x109ece)
    #4 0x102135c5c in cv::imread_(cv::String const&, int, int, cv::Mat*) (libopencv_imgcodecs.3.4.dylib:x86_64+0x4c5c)
    #5 0x10213593f in cv::imread(cv::String const&, int) (libopencv_imgcodecs.3.4.dylib:x86_64+0x493f)
    #6 0x100028592 in OpenCVImage::appendOverlay(OpenCVImage) Image.cpp:27
    #7 0x100067434 in testOverlay() main.cpp:45
    #8 0x10007762a in main main.cpp:136
    #9 0x7fff6b15a114 in start (libdyld.dylib:x86_64+0x1114)

SUMMARY: AddressSanitizer: heap-buffer-overflow matx.hpp:665 in cv::Matx<float, 4, 1>::Matx(float const*)
Shadow bytes around the buggy address:
  0x1000217da0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000217da0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000217da0d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000217da0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000217da0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x1000217da100:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1000217da110: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1000217da120: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1000217da130: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1000217da140: fa fa fa fa fa fa fa fa fa fa fa fa fa fa ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by LBerger
close date 2018-02-12 08:56:13.128311

Comments

it is not Vec4f but Vec4b if overlay2.channels() is equal to 4

LBerger gravatar imageLBerger ( 2018-02-12 07:06:56 -0500 )edit

@LBerger it actually did not make any difference. i used Vec4f because of 4 channels and to get float values. would my image somehow be corrupt?

Mengu gravatar imageMengu ( 2018-02-12 07:23:34 -0500 )edit

thank you very much @LBerger. i don't know why but cv::imread was setting my png channels to 1 rather than 4. hence i was getting those errors.

Mengu gravatar imageMengu ( 2018-02-12 08:53:23 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-02-12 07:32:44 -0500

LBerger gravatar image

You cannot use a Vec4f if data are uchar you will get an exception.

You have to check that overlay2 is read :

if (overlay2.empty())
{
     cout<< "Image is empty. Check file path";
     return;
}
cout<< "Channels "<<overlay2.channels()<<endl;
if (channels!=4)
{
     cout<< "should be 4 and not"<<. overlay2.channels()<<endl;
     return;
}

if my memory png can be 8 bits or 16 bits It can be Vec4b, Vec4s or Vec4w but not Vec4f Vec4f means 128 bits per pixel

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-12 06:15:47 -0500

Seen: 47 times

Last updated: Feb 12