(Leetcode) ERROR: AddressSanitizer: heap-buffer-overflow

坐标:LeetCode 605:种花问题
原代码:

int len=flowerbedSize;
for(int i=0;i

然后……,它就报错了……

==42==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000174 at pc 0x55dafceb37fc bp 0x7ffe637aa8c0 sp 0x7ffe637aa8b0
READ of size 4 at 0x602000000174 thread T0
    #2 0x7f28cd0d50b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000174 is located 0 bytes to the right of 4-byte region [0x602000000170,0x602000000174)
allocated by thread T0 here:
    #0 0x7f28cdd1abc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #3 0x7f28cd0d50b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  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 05 fa fa fa 06 fa fa fa 06 fa fa fa 05 fa
  0x0c047fff8010: fa fa 06 fa fa fa 06 fa fa fa 05 fa fa fa 05 fa
=>0x0c047fff8020: fa fa 05 fa fa fa 05 fa fa fa 05 fa fa fa[04]fa
  0x0c047fff8030: fa fa fa 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
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
==42==ABORTING
最后执行的输入:
[1]
0

经过不懈的努力,我总算知道原因了:

int len=flowerbedSize;
for(int i=0;i

在原来的代码中,最后一次for循环会执行i++,此时i=len(已经造成越界),然后判断(i

然后,更改后的代码如下:

int len=flowerbedSize-1;
for(int i=0;i

(o゜▽゜)o☆[BINGO!]
问题解决!!!
一定一定要注意细节啊!!!细节决定成败!!!

你可能感兴趣的:(小笔记,服务器,运维)