Home > database >  Unable to find error in Leetcode 733.Flood Fill
Unable to find error in Leetcode 733.Flood Fill

Time:07-07

I am getting the error below for this question in Leetcode, but I am unable to spot the error.

Question: https://leetcode.com/problems/flood-fill/

class Solution {
public:  
     int visited[50][50]={0};
    vector<vector<int>> helper(vector<vector<int>>& image, int sr, int sc, int color, int c) {
        int n = image.size();
        int m = image[0].size();
        image[sc][sr] = color;
        visited[sr][sc] = 1;
        if(sr>0 and visited[sr-1][sc] == 0 and image[sr-1][sc]==c){
            helper(image, sr-1, sc, color, c);
        }
        if(sc>0 and visited[sr][sc-1] ==0 and image[sr][sc-1]==c){
            helper(image, sr, sc-1, color, c);
        }
        if(sc<m-1 and visited[sr][sc 1] ==0 and image[sr][sc 1]==c){
            helper(image, sr, sc 1, color, c);
        }
        if(sr<n-1 and visited[sr 1][sc]==0 and image[sr 1][sc]==c){
            helper(image, sr 1, sc, color, c);
        }
        return image;
    }
    
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        int n = image.size();
        int m = image[0].size();
        return helper(image, sr, sc, color, image[sr][sc]);
    }
};

Testcase:

[[0,0,0],[0,0,0]]
0
0
0

Error Message:

=================================================================
==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6040000000c0 at pc 0x000000346e0d bp 0x7ffdecd39d20 sp 0x7ffdecd39d18
READ of size 8 at 0x6040000000c0 thread T0
    #6 0x7f8371b800b2  (/lib/x86_64-linux-gnu/libc.so.6 0x270b2)
0x6040000000c0 is located 0 bytes to the right of 48-byte region [0x604000000090,0x6040000000c0)
allocated by thread T0 here:
    #6 0x7f8371b800b2  (/lib/x86_64-linux-gnu/libc.so.6 0x270b2)
Shadow bytes around the buggy address:
  0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff8000: fa fa fd fd fd fd fd fd fa fa 00 00 00 00 00 06
=>0x0c087fff8010: fa fa 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa
  0x0c087fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8060: 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

CodePudding user response:

In your code, sr represents the current row and sc represents the current column.

Therefore, this line of code:

image[sc][sr] = color;

should instead be changed to:

image[sr][sc] = color;
  •  Tags:  
  • c
  • Related