[Bug tree-optimization/103176] New: -foptimize-strlen causes stringop-overflow warning

josiah_vanderzee at mediacombb dot net gcc-bugzilla@gcc.gnu.org
Wed Nov 10 18:40:31 GMT 2021


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

            Bug ID: 103176
           Summary: -foptimize-strlen causes stringop-overflow warning
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josiah_vanderzee at mediacombb dot net
  Target Milestone: ---

The following warning appears while compiling
https://github.com/minetest/irrlicht with at least -O2. Compiling with
-fno-optimize-strlen makes the warning disappear. Diffing a binary compiled
with the optimization and one compiled without show that there is no difference
in the generated code.The relevant functions are inlined (and I verified this
by hand) so all of it is present in the binary. The warning follows.

I have considered the view that the warning is desirable; the code is very
dangerous; and I am not claiming that the warning is incorrect. The problem
here is that for some reason the warning only occurs when strlen optimizations
are enabled. This doesn't seem logical to me.

NOTE: The binaries I diffed were generated by compiling the flattenFilepath()
function as a unit test.

[49/99] ccache /usr/bin/c++ -DIRRLICHT_EXPORTS -D_IRR_STATIC_LIB_ -I../include
-I../source/Irrlicht -O3 -fPIC -Wall -pipe -fno-exceptions -fno-rtti
-std=gnu++11 -MD -MT source/Irrlicht/CMakeFiles/IRRIOOBJ.dir/CFileSystem.cpp.o
-MF source/Irrlicht/CMakeFiles/IRRIOOBJ.dir/CFileSystem.cpp.o.d -o
source/Irrlicht/CMakeFiles/IRRIOOBJ.dir/CFileSystem.cpp.o -c
../source/Irrlicht/CFileSystem.cpp
In file included from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
In member function ‘irr::core::string<T> irr::core::string<T,
TAlloc>::subString(irr::u32, irr::s32, bool) const [with T = char; TAlloc =
irr::core::irrAllocator<char>]’,
    inlined from ‘virtual irr::io::path&
irr::io::CFileSystem::flattenFilename(irr::io::path&, const path&) const’ at
../source/Irrlicht/CFileSystem.cpp:679:58:
../include/irrString.h:937:19: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
  937 |   o.array[length] = 0;
      |   ~~~~~~~~~~~~~~~~^~~
In file included from ../include/irrString.h:9,
                 from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
../include/irrAllocator.h: In member function ‘virtual irr::io::path&
irr::io::CFileSystem::flattenFilename(irr::io::path&, const path&) const’:
../include/irrAllocator.h:60:22: note: at offset [1, -1] to an object with size
1 allocated by ‘operator new’ here
   60 |   return operator new(cnt);
      |          ~~~~~~~~~~~~^~~~~
In file included from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
In member function ‘irr::core::string<T> irr::core::string<T,
TAlloc>::subString(irr::u32, irr::s32, bool) const [with T = char; TAlloc =
irr::core::irrAllocator<char>]’,
    inlined from ‘virtual irr::io::path
irr::io::CFileSystem::getFileBasename(const path&, bool) const’ at
../source/Irrlicht/CFileSystem.cpp:655:73:
../include/irrString.h:937:19: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
  937 |   o.array[length] = 0;
      |   ~~~~~~~~~~~~~~~~^~~
In file included from ../include/irrString.h:9,
                 from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
../include/irrAllocator.h: In member function ‘virtual irr::io::path
irr::io::CFileSystem::getFileBasename(const path&, bool) const’:
../include/irrAllocator.h:60:22: note: at offset [1, -1] to an object with size
1 allocated by ‘operator new’ here
   60 |   return operator new(cnt);
      |          ~~~~~~~~~~~~^~~~~
In file included from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
In member function ‘irr::core::string<T> irr::core::string<T,
TAlloc>::subString(irr::u32, irr::s32, bool) const [with T = char; TAlloc =
irr::core::irrAllocator<char>]’,
    inlined from ‘virtual irr::io::path
irr::io::CFileSystem::getRelativeFilename(const path&, const path&) const’ at
../include/coreutil.h:158:70:
../include/irrString.h:937:19: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
  937 |   o.array[length] = 0;
      |   ~~~~~~~~~~~~~~~~^~~
In file included from ../include/irrString.h:9,
                 from ../include/coreutil.h:8,
                 from ../include/IReadFile.h:9,
                 from ../include/IFileArchive.h:8,
                 from ../include/IFileSystem.h:9,
                 from ../source/Irrlicht/CFileSystem.h:8,
                 from ../source/Irrlicht/CFileSystem.cpp:7:
../include/irrAllocator.h: In member function ‘virtual irr::io::path
irr::io::CFileSystem::getRelativeFilename(const path&, const path&) const’:
../include/irrAllocator.h:60:22: note: at offset [1, -1] to an object with size
1 allocated by ‘operator new’ here
   60 |   return operator new(cnt);
      |          ~~~~~~~~~~~~^~~~~```


More information about the Gcc-bugs mailing list