]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add #pragma to slience null-dereference warning in istreambuf_iterator...
authorTomasz Kamiński <tkaminsk@redhat.com>
Fri, 27 Feb 2026 19:14:44 +0000 (20:14 +0100)
committerTomasz Kamiński <tkaminsk@redhat.com>
Mon, 2 Mar 2026 10:21:51 +0000 (11:21 +0100)
The warning was produced by following sequence, given an istream_iterator<char>
it, such that *it will result in hitting EoF in it->_M_get(), and thus clearing
_M_sbuf, the subsequent call to ++it, will result in _M_sbuf->sbumpc() call on
null pointer. This is however an false-positive, as in such situation
it == istream_iteator() returns true, and the iterator should not be
incremented in first place.

This patch sliences the issue, by disabling the "-Wnull-dereference" using
GCC diagnostic pragmas. To work correctly the pragmas needs to be placed around
streambuf functions on which the issue originating from istreambuf_iterator are
reported.

PR libstdc++/105580

libstdc++-v3/ChangeLog:

* include/std/streambuf (streambuf::gptr, streambuf::egptr)
(streambuf::gbump): Surround with pragma disabling -Wnull-dereference.
* testsuite/24_iterators/istreambuf_iterator/105580.cc: Remove check for
warning being emitted.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/std/streambuf
libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/105580.cc

index d6036bd5ddc4091372c351e50db9b51707753164..f013aa06c28b7296bd26ec19432b0ffdf2f9ab34 100644 (file)
@@ -490,6 +490,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       char_type*
       eback() const { return _M_in_beg; }
 
+// Required to silence false-positive warnings originating from istream_iterator::operator++,
+// see PR105580.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wnull-dereference"
       char_type*
       gptr()  const { return _M_in_cur;  }
 
@@ -505,6 +509,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       */
       void
       gbump(int __n) { _M_in_cur += __n; }
+#pragma GCC diagnostic pop
 
       /**
        *  @brief  Setting the three read area pointers.
index 85f888b86e72d28a16eadd4c57e047200ff10229..87edf999ffcdf53ce876c69c853672611a3950ec 100644 (file)
@@ -1,5 +1,4 @@
 // { dg-compile }
-// { dg-require-normal-mode "" }
 // { dg-additional-options "-Wnull-dereference" }
 
 #include <string>
@@ -12,5 +11,4 @@ int main()
   std::string ss(it, end);
   return 0;
 }
-// { dg-warning ".*null pointer dereference" "" { target *-*-* } 0 }