<div dir="ltr"><div dir="ltr">On Sun, Jun 21, 2020 at 11:27 AM Colin Watson <<a href="mailto:cjwatson@ubuntu.com">cjwatson@ubuntu.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sun, Jun 21, 2020 at 09:23:20AM -0700, David L wrote:<br>
> The program below crashes when compiled with -O2 or greater optimization on<br>
> Ubuntu 20.04 (it worked fine on at least 1[468].04. I know that it's my own<br>
> fault for forgetting a return value and ignoring the compiler warning, but<br>
> IMHO, this shouldn't result in a difficult to debug segfault. Shame on me<br>
> for writing code with undefined behavior. Shame on gcc for a punishment not<br>
> suited to the crime. ;) I write this in the hope that somebody else doesn't<br>
> waste hours debugging a problem with a program that has run fine through<br>
> many versions of gcc with a previously benign missing but ignored return<br>
> value.<br><br></blockquote><div><snip></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I can't reproduce this crash, and I don't *think* the letter of the C<br>
standard forbids this program (since the return value isn't used; see<br>
C99 6.9.1(12)).  But it's quite possible it might depend on other<br>
compiler options or details that you haven't told us; and in any case C<br>
is a sufficiently unsafe language that I would consider it, er,<br>
ambitious to ignore compiler warnings.  If you're writing C, take all<br>
the static-analysis help you can get!<br></blockquote><div><br></div><div>My bad, I should have mentioned the filename extension was cpp, not c, so I should have said g++ not gcc. And you're right, I shouldn't ignore compiler warnings. Ones like this have historically been benign and I was lazy. But I bet I'm not the only one guilty of this transgression from time to time. ;)</div><div><br></div><div>Anyway here's more info:</div><div>david@scd0:/tmp$ cat ub2004crash.cpp<br></div><div>int init()<br>{<br>}<br>int main(int argc, char *argv[])<br>{<br>  init();<br>  return 0;<br>}<br>david@scd0:/tmp$ gcc -o ub2004crash -O2 ub2004crash.cpp<br>ub2004crash.cpp: In function ‘int init()’:<br>ub2004crash.cpp:3:1: warning: no return statement in function returning non-void [-Wreturn-type]<br>    3 | }<br>      | ^<br>david@scd0:/tmp$ ./ub2004crash <br>Segmentation fault (core dumped)</div><div><br></div><div><br></div></div></div>