diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c5a4dbc3f63..81ac17ed5d2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2003-03-08 Jerry Quinn + + 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 * include/bits/locale_facets.tcc: Fix typo. diff --git a/libstdc++-v3/include/bits/basic_ios.h b/libstdc++-v3/include/bits/basic_ios.h index 7d0e47ef3af..03b54b7198b 100644 --- a/libstdc++-v3/include/bits/basic_ios.h +++ b/libstdc++-v3/include/bits/basic_ios.h @@ -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 diff --git a/libstdc++-v3/include/bits/istream.tcc b/libstdc++-v3/include/bits/istream.tcc index 0bd7fff51da..68295b4c9f3 100644 --- a/libstdc++-v3/include/bits/istream.tcc +++ b/libstdc++-v3/include/bits/istream.tcc @@ -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; } diff --git a/libstdc++-v3/include/bits/ostream.tcc b/libstdc++-v3/include/bits/ostream.tcc index a60dd720739..64b37a48826 100644 --- a/libstdc++-v3/include/bits/ostream.tcc +++ b/libstdc++-v3/include/bits/ostream.tcc @@ -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; } diff --git a/libstdc++-v3/include/std/std_istream.h b/libstdc++-v3/include/std/std_istream.h index aab3db3d7c2..fa97f722093 100644 --- a/libstdc++-v3/include/std/std_istream.h +++ b/libstdc++-v3/include/std/std_istream.h @@ -73,6 +73,14 @@ namespace std typedef num_get<_CharT, __istreambuf_iter> __numget_type; typedef ctype<_CharT> __ctype_type; + template + friend basic_istream<_CharT2, _Traits2>& + operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2&); + + template + friend basic_istream<_CharT2, _Traits2>& + operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*); + protected: // Data Members: /** diff --git a/libstdc++-v3/include/std/std_ostream.h b/libstdc++-v3/include/std/std_ostream.h index 9f7c386f181..103b3fd7299 100644 --- a/libstdc++-v3/include/std/std_ostream.h +++ b/libstdc++-v3/include/std/std_ostream.h @@ -73,6 +73,26 @@ namespace std typedef num_put<_CharT, __ostreambuf_iter> __numput_type; typedef ctype<_CharT> __ctype_type; + template + friend basic_ostream<_CharT2, _Traits2>& + operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2); + + template + friend basic_ostream& + operator<<(basic_ostream&, char); + + template + friend basic_ostream<_CharT2, _Traits2>& + operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*); + + template + friend basic_ostream& + operator<<(basic_ostream&, const char*); + + template + friend basic_ostream<_CharT2, _Traits2>& + operator<<(basic_ostream<_CharT2, _Traits2>&, const char*); + // [27.6.2.2] constructor/destructor /** * @brief Base constructor. diff --git a/libstdc++-v3/testsuite/27_io/istream_exception.cc b/libstdc++-v3/testsuite/27_io/istream_exception.cc new file mode 100644 index 00000000000..2c3b1217f2a --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/istream_exception.cc @@ -0,0 +1,68 @@ +// 2003-03-08 Jerry Quinn + +// 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 +#include +#include + +// 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; +} diff --git a/libstdc++-v3/testsuite/27_io/ostream_exception.cc b/libstdc++-v3/testsuite/27_io/ostream_exception.cc new file mode 100644 index 00000000000..8c2b3407056 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/ostream_exception.cc @@ -0,0 +1,66 @@ +// 2003-03-08 Jerry Quinn + +// 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 +#include +#include + +// 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; +}