Is passing the uninitialized variable to function considered UB in C++?

70 views Asked by At

Recently I was writing my raytracer project and made some bugs in it. I have a function:

double Length(const Vector& v) {
    return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}

and then I had a typo in this statement:

Vector normal = Length(normal) < eps ? intersection.GetNormal() : baricentric.GetNormal();

So when normal passed to function it is declared but not defined (the name was not present in other scope, but this). The program ran incorrectly and I had to debug it with gdb before the bug was found.

I expect that it should be UB, but when I built my project with clang, using this flags:

set(CMAKE_CXX_FLAGS_ASAN "-g -fsanitize=address,undefined -fno-sanitize-recover=all"
    CACHE STRING "Compiler flags in asan build"
    FORCE)

I did not get Compiler Error or even Runtime Error during program run.

So is it UB? Should USAN catch this kind of bugs? I have a hypothesis that the problem may be in some compiler optimizations,( RVO e.g ), so if I would compile program with -O0 will it turn off all optimization or I need another flag?

Thanks!

0

There are 0 answers