ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001d4 at pc 0x0000003a3e3e bp 0x7fffdafb66c0 sp 0x7fffdafb66b8

  c++, c++11

I am getting this error when i am trying to use erase and insert methods of vector.

int bsearch(vector<int>v,int st,int ed,int n){
        while(ed>=st){
                    int mid = (ed+st)/2;
                    if(v[mid]==n){
                            return mid;
                    }else if(v[mid]>n){
                            ed=mid-1;
                    }else{
                            st=mid+1;
                    }
        }
 return 10;       
}
vector<int> processQueries(vector<int>& q, int m) {
        vector<int> v;
        vector<int> res;
        for(int i=0;i<m;i++){
                v.emplace_back(i+1);
        }
        for(int i=0;i<q.size();i++){
                int k = bsearch(v,0,m-1,q[i]);
                cout<<k<<endl;
                res.push_back(k);
                int temp=v[k];
                v.erase(v.begin());
               // v.insert(v.begin(),temp);
        }
        return res;
}


=================================================================

==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000001d4 at pc 0x0000003a3e3e bp 0x7fffdafb66c0 sp 0x7fffdafb66b8
READ of size 4 at 0x6020000001d4 thread T0
#4 0x7fe5cbd550b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000001d4 is located 0 bytes to the right of 4-byte region [0x6020000001d0,0x6020000001d4)
allocated by thread T0 here:
#10 0x7fe5cbd550b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:

0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
0x0c047fff8010: fa fa 00 00 fa fa fd fa fa fa fd fa fa fa 00 00
0x0c047fff8020: fa fa fd fd fa fa fd fa fa fa fd fd fa fa fd fa
=>0x0c047fff8030: fa fa fd fa fa fa 00 00 fa fa[04]fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==30==ABORTING

Source: Windows Questions C++

LEAVE A COMMENT