https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115067
Bug ID: 115067 Summary: Bogus -O2 -Wnull-dereference for istreambuf_iterator Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hewillk at gmail dot com Target Milestone: --- #include <sstream> #include <iterator> #include <iostream> std::string fn() { std::istringstream is {"bob"}; auto beg = std::istreambuf_iterator<char>(is); auto end = std::istreambuf_iterator<char>(); return {beg, end}; } int main() { std::cout << fn() << "\n"; } https://godbolt.org/z/rGqW5he87 The above code always triggers the following warning when -O2 -Wnull-dereference is used since GCC-12: /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/streambuf:495:30: error: potential null pointer dereference [-Werror=null-dereference] 495 | egptr() const { return _M_in_end; } | ^~~~~~~~~ In member function 'std::basic_streambuf<_CharT, _Traits>::char_type* std::basic_streambuf<_CharT, _Traits>::gptr() const [with _CharT = char; _Traits = std::char_traits<char>]', inlined from 'std::basic_streambuf<_CharT, _Traits>::int_type std::basic_streambuf<_CharT, _Traits>::sbumpc() [with _CharT = char; _Traits = std::char_traits<char>]' at /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/streambuf:326:33, inlined from 'std::istreambuf_iterator<_CharT, _Traits>& std::istreambuf_iterator<_CharT, _Traits>::operator++() [with _CharT = char; _Traits = std::char_traits<char>]' at /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/bits/streambuf_iterator.h:173:17, inlined from 'void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::input_iterator_tag) [with _InIterator = std::istreambuf_iterator<char, std::char_traits<char> >; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/bits/basic_string.tcc:182:6, inlined from 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::istreambuf_iterator<char, std::char_traits<char> >; <template-parameter-2-2> = void; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/bits/basic_string.h:770:16, inlined from 'std::string fn()' at <source>:9:19: /opt/compiler-explorer/gcc-trunk-20240513/include/c++/15.0.0/streambuf:492:30: error: potential null pointer dereference [-Werror=null-dereference] 492 | gptr() const { return _M_in_cur; }