[llvm-commits] Patch for bug in llvm-ld

Chris Lattner clattner at apple.com
Tue May 1 11:53:17 PDT 2007


On Apr 30, 2007, at 6:45 AM, jlh wrote:
> Hello!
>
> I've been told to send this here.  In tools/llvm-ld/llvm-ld.cpp,
> line 360, a const char* to a temporary std::string is being
> stored, with the string going out of scope right after, making
> that pointer invalid.
>
>     std::string lib_name = "-l" + LinkItems[index].first;
>     args.push_back(lib_name.c_str());
>     <end of scope>

Thanks, but this doesn't seem safe.  If the args_temp vector is  
reallocated, it will move all the std::string objects, invalidating  
the pointers.

-Chris

> The attached patch fixes this in a simple way.
>
> jlh
> Index: llvm-ld.cpp
> ===================================================================
> RCS file: /var/cvs/llvm/llvm/tools/llvm-ld/llvm-ld.cpp,v
> retrieving revision 1.51
> diff -u -r1.51 llvm-ld.cpp
> --- llvm-ld.cpp	29 Apr 2007 23:59:47 -0000	1.51
> +++ llvm-ld.cpp	30 Apr 2007 13:34:21 -0000
> @@ -330,6 +330,9 @@
>    //  We can't just assemble and link the file with the system  
> assembler
>    //  and linker because we don't know where to put the _start  
> symbol.
>    //  GCC mysteriously knows how to do it.
> +
> +  // args_temp is for storing temporary strings while we have a  
> const char * to them
> +  std::vector<std::string> args_temp;
>    std::vector<const char*> args;
>    args.push_back(gcc.c_str());
>    args.push_back("-fno-strict-aliasing");
> @@ -354,8 +357,8 @@
>    for (unsigned index = 0; index < LinkItems.size(); index++)
>      if (LinkItems[index].first != "crtend") {
>        if (LinkItems[index].second) {
> -        std::string lib_name = "-l" + LinkItems[index].first;
> -        args.push_back(lib_name.c_str());
> +        args_temp.push_back("-l" + LinkItems[index].first);
> +        args.push_back(args_temp.back().c_str());
>        } else
>          args.push_back(LinkItems[index].first.c_str());
>      }
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list