[Bug] Segmentation fault while getting SourceLocation of a Decl

Hi,

I am iterating on an std::map whose keys are FunctionDecl's pointers. Many times I encounter no problems, but sometimes I get segfaults for specific C files and I do not manage to find out why nor where parsed C code might produces such a bug.

Every time it happens when calling clang::FunctionDecl::getNameInfo() and then clang::Decl::getLocation()

I am wondering whether the getNameInfo/getLocation functions are always safe or not ? Do you think it has to come from my tool ? Thanks for the help !

Here is an example backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000544bb6 in clang::Decl::getLocation (this=0x21) at /import/linux/x86_64/soft/llvm/3.4/include/clang/AST/DeclBase.h:373
373 SourceLocation getLocation() const { return Loc; }
(gdb) bt
#0 0x0000000000544bb6 in clang::Decl::getLocation (this=0x21) at /import/linux/x86_64/soft/llvm/3.4/include/clang/AST/DeclBase.h:373
#1 0x0000000000544c14 in clang::FunctionDecl::getNameInfo (this=0x21) at /import/linux/x86_64/soft/llvm/3.4/include/clang/AST/Decl.h:1605
#2 0x000000000054454f in DotGraph::printDotGraph (this=0x7fffffffe260, IOInfo=0x14eb110) at DotGraph.cpp:32
#3 0x000000000058e27d in main (argc=4, argv=0x7fffffffe658) at main.cpp:73

That sounds like you're ending up with null FunctionDecl pointers—notice the "this=0x21" in your backtrace. Are you modifying the map while you're iterating over it? That's always dangerous.

Jordan

That sounds like you're ending up with null FunctionDecl pointers—notice the "this=0x21" in your backtrace. Are you modifying the map while you're iterating over it? That's always dangerous.

Jordan

Thank you Jordan for your answer. I finally found out that my error was coming from using std::vector::back() which has undefined behavior when the vector is empty.
I knew there was something wrong with this pointer value "0x21" but I could not figure out why it was not a null pointer. Now I understood, thanks again :slight_smile: