21

When I compile my C++ code with -fsanitize=address, my software prints out a list of leaks at the time it exits. Is there a way to avoid the leaks report (I'm only interested in memory corruptions, not leaks)? I went to the page with ASAN flags page, but it doesn't look like any of those flags is a match.

yugr
  • 19,769
  • 3
  • 51
  • 96
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • Hm, you tell the compiler to add code that checks the correct behavior of your program, but then you don't want the information where your program behaves wrongly? ... Simply remove the flag again. – Rene Jun 27 '18 at 11:07
  • 1
    what is the point of using `-fsanitize=address` if you don't want his report – Tyker Jun 27 '18 at 11:07
  • 7
    @Tyker Because OP is interested in memory overflows but not memory leaks? – yugr Jun 27 '18 at 11:12
  • 3
    @yugr Indeed. I want to know of memory overflows, double free, use of temporary references after the object was destroyed, etc. Leaks don't matter as I run an app. for under 1 sec. and quit. It can leak as much as it wants. Unix way! – Alexis Wilke Jun 27 '18 at 20:01

1 Answers1

32

You can run with export ASAN_OPTIONS=detect_leaks=0 or add a function to your application:

#ifdef __cplusplus
extern "C"
#endif
const char* __asan_default_options() { return "detect_leaks=0"; }

See Asan flags wiki and common sanitizer flags wiki for more details.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • Unfortunately, there is no "detect_leaks" in the spec provided... – Hardwired Aug 01 '22 at 09:20
  • 1
    @AlexandrDerkach You mean `detect_leaks` is not mentioned in [wiki](https://stackoverflow.com/help/minimal-reproducible-example)? Yes, that is unfortunate, I wish someone fixed that wikipage. – yugr Aug 01 '22 at 09:25
  • 1
    Yes. But I've found it like inside this spec: [link](https://github.com/google/sanitizers/wiki/SanitizerCommonFlags) – Hardwired Aug 02 '22 at 06:58
  • 1
    @AlexandrDerkach Ah, so they moved the description. I've updated the answer, thank you. – yugr Aug 02 '22 at 07:02
  • 1
    Adding a note: this also works in C++, if you prepend extern "C". – rwallace May 12 '23 at 04:55
  • 1
    @rwallace Oh, right, added this to the answer. – yugr May 12 '23 at 15:06