Search in a rotated array

Viewed 35
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n=nums.size();
        int i=0, low, high;
        int pos=-1;
        while(nums[i]<nums[i+1])
            i++;
        //i stores the index of max element of the array
        if(target<nums[0]) {
            low = i+1;
            high = n-1;
        }
        else {
            low = 0;
            high = i;
        }
        while(low<=high) {
            int mid = (low+high)/2;
            // cout<<low<<high<<mid<<target;
            // cout<<(nums[mid]==target);
            if(nums[mid] == target){
                pos = mid;
                // flag=1;
                break;
            }
            else if(nums[mid]<target) {
                low = mid+1;
            }
            else {
                high = mid-1;
            }
        }
        // if (flag==0)
        //     return -1;
        return pos;
    }
};

I have attached the code above

    =================================================================
==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000194 at pc 0x000000345587 bp 0x7fff5346e5a0 sp 0x7fff5346e598
READ of size 4 at 0x602000000194 thread T0
    #2 0x7f210b53a0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000194 is located 0 bytes to the right of 4-byte region [0x602000000190,0x602000000194)
allocated by thread T0 here:
    #6 0x7f210b53a0b2  (/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 fd fd fa fa fd fa fa fa fd fa fa fa fd fa
  0x0c047fff8020: fa fa fd fa fa fa fd fd fa fa fd fa fa fa fd fa
=>0x0c047fff8030: fa fa[04]fa fa fa fa fa fa fa fa 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
==31==ABORTING

This is the error message that I am getting on LeetCode. Could somebody look into this. The question was to search is a rotated array that was initially sorted in an ascending order. When I run this code, it works perfectly fine but when I try to submit, it gives the heap error.

0 Answers
Related