20.04 gcc with -O2 crashes due to previously benign error

David L david4lists at gmail.com
Mon Jun 22 17:51:40 UTC 2020


On Sun, Jun 21, 2020 at 11:27 AM Colin Watson <cjwatson at ubuntu.com> wrote:

> On Sun, Jun 21, 2020 at 09:23:20AM -0700, David L wrote:
> > The program below crashes when compiled with -O2 or greater optimization
> on
> > Ubuntu 20.04 (it worked fine on at least 1[468].04. I know that it's my
> own
> > fault for forgetting a return value and ignoring the compiler warning,
> but
> > IMHO, this shouldn't result in a difficult to debug segfault. Shame on me
> > for writing code with undefined behavior. Shame on gcc for a punishment
> not
> > suited to the crime. ;) I write this in the hope that somebody else
> doesn't
> > waste hours debugging a problem with a program that has run fine through
> > many versions of gcc with a previously benign missing but ignored return
> > value.
>
> <snip>


> I can't reproduce this crash, and I don't *think* the letter of the C
> standard forbids this program (since the return value isn't used; see
> C99 6.9.1(12)).  But it's quite possible it might depend on other
> compiler options or details that you haven't told us; and in any case C
> is a sufficiently unsafe language that I would consider it, er,
> ambitious to ignore compiler warnings.  If you're writing C, take all
> the static-analysis help you can get!
>

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.
;)

Anyway here's more info:
david at scd0:/tmp$ cat ub2004crash.cpp
int init()
{
}
int main(int argc, char *argv[])
{
  init();
  return 0;
}
david at scd0:/tmp$ gcc -o ub2004crash -O2 ub2004crash.cpp
ub2004crash.cpp: In function ‘int init()’:
ub2004crash.cpp:3:1: warning: no return statement in function returning
non-void [-Wreturn-type]
    3 | }
      | ^
david at scd0:/tmp$ ./ub2004crash
Segmentation fault (core dumped)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20200622/5bc46671/attachment.html>


More information about the ubuntu-users mailing list