PR libstdc++/53984 handle exceptions in basic_istream::sentry
authorJonathan Wakely <jwakely@redhat.com>
Tue, 25 Jul 2017 20:36:06 +0000 (21:36 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 25 Jul 2017 20:36:06 +0000 (21:36 +0100)
PR libstdc++/53984
* include/bits/basic_ios.h (basic_ios::_M_setstate): Adjust comment.
* include/bits/istream.tcc (basic_istream::sentry): Handle exceptions
during construction.
* include/std/istream: Adjust comments for formatted input functions
and unformatted input functions.
* testsuite/27_io/basic_fstream/53984.cc: New.
* testsuite/27_io/basic_istream/sentry/char/53984.cc: New.

From-SVN: r250545

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_ios.h
libstdc++-v3/include/bits/istream.tcc
libstdc++-v3/include/std/istream
libstdc++-v3/testsuite/27_io/basic_fstream/53984.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_istream/sentry/char/53984.cc [new file with mode: 0644]

index 6e5d2f988a8bfbfc0c695803804221b264f10196..7c064b9527e70c724cbc137a8987d9676b28f0b0 100644 (file)
@@ -1,5 +1,14 @@
 2017-07-25  Jonathan Wakely  <jwakely@redhat.com>
 
+       PR libstdc++/53984
+       * include/bits/basic_ios.h (basic_ios::_M_setstate): Adjust comment.
+       * include/bits/istream.tcc (basic_istream::sentry): Handle exceptions
+       during construction.
+       * include/std/istream: Adjust comments for formatted input functions
+       and unformatted input functions.
+       * testsuite/27_io/basic_fstream/53984.cc: New.
+       * testsuite/27_io/basic_istream/sentry/char/53984.cc: New.
+
        * include/bits/ios_base.h (ios_base::io_state, ios_base::open_mode)
        (ios_base::seek_dir): Remove for C++17.
        * include/std/streambuf (basic_streambuf::stossc): Remove for C++17.
index 318e41b7c55e89e9d7ff8c0332fa5f74efbebc7f..f0b8682b4fe4ce0a7cceb62c74d78672ccb82226 100644 (file)
@@ -157,8 +157,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       setstate(iostate __state)
       { this->clear(this->rdstate() | __state); }
 
-      // Flip the internal state on for the proper state bits, then re
-      // throws the propagated exception if bit also set in
+      // Flip the internal state on for the proper state bits, then
+      // rethrows the propagated exception if bit also set in
       // exceptions().
       void
       _M_setstate(iostate __state)
index b390f743bba21d3916c432d436521ae0edf417a0..92df9d126ef0c361de540317b4503691b17245a9 100644 (file)
@@ -48,28 +48,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       ios_base::iostate __err = ios_base::goodbit;
       if (__in.good())
-       {
-         if (__in.tie())
-           __in.tie()->flush();
-         if (!__noskip && bool(__in.flags() & ios_base::skipws))
-           {
-             const __int_type __eof = traits_type::eof();
-             __streambuf_type* __sb = __in.rdbuf();
-             __int_type __c = __sb->sgetc();
-
-             const __ctype_type& __ct = __check_facet(__in._M_ctype);
-             while (!traits_type::eq_int_type(__c, __eof)
-                    && __ct.is(ctype_base::space, 
-                               traits_type::to_char_type(__c)))
-               __c = __sb->snextc();
+       __try
+         {
+           if (__in.tie())
+             __in.tie()->flush();
+           if (!__noskip && bool(__in.flags() & ios_base::skipws))
+             {
+               const __int_type __eof = traits_type::eof();
+               __streambuf_type* __sb = __in.rdbuf();
+               __int_type __c = __sb->sgetc();
+
+               const __ctype_type& __ct = __check_facet(__in._M_ctype);
+               while (!traits_type::eq_int_type(__c, __eof)
+                      && __ct.is(ctype_base::space,
+                                 traits_type::to_char_type(__c)))
+                 __c = __sb->snextc();
 
-             // _GLIBCXX_RESOLVE_LIB_DEFECTS
-             // 195. Should basic_istream::sentry's constructor ever
-             // set eofbit?
-             if (traits_type::eq_int_type(__c, __eof))
-               __err |= ios_base::eofbit;
-           }
-       }
+               // _GLIBCXX_RESOLVE_LIB_DEFECTS
+               // 195. Should basic_istream::sentry's constructor ever
+               // set eofbit?
+               if (traits_type::eq_int_type(__c, __eof))
+                 __err |= ios_base::eofbit;
+             }
+         }
+       __catch(__cxxabiv1::__forced_unwind&)
+         {
+           __in._M_setstate(ios_base::badbit);
+           __throw_exception_again;
+         }
+       __catch(...)
+         { __in._M_setstate(ios_base::badbit); }
 
       if (__in.good() && __err == ios_base::goodbit)
        _M_ok = true;
index 1fa25555771a46e41aa1937cee3c179dace50e17..233cc6b01458b73f289e797fb97be32a5ccd7836 100644 (file)
@@ -150,9 +150,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  whatever data is appropriate for the type of the argument.
        *
        *  If an exception is thrown during extraction, ios_base::badbit
-       *  will be turned on in the stream's error state without causing an
-       *  ios_base::failure to be thrown.  The original exception will then
-       *  be rethrown.
+       *  will be turned on in the stream's error state (without causing an
+       *  ios_base::failure to be thrown) and the original exception will
+       *  be rethrown if badbit is set in the exceptions mask.
       */
 
       //@{
@@ -286,9 +286,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  by gcount().
        *
        *  If an exception is thrown during extraction, ios_base::badbit
-       *  will be turned on in the stream's error state without causing an
-       *  ios_base::failure to be thrown.  The original exception will then
-       *  be rethrown.
+       *  will be turned on in the stream's error state (without causing an
+       *  ios_base::failure to be thrown) and the original exception will
+       *  be rethrown if badbit is set in the exceptions mask.
       */
 
       /**
diff --git a/libstdc++-v3/testsuite/27_io/basic_fstream/53984.cc b/libstdc++-v3/testsuite/27_io/basic_fstream/53984.cc
new file mode 100644 (file)
index 0000000..e84072e
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-require-file-io "" }
+
+#include <fstream>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::fstream in(".");
+  int x;
+  in >> x;
+  VERIFY( in.bad() );
+}
+
+int
+main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_istream/sentry/char/53984.cc b/libstdc++-v3/testsuite/27_io/basic_istream/sentry/char/53984.cc
new file mode 100644 (file)
index 0000000..9c08c72
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <streambuf>
+#include <istream>
+#include <testsuite_hooks.h>
+
+struct SB : std::streambuf
+{
+  virtual int_type underflow() { throw 1; }
+};
+
+void
+test01()
+{
+  SB sb;
+  std::istream is(&sb);
+  int i;
+  is >> i;
+  VERIFY( is.bad() );
+}
+
+int
+main()
+{
+  test01();
+}