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