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

            Bug ID: 116332
           Summary: -O2 makes c++ std::regex give zero-alloc warnings
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: junxuan_liao at outlook dot com
  Target Milestone: ---

When compiled with -O2, some inlined std::regex code produces -Wzero-alloc
warnings. It's probably a bug since system headers should not produce warnings.
Also, with lto enabled, or without -O2, the warnings disappear.

Attached is a minimum test file and here's the output:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --with-linker-hash-style=gnu
--with-system-zlib --enable-__cxa_atexit --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.1 20240805 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Walloc-zero' '-Werror' '-O2'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/cc1plus -E -quiet -v -D_GNU_SOURCE
regex_alloc_zero.cc -mtune=generic -march=x86-64 -Walloc-zero -Werror -O2
-fpch-preprocess -o a-regex_alloc_zero.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1

/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/x86_64-pc-linux-gnu

/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Walloc-zero' '-Werror' '-O2'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/cc1plus -fpreprocessed
a-regex_alloc_zero.ii -quiet -dumpdir a- -dumpbase regex_alloc_zero.cc
-dumpbase-ext .cc -mtune=generic -march=x86-64 -O2 -Walloc-zero -Werror
-version -o a-regex_alloc_zero.s
GNU C++17 (GCC) version 14.2.1 20240805 (x86_64-pc-linux-gnu)
        compiled by GNU C version 14.2.1 20240805, GMP version 6.3.0, MPFR
version 4.2.1, MPC version 1.3.1, isl version isl-0.26-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 438deb6040cbc4b51ad3ab8b369917b9
In file included from /usr/include/c++/14.2.1/regex:69,
                 from regex_alloc_zero.cc:1:
In constructor ‘std::__detail::_Executor< <template-parameter-1-1>,
<template-parameter-1-2>, <template-parameter-1-3>, <anonymous>
>::_State_info<std::integral_constant<bool, false>,
_ResultsVec>::_State_info(std::__detail::_StateIdT, std::size_t) [with
_ResultsVec = std::vector<std::__cxx11::sub_match<const char*>,
std::allocator<std::__cxx11::sub_match<const char*> > >; _BiIter = const char*;
_Alloc = std::allocator<std::__cxx11::sub_match<const char*> >; _TraitsT =
std::__cxx11::regex_traits<char>; bool __dfs_mode = false]’,
    inlined from ‘std::__detail::_Executor< <template-parameter-1-1>,
<template-parameter-1-2>, <template-parameter-1-3>, <anonymous>
>::_Executor(_BiIter, _BiIter, _ResultsVec&, const _RegexT&, _FlagT) [with
_BiIter = const char*; _Alloc = std::allocator<std::__cxx11::sub_match<const
char*> >; _TraitsT = std::__cxx11::regex_traits<char>; bool __dfs_mode =
false]’ at /usr/include/c++/14.2.1/bits/regex_executor.h:81:2,
    inlined from ‘bool std::__detail::_Executor< <template-parameter-1-1>,
<template-parameter-1-2>, <template-parameter-1-3>, <anonymous>
>::_M_lookahead(std::__detail::_StateIdT) [with _BiIter = const char*; _Alloc =
std::allocator<std::__cxx11::sub_match<const char*> >; _TraitsT =
std::__cxx11::regex_traits<char>; bool __dfs_mode = false]’ at
/usr/include/c++/14.2.1/bits/regex_executor.tcc:155:17:
/usr/include/c++/14.2.1/bits/regex_executor.h:244:31: error: argument 1 value
is zero [-Werror=alloc-zero]
  244 |           : _M_visited_states(new bool[__n]()), _M_start(__start)
      |                               ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14.2.1/bits/new_allocator.h:34,
                 from
/usr/include/c++/14.2.1/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /usr/include/c++/14.2.1/bits/allocator.h:46,
                 from /usr/include/c++/14.2.1/string:43,
                 from /usr/include/c++/14.2.1/bitset:52,
                 from /usr/include/c++/14.2.1/regex:40:
/usr/include/c++/14.2.1/new: In member function ‘bool std::__detail::_Executor<
<template-parameter-1-1>, <template-parameter-1-2>, <template-parameter-1-3>,
<anonymous> >::_M_lookahead(std::__detail::_StateIdT) [with _BiIter = const
char*; _Alloc = std::allocator<std::__cxx11::sub_match<const char*> >; _TraitsT
= std::__cxx11::regex_traits<char>; bool __dfs_mode = false]’:
/usr/include/c++/14.2.1/new:133:25: note: in a call to allocation function
‘void* operator new [](std::size_t)’ declared here
  133 | _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW
(std::bad_alloc)
      |                         ^~~~~~~~
cc1plus: all warnings being treated as errors

Reply via email to