LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 1448 - Pointer to invalid memory kept
Summary: Pointer to invalid memory kept
Status: RESOLVED FIXED
Alias: None
Product: tools
Classification: Unclassified
Component: llvm-ld (show other bugs)
Version: 2.0
Hardware: All All
: P normal
Assignee: Chris Lattner
URL:
Keywords: compile-fail
Depends on:
Blocks:
 
Reported: 2007-05-26 17:24 PDT by jlh
Modified: 2010-02-22 12:41 PST (History)
1 user (show)

See Also:
Fixed By Commit(s):


Attachments
fix using strdup (1.20 KB, patch)
2007-05-28 08:55 PDT, Nick Lewycky
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jlh 2007-05-26 17:24:29 PDT
In tools/llvm-ld/llvm-ld.cpp, around line 353, a 'const char *' to a temporary 
std::string is being stored, with the string going out of scope right after, 
making that pointer invalid.  Snippet:

    std::string lib_name = "-l" + LinkItems[index].first;
    args.push_back(lib_name.c_str());
    <end of scope>

I originally reported this problem on the llvm-commit list, because I was told 
to do so, but maybe I should have opened a bug report anyway.  See [1] and its 
follow-ups for a simple fix.

Funnily, in my particular case, things still worked fine as long as I only had 
one -l option.  But with several such option, all pointers in 'args' pointed to 
the exact same memory location, still containing the last library name 
processed and gcc got called like "gcc -llib -llib -llib -llib".

[1] http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070430/
048734.html
Comment 1 Chris Lattner 2007-05-26 17:27:33 PDT
This is certainly a bug, but the attached patch doesn't fix it completely, because the vector could cause 
reallocation of the string data.
Comment 2 jlh 2007-05-26 17:51:31 PDT
Yes, that's been mentioned too[1].  std::list would be one way to go.

[1] http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070430/
048862.html
Comment 3 Nick Lewycky 2007-05-28 08:55:15 PDT
Created attachment 869 [details]
fix using strdup

Here's a potential fix based on strdup.

Note that this could leak if an exception is thrown, though I don't think
that's possible. It's too bad that vector<auto_ptr> is illegal code.
Comment 4 Chris Lattner 2007-06-19 11:47:58 PDT
Fixed, patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070618/050627.html

-Chris