當前位置:網站首頁>解决報錯:AddressSanitizer: heap-buffer-overflow

解决報錯:AddressSanitizer: heap-buffer-overflow

2022-01-27 02:24:12 山頂夕景

leetcode上報錯:

=================================================================
==42==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60c000000888 at pc 0x00000034f486 bp 0x7ffd5554bb10 sp 0x7ffd5554bb08
READ of size 8 at 0x60c000000888 thread T0
    #4 0x7fb0243d90b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Address 0x60c000000888 is a wild pointer.
Shadow bytes around the buggy address:
  0x0c187fff80c0: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd
  0x0c187fff80d0: fd fd fd fd fd fd fd fa fa fa fa fa fa fa fa fa
  0x0c187fff80e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa
  0x0c187fff80f0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c187fff8100: 00 00 00 00 00 00 00 fa fa fa fa fa fa fa fa fa
=>0x0c187fff8110: fa[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8120: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8130: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8140: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8150: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c187fff8160: 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

leetcode使用AddressSanitizer檢查內存是否存在非法訪問。報此錯,主要是訪問了非法內容。
解决方法:數組訪問越界,導致此錯,後來發現是在訪問二維數組的邊界row和col弄反了。。

【注意】
LeetCode系統在執行代碼時,系統都會判斷數組越界問題,並直接報錯,根據經驗leetcode在用到數組或指針時,做兩種處理:

  • 1.定義指針時,需要申請內存塊,如 int* data = malloc(SIZE * sizeof(int));後面訪問指針時,不要大於SIZE值的地址範圍。
  • 2.定義數組,如int data[SIZE] 後,訪問數組時,不要大於SIZE值的地址範圍。

版權聲明
本文為[山頂夕景]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201270224115458.html

隨機推薦