I'm trying to learn how to use -fsanitize=address -fno-omit-frame-pointer
to detect memory leaks. I wrote something simple which clearly has a memory leak in it, but compiling with gcc -fsanitize=address -fno-omit-frame-pointer file.c
and then executing the function doesn't seem to have a problem.
This post seems to indicate that we need to set botth the compiler flag and linker flag and NOT to use -lasan
How to use AddressSanitizer in gcc?.
I don't think it is necessary, but I've tried gcc -fsanitize=address -static-libasan -fno-omit-frame-pointer s.c
as the docs seem mention here. https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
I also tried following this: https://lemire.me/blog/2016/04/20/no-more-leaks-with-sanitize-flags-in-gcc-and-clang/
Just in case this is relevant, I'm on MacOS. I've updated and upgraded gcc via brew. The version info is:
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Here's the dummy code that I wrote:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char *buffer = malloc(1024);
sprintf(buffer, "%d", argc);
printf("%s\n", buffer);
buffer = malloc(10);
buffer[0] = 'A';
buffer[9] = '\0';
printf("%s\n", buffer);
}
I never freed anything so I expect the address sanitizer to catch it. But when I run the code I get the following:
$ ./s
1
A????????
What am I misunderstanding/doing wrong?