https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89056

            Bug ID: 89056
           Summary: Optimizer generates bad code for non-void function
                    that fails to return a value
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: darryl_okahata at keysight dot com
  Target Milestone: ---

Created attachment 45530
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45530&action=edit
preprocessed file

System: ancient Red Hat Enterprise Linux Server release 6.10 (Santiago)
Intel x86_64 system.


g++ -v

Using built-in specs.
COLLECT_GCC=/hped/builds/tfstools/gcc540/linux_x86_64//gcc/8.2.0/bin/g++_x86_64
COLLECT_LTO_WRAPPER=/a/new/sr/proton/d11/build/tfstools/gcc472/linux_x86_64/gcc/8.2.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/8.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --enable-checking=release --enable-languages=c,c++
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu --disable-multilib --with-system-zlib
--prefix=/hped/builds/tfstools/gcc472/linux_x86_64/gcc/8.2.0_rebld
--with-gmp=/gfs/sr/sherry/d1/local/dbjornba/btmp
--with-mpfr=/gfs/sr/sherry/d1/local/dbjornba/btmp
--with-mpc=/gfs/sr/sherry/d1/local/dbjornba/btmp
Thread model: posix
gcc version 8.2.0 (GCC)


If you have cruddy code with a non-void function that fails to return a value,
the gcc optimizer can generate an "infinite" loop for a simple iterative loop
(see the end of the *.ii file):

    bool test::bah(void)
    {
        std::deque<struct foo>::iterator iter;

        for (iter = values.begin(); iter != values.end(); iter++)
            iter->myval -= 0.1;
        // returning a value here causes correct code to be generated.
    }

The bug can be easily seen using:

    g++ -S -O badbad.cc
    badbad.cc: In member function 'bool test::bah()':
    badbad.cc:42:1: warning: no return statement in function returning non-void
[-Wreturn-type]
     }
     ^

The generated assembly code shows an "infinite" loop for the simple iterative
loop (which only "terminates" when a bus error occurs).  The correct code is
generated if you add a proper return value.

Yes, this is a poster child for using -Werror=return-type, but gcc should still
not generate bad code (the return value will, of course, still be undefined).

Reply via email to