Bug 112421 - GCC emits warning potential null dereference
Summary: GCC emits warning potential null dereference
Status: RESOLVED DUPLICATE of bug 105580
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wnull-dereference
  Show dependency treegraph
 
Reported: 2023-11-07 07:46 UTC by Jason Liam
Modified: 2023-11-07 11:38 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jason Liam 2023-11-07 07:46:25 UTC
GCC emits a false positive with the following code. https://godbolt.org/z/KojKWT4WK

```
std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());
```

It says:

```
In file included from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ios:45,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/ostream:40,
                 from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/iostream:41,
                 from <source>:1:
In member function 'std::basic_streambuf<_CharT, _Traits>::char_type* std::basic_streambuf<_CharT, _Traits>::egptr() 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-13.2.0/include/c++/13.2.0/streambuf:326:49,
    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-13.2.0/include/c++/13.2.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-13.2.0/include/c++/13.2.0/bits/basic_string.tcc:178: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-13.2.0/include/c++/13.2.0/bits/basic_string.h:753:16,
    inlined from 'int main()' at <source>:18:53:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/streambuf:495:30: warning: potential null pointer dereference [-Wnull-dereference]
  495 |       egptr() const { return _M_in_end; }
```
Comment 1 Jonathan Wakely 2023-11-07 11:38:31 UTC
Looks like a dup

*** This bug has been marked as a duplicate of bug 105580 ***