<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 - Missing return statement causes wrong branch to be taken"
href="https://bugs.llvm.org/show_bug.cgi?id=43075">43075</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missing return statement causes wrong branch to be taken
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>7.0
</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>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>doughera@lafayette.edu
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=22409" name="attach_22409" title="Program to show odd effect of missing return statement">attachment 22409</a> <a href="attachment.cgi?id=22409&action=edit" title="Program to show odd effect of missing return statement">[details]</a></span>
Program to show odd effect of missing return statement
In the following function,
int checkit(void)
{
int rtn = 0;
if (rtn != 0) {
printf("In rtn != 0 branch with rtn = %d\n", rtn);
printf("Not Ok.\n");
exit(1);
}
}
the missing return statement causes clang++ -O1 to take the "Not Ok" branch,
even though 'rtn' is correctly reported to be zero. A complete test case is
attached.
clang++ correctly reports the error:
warning: control may reach end of non-void function [-Wreturn-type]
and I gather that omitting the return statement in C++ leads to undefined
behavior, but taking the rtn !=0 branch is still rather surprising.
This occurred in a C program inside Perl's Configure that a user wishes to
compile with clang++. We have already fixed the Configure code to work
correctly. It seems to be similar to
<a class="bz_bug_link
bz_status_RESOLVED bz_closed"
title="RESOLVED INVALID - LLVM optimizer eliminates zero check if return statement is missing"
href="show_bug.cgi?id=42897">https://bugs.llvm.org/show_bug.cgi?id=42897</a>, but I thought the behavior
sufficiently odd to merit reporting.
Steps to reproduce:
Compile the attached program with
clang++ -O1 -o try try.c && ./try; echo $?
clang++ will correctly warn about 'control may reach end of non-void function'.
Expected output:
The expected output is "Ok", with an exit status of 0.
Actual output:
a. With clang++-7 (clang version 7.0.1-8 (tags/RELEASE_701/final),
distributed with current Debian stable), the output is
In rtn != 0 branch with rtn = 0
Not Ok.
Exit status is 1.
b. With a recent git checkout, clang version 10.0.0
(<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
928071ae4ef5e2e6342afb126518a79fde81cf8b) the output is
empty (nothing is printed) and the exit status is 0.
Build date and hardware:
2019-08-19, x86_64-pc-linux-gnu</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>