[LLVMdev] dodgy use of c_str()

Chris Lattner clattner at apple.com
Fri Nov 13 08:26:55 PST 2009


On Nov 13, 2009, at 5:07 AM, Jay Foad wrote:

>> From llvm-gcc-4.2/trunk/gcc/llvm-debug.cpp:
>
> void DebugInfo::EmitGlobalVariable(GlobalVariable *GV, tree decl) {
>  // Gather location information.
>  expanded_location Loc = expand_location(DECL_SOURCE_LOCATION(decl));
>  DIType TyD = getOrCreateType(TREE_TYPE(decl));
>  const char *DispName = GV->getNameStr().c_str();
>
> Isn't this use of c_str() dodgy, because the temporary string returned
> by Gv->getNameStr() will be destroyed at the end of the expression, so
> it's no longer valid to use the memory pointed to by DispName?

Yes, absolutely.  Devang, please take a look when you get a chance.   
This should use getName() with StringRef.

-Chris



More information about the llvm-dev mailing list