title: How to use gcc with fsanitize=address?
body: <p>I'm trying to learn how to use <code>-fsanitize=address -fno-omit-frame-pointer</code> to detect memory leaks. I wrote something simple which clearly has a memory leak in it, but compiling with <code>gcc -fsanitize=address -fno-omit-frame-pointer file.c</code> and then executing the function doesn't seem to have a problem.</p>
  
  <p>This post seems to indicate that we need to set botth the compiler flag and linker flag and NOT to use <code>-lasan</code> <a href="https://stackoverflow.com/questions/37970758/how-to-use-addresssanitizer-in-gcc">How to use AddressSanitizer in gcc?</a>. </p>
  
  <p>I don't think it is necessary, but I've tried <code>gcc -fsanitize=address -static-libasan -fno-omit-frame-pointer s.c</code> as the docs seem mention here. <a href="https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html" rel="nofollow noreferrer">https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html</a></p>
  
  <p>I also tried following this: <a href="https://lemire.me/blog/2016/04/20/no-more-leaks-with-sanitize-flags-in-gcc-and-clang/" rel="nofollow noreferrer">https://lemire.me/blog/2016/04/20/no-more-leaks-with-sanitize-flags-in-gcc-and-clang/</a></p>
  
  <p>Just in case this is relevant, I'm on MacOS. I've updated and upgraded gcc via brew. The version info is:
  <code>Apple LLVM version 10.0.1 (clang-1001.0.46.4)</code></p>
  
  <p>Here's the dummy code that I wrote:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  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);
  }
  </code></pre>
  
  <p>I never freed anything so I expect the address sanitizer to catch it. But when I run the code I get the following:</p>
  
  <pre><code>$ ./s
  1
  A????????
  </code></pre>
  
  <p>What am I misunderstanding/doing wrong?</p>