Memory disinfection

I play with Memory Sanitizer with Clang 3.7.0 on Ubuntu 14.04. The following code works fine:

#include <cstdio> int main() { double ans; printf("Hello World: %f\n", ans); return 0; } 

when compiling with

  clang++ -g -O1 -fsanitize=memory -fsanitize-memory-track-origins=2 -fomit-frame-pointer sanitize.cpp -o sanitize 

I was expecting an error. Does the memory disintegrant consider the fact that ans was not initialized?

Thank you for your help.

+5
source share
3 answers

From the santitizer clang documentation, it’s clear that we are talking only about unified readings of memory from dynamically allocated memory. Automatic memory is not part of disinfection checks.

+5
source

You do not need a Sanitizer to catch this error. The compiler can detect this error at compile time (sanitizers and valgrind work at runtime). In fact, all GCC Clang and ICC will give a warning for this code if you enable warnings. This particular warning is controlled by the -Wuninitialized flag. In general, it is good practice to always use a high level of warning. I would recommend the following combination of warning flags, especially when learning a language:

 -Wall -Wextra -pedantic 

If you receive false positives, only after carefully checking that they are really false, you can turn off certain warnings. There is no reason not to use warning flags. Some projects use the -Werror flag, turning all warnings into errors.

+1
source

Valgrind memcheck may be an option for detecting uninitialized stack values.

Valgrind Documentation:

For uninitialized values ​​originating from a heap block, Memcheck shows where the block was allocated. For uninitialized values ​​coming from the stack distribution, Memcheck can tell you which function assigned this value, but nothing more - usually this shows the original location of the opening parenthesis of the function. Therefore, you must carefully verify that all functional local variables are initialized correctly.

Link: http://valgrind.org/docs/manual/mc-manual.html

0
source

Source: https://habr.com/ru/post/1237599/


All Articles