re PR libstdc++/9561 (ostream inserters rethrow exception of wrong type)

2003-03-08  Jerry Quinn  <jlquinn@optonline.net>

	PR libstdc++/9561
	* include/bits/basic_ios.h (_M_setstate): New.
	* include/bits/ostream.tcc (operator<<): Use it.
	* include/bits/istream.tcc (operator>>): Use it.
	* include/std/std_ostream.h (operator<<): Make friends.
	* include/std/std_istream.h (operator>>): Make friends.
	* testsuite/27_io/ostream_exception.cc,
	testsuite/27_io/istream_exception.cc: New tests.

From-SVN: r64021
This commit is contained in:
Jerry Quinn 2003-03-09 04:19:52 +00:00 committed by Jerry Quinn
parent 4f4ebda329
commit 6f62fbd81a
8 changed files with 223 additions and 45 deletions

View File

@ -1,3 +1,14 @@
2003-03-08 Jerry Quinn <jlquinn@optonline.net>
PR libstdc++/9561
* include/bits/basic_ios.h (_M_setstate): New.
* include/bits/ostream.tcc (operator<<): Use it.
* include/bits/istream.tcc (operator>>): Use it.
* include/std/std_ostream.h (operator<<): Make friends.
* include/std/std_istream.h (operator>>): Make friends.
* testsuite/27_io/ostream_exception.cc,
testsuite/27_io/istream_exception.cc: New tests.
2003-03-08 Benjamin Kosnik <bkoz@redhat.com>
* include/bits/locale_facets.tcc: Fix typo.

View File

@ -441,6 +441,11 @@ namespace std
void
_M_cache_locale(const locale& __loc);
// Internal state setter that won't throw, only set the state bits.
// Used to guarantee we don't throw when setting badbit.
void
_M_setstate(iostate __state) { _M_streambuf_state |= __state; }
};
} // namespace std

View File

@ -122,7 +122,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -159,7 +159,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -186,7 +186,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -223,7 +223,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -250,7 +250,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -277,7 +277,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -304,7 +304,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -332,7 +332,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -359,7 +359,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -387,7 +387,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -414,7 +414,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -441,7 +441,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -468,7 +468,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -499,7 +499,7 @@ namespace std
{
// 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -531,7 +531,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -565,7 +565,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -604,7 +604,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -647,7 +647,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -698,7 +698,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -739,7 +739,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -763,7 +763,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -790,7 +790,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -826,7 +826,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -856,7 +856,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -887,7 +887,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -922,7 +922,7 @@ namespace std
{
// 27.6.1.3 paragraph 1
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -998,7 +998,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__in.setstate(ios_base::badbit);
__in._M_setstate(ios_base::badbit);
if ((__in.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -1055,7 +1055,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__in.setstate(ios_base::badbit);
__in._M_setstate(ios_base::badbit);
if ((__in.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}

View File

@ -70,7 +70,7 @@ namespace std
{
// 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -92,7 +92,7 @@ namespace std
{
// 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -114,7 +114,7 @@ namespace std
{
// 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -138,7 +138,7 @@ namespace std
{
// 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -166,7 +166,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -205,7 +205,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -231,7 +231,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -272,7 +272,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -298,7 +298,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -325,7 +325,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -351,7 +351,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -377,7 +377,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
this->_M_setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -499,7 +499,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit);
__out._M_setstate(ios_base::badbit);
if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -535,7 +535,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit);
__out._M_setstate(ios_base::badbit);
if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -570,7 +570,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit);
__out._M_setstate(ios_base::badbit);
if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -619,7 +619,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit);
__out._M_setstate(ios_base::badbit);
if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
@ -658,7 +658,7 @@ namespace std
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit);
__out._M_setstate(ios_base::badbit);
if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}

View File

@ -73,6 +73,14 @@ namespace std
typedef num_get<_CharT, __istreambuf_iter> __numget_type;
typedef ctype<_CharT> __ctype_type;
template<typename _CharT2, typename _Traits2>
friend basic_istream<_CharT2, _Traits2>&
operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2&);
template<typename _CharT2, typename _Traits2>
friend basic_istream<_CharT2, _Traits2>&
operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
protected:
// Data Members:
/**

View File

@ -73,6 +73,26 @@ namespace std
typedef num_put<_CharT, __ostreambuf_iter> __numput_type;
typedef ctype<_CharT> __ctype_type;
template<typename _CharT2, typename _Traits2>
friend basic_ostream<_CharT2, _Traits2>&
operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2);
template<typename _Traits2>
friend basic_ostream<char, _Traits2>&
operator<<(basic_ostream<char, _Traits2>&, char);
template<typename _CharT2, typename _Traits2>
friend basic_ostream<_CharT2, _Traits2>&
operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*);
template<typename _Traits2>
friend basic_ostream<char, _Traits2>&
operator<<(basic_ostream<char, _Traits2>&, const char*);
template<typename _CharT2, typename _Traits2>
friend basic_ostream<_CharT2, _Traits2>&
operator<<(basic_ostream<_CharT2, _Traits2>&, const char*);
// [27.6.2.2] constructor/destructor
/**
* @brief Base constructor.

View File

@ -0,0 +1,68 @@
// 2003-03-08 Jerry Quinn <jlquinn@optonline.net>
// Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
#include <istream>
#include <streambuf>
#include <testsuite_hooks.h>
// libstdc++/9561
struct foobar: std::exception { };
struct buf: std::streambuf
{
virtual int_type underflow () {
throw foobar ();
return -1;
}
virtual int_type uflow () {
throw foobar ();
return -1;
}
};
void test01()
{
using namespace std;
buf b;
std::istream strm (&b);
strm.exceptions (std::ios::badbit);
int i = 0;
try {
i = strm.get();
}
catch (foobar) {
// strm should throw foobar and not do anything else
VERIFY(strm.bad());
}
catch (...) {
VERIFY(false);
}
VERIFY(i == 0);
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,66 @@
// 2003-03-08 Jerry Quinn <jlquinn@optonline.net>
// Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
#include <ostream>
#include <streambuf>
#include <testsuite_hooks.h>
// libstdc++/9561
struct foobar: std::exception { };
struct buf: std::streambuf
{
virtual int_type overflow (int_type) {
throw foobar ();
return -1;
}
};
void test01()
{
using namespace std;
bool test = true;
buf b;
std::ostream strm (&b);
strm.exceptions (std::ios::badbit);
try {
strm << std::endl;
}
catch (foobar) {
// strm should throw foobar and not do anything else
VERIFY(strm.bad());
return;
}
catch (...) {
VERIFY(false);
return;
}
VERIFY(false);
}
int main()
{
test01();
return 0;
}