Last Updated on 2021-10-06 by Clay
When I was solving LeetCode questions recently, I got an error that did not provide any line number:
ERROR: AddressSanitizer: heap-buffer-overflow on address
This is troublesome, and suddenly I don't know how to debug.
I checked it on the Internet and found that Address Sanitizer is a tool to check whether the memory access is wrong.
Generally, when the compiler is executed on the local side, because there is no setting, it is almost certainly not encountered, because C/C++ compiler does not do boundary checking for your memory access.
But your code run in LeetCode, they strictly controlled not to access the memory block that is not requested by you. So the error message will be return when the memory block is requested incorrectly.
Solution
The solution is simple but troublesome: make sure that you have not made a request that exceeds the memory boundary.
One of the most common situation is to call an index element that exceeds the length of the array.
On the Internet, many people have added judgments to prevent them from accessing the wrong index. This is also a possible method.
Of course, if you want to completely solve this problem, you can also install the Address Sanitizer tool in your own local environment to test.
References
- https://stackoverflow.com/questions/51579267/addresssanitizer-heap-buffer-overflow-on-address
- https://github.com/google/sanitizers/wiki/AddressSanitizer