On Sun, Jan 30, 2022 at 10:47:41AM +0100, Theodore Papadopoulo wrote:
> Before creating a bug report, I want to check with the GCC community (all
> the more that checking that the problem has not yet been reported is
> complicated at leat for me).
>
> The following (admitedly buggy) program generates a segmentation violation
> on fedora 35 (this is with g++ 11.2.1 20211203 (Red Hat 11.2.1-7) (GCC))
> when compiled with -O3 (other versions replacing unisgned by std::string may
> trigger the exception instead of the segv)
>
> bool assert_sthg(const unsigned s) {
> if (s==123)
> throw 1;
> }
>
> int main() {
> assert_sthg(0);
> return 0;
> }
>
> When compiling, we indeed get a warning:
>
> test.C:4:1: warning: control reaches end of non-void function
> [-Wreturn-type]
>
> I can well understand that the program being buggy that the optimizer is
> allowed to do anything including the observed segmentation violation.
> Yet the result is quite surprising....
Undefined behavior can have any kind of surprising behavior.
> The question is, in that case, wouldn't it be better to turn the warning
> into an error at -O3 ?
No, it can't be an error by default, it is undefined behavior only at
runtime, if you never call the function or always call it with
assert_sthg(123), then the program can be valid.
Jakub