<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Possible incorrect code generation on use of initializer_list in range-based for loop"
href="https://bugs.llvm.org/show_bug.cgi?id=32824">32824</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Possible incorrect code generation on use of initializer_list in range-based for loop
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.9
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++11
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>jasonr@3db-labs.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>This issue was originally posted at
<a href="http://stackoverflow.com/questions/43624400/is-this-unsafe-usage-of-a-braced-initializer-list-in-a-range-based-for-loop">http://stackoverflow.com/questions/43624400/is-this-unsafe-usage-of-a-braced-initializer-list-in-a-range-based-for-loop</a>
- I didn't get much of an answer there, and I'm inclined to think it might be a
clang bug.
My example program is as follows:
#include <initializer_list>
#include <memory>
struct Test
{
int x;
};
int main()
{
std::unique_ptr<Test> a(new Test);
std::unique_ptr<Test> b(new Test);
std::unique_ptr<Test> c(new Test);
int id = 0;
for(auto t : std::initializer_list<Test*>({a.get(), b.get(), c.get()}))
t->x = id++;
return 0;
}
I compile this with the following command line:
clang++ -std=c++11 -O3 crash.cc -o crash
The example is online at <a href="https://godbolt.org/g/r9uOEd">https://godbolt.org/g/r9uOEd</a> as well.
When I compile the code using clang versions less than or equal to 3.8.1, I get
what I would expect; the generated assembly looks like:
main: # @main
xor eax, eax
ret
No problem there. For clang 3.9.0, 3.9.1, and 4.0.0 (I haven't tested anything
newer), I get the following:
main: # @main
mov rax, qword ptr [rsp - 24]
mov dword ptr [rax], 0
mov rax, qword ptr [rsp - 16]
mov dword ptr [rax], 1
mov rax, qword ptr [rsp - 8]
mov dword ptr [rax], 2
xor eax, eax
ret
It looks like the compiler optimized away the construction of the `Test`
objects; it never invokes `operator new` to allocate memory for them, but it
still runs the body of the for loop, dereferencing pointers that were never
initialized. This obviously results in a segfault.
If I subtly tweak the example, taking out the parentheses used in the
construction of the initializer_list in the range-based for loop, like this:
#include <initializer_list>
#include <memory>
struct Test
{
int x;
};
int main()
{
std::unique_ptr<Test> a(new Test);
std::unique_ptr<Test> b(new Test);
std::unique_ptr<Test> c(new Test);
int id = 0;
for(auto t : std::initializer_list<Test*>{a.get(), b.get(), c.get()})
t->x = id++;
return 0;
}
Then I get good code generation on all clang versions that I've tried. What
I've been trying to understand is whether this is a compiler bug or undefined
behavior. In the first example, is the inner braced initializer treated as a
temporary whose lifetime ends after the range-based for loop is initialized,
and therefore gets optimized out?</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>