tqdm.cpp icon indicating copy to clipboard operation
tqdm.cpp copied to clipboard

AddressSanitizer error in an unused variable

Open PeterZhizhin opened this issue 4 years ago • 0 comments

There is an address sanitizer error in the following code in utils.h:

    template<class Node>
    AtomicList<Node>::AtomicList()
            : meta(static_cast<Node *>(&meta), static_cast<Node *>(&meta)) {}

The error happens when AtomicList is constructed inside an unused variable: standard_sink(SinkOptions(STDERR_FILENO)). Here is the error message:

/mnt/c/Users/peter/Documents/Projects/RayTracerCpp/src/tqdm/utils.h:380:48: runtime error: downcast of address 0x561e0e75b518 which does not point to an object of type 'AbstractLine'
0x561e0e75b518: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
pc_0x561e0e34b171###func_tqdm::AtomicList<tqdm::AbstractLine>::AtomicList()###file_<null>###line_0###obj_(RayTracerCpp+0x15b171)
pc_0x561e0e344687###func_tqdm::Sink::Sink(tqdm::SinkOptions)###file_<null>###line_0###obj_(RayTracerCpp+0x154687)
pc_0x561e0e341dc8###func___static_initialization_and_destruction_0###file_/mnt/c/Users/peter/Documents/Projects/RayTracerCpp/src/tqdm/utils.h###line_367###obj_(RayTracerCpp+0x151dc8)
pc_0x561e0e3421f3###func__GLOBAL__sub_I__ZN4tqdm13_term_move_upEv###file_/mnt/c/Users/peter/Documents/Projects/RayTracerCpp/src/main.cpp###line_123###obj_(RayTracerCpp+0x1521f3)
pc_0x561e0e51c35c###func___libc_csu_init###file_<null>###line_0###obj_(RayTracerCpp+0x32c35c)
pc_0x7f24d15c703f###func___libc_start_main###file_<null>###line_0###obj_(libc.so.6+0x2703f)
pc_0x561e0e33dddd###func__start###file_<null>###line_0###obj_(RayTracerCpp+0x14dddd)

Removing static_cast actually unveils the error. Remove static_cast in the lines above to get:

    template<class Node>
    AtomicList<Node>::AtomicList()
            : meta(&meta, &meta) {}

And here is the error:

....
note: candidate constructor not viable: cannot convert from base class pointer 'AtomicNode<tqdm::Sink> *' to derived class pointer 'tqdm::Sink *' for 1st argument
        AtomicNode(Node *next, Node *prev);

The static_cast hides this programming error and makes it a runtime error. I assume that the code was running fine as this variable is never used.

PeterZhizhin avatar Jul 07 '20 13:07 PeterZhizhin