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

            Bug ID: 90516
           Summary: Strange behaviour of code if function no return value
                    and code embraced by try..catch with opt flags
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matszpk at interia dot pl
  Target Milestone: ---

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

OS: Arch Linux 2019 (updated in 2019-05-12).
gcc -v output:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --enable-libmpx --with-system-zlib --with-isl
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu
--disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object
--enable-linker-build-id --enable-lto --enable-plugin
--enable-install-libiberty --with-linker-hash-style=gnu
--enable-gnu-indirect-function --enable-multilib --disable-werror
--enable-checking=release --enable-default-pie --enable-default-ssp
--enable-cet=auto
Thread model: posix
gcc version 8.3.0 (GCC)

I encountered that bug when I trying to test some program that have bug: a
missing return in loading file function that code was embraced by try...catch.
Due to this bug and enabled optimization's flags program aborts due to double
free of the memory and it was strange behaviour of this function.
Later, I wrote sample program that load some string from file and doing nothing
this same bug (missing return) with code embraced by try..catch. If program was
compiled with optimizations flags (-O3) then and if file was exist and then
program print out exception in by statement in catch clause (just run clause)
with like: 'Exception at loading: basic_ios::clear: iostream error'.

In attachment are sample program code the preprocessed file of this sample
program.

program has been compiled following gcc command:
g++ -Wall -std=c++11 -O3 -o fstream-inc-beh fstream-inc-beh.cpp

Sample program:
-------------------------------
#include <iostream>
#include <fstream>
#include <exception>

bool loadFile(const char* fileName)
{
    try
    {
        std::ifstream ifs(fileName);
        std::string s;
        ifs >> s;
    }
    catch(const std::exception& ex)
    {
        std::cout << "Exception at loading: " << ex.what() << std::endl;
        return false;
    }
}

int main(int argc, const char** argv)
{
    if (argc < 2)
    {
        std::cout << "PROGRAM file" << std::endl;
        return 0;
    }
    if (loadFile(argv[1]))
        std::cout << "OK. Loaded" << std::endl;
    else
        std::cout << "ERROR: Not loaded!" << std::endl;
    return 0;
}
--------------
end of program.

warnings while compiling this program:
---------
fstream-inc-beh.cpp: In function ‘bool loadFile(const char*)’:
fstream-inc-beh.cpp:18:1: warning: control reaches end of non-void function
[-Wreturn-type]
 }
 ^
-----------

Reply via email to