This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Fix a (long standing) bug in stringbuf::pbackfail, add tests


Hi,

tested x86/x86_64-linux, committed to mainline.

Paolo.

///////////////
2004-10-01  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/sstream.tcc (pbackfail): Implement correctly
	27.7.1.3/2, bullet 2: if mode & ios_base::out is false do not
	write in the buffer.
	* testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc: New.
	* testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc: Likewise.
	* testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc: Likewise.
	* testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc: Likewise.
	* testsuite/27_io/basic_istream/putback/char/1.cc: Tweak consistently.
diff -prN libstdc++-v3-orig/include/bits/sstream.tcc libstdc++-v3/include/bits/sstream.tcc
*** libstdc++-v3-orig/include/bits/sstream.tcc	Fri Oct  1 09:56:36 2004
--- libstdc++-v3/include/bits/sstream.tcc	Fri Oct  1 17:49:15 2004
*************** namespace std
*** 47,70 ****
      pbackfail(int_type __c)
      {
        int_type __ret = traits_type::eof();
-       const bool __testeof = traits_type::eq_int_type(__c, __ret);
- 
        if (this->eback() < this->gptr())
  	{
- 	  const bool __testeq = traits_type::eq(traits_type::to_char_type(__c),
- 						this->gptr()[-1]);
- 	  this->gbump(-1);
- 
  	  // Try to put back __c into input sequence in one of three ways.
  	  // Order these tests done in is unspecified by the standard.
! 	  if (!__testeof && __testeq)
! 	    __ret = __c;
! 	  else if (__testeof)
! 	    __ret = traits_type::not_eof(__c);
  	  else
  	    {
! 	      *this->gptr() = traits_type::to_char_type(__c);
! 	      __ret = __c;
  	    }
  	}
        return __ret;
--- 47,75 ----
      pbackfail(int_type __c)
      {
        int_type __ret = traits_type::eof();
        if (this->eback() < this->gptr())
  	{
  	  // Try to put back __c into input sequence in one of three ways.
  	  // Order these tests done in is unspecified by the standard.
! 	  const bool __testeof = traits_type::eq_int_type(__c, __ret);
! 	  if (!__testeof)
! 	    {
! 	      const bool __testeq = traits_type::eq(traits_type::
! 						    to_char_type(__c),
! 						    this->gptr()[-1]);	  
! 	      const bool __testout = this->_M_mode & ios_base::out;
! 	      if (__testeq || __testout)
! 		{
! 		  this->gbump(-1);
! 		  if (!__testeq)
! 		    *this->gptr() = traits_type::to_char_type(__c);
! 		  __ret = __c;
! 		}
! 	    }
  	  else
  	    {
! 	      this->gbump(-1);
! 	      __ret = traits_type::not_eof(__c);
  	    }
  	}
        return __ret;
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_istream/putback/char/1.cc libstdc++-v3/testsuite/27_io/basic_istream/putback/char/1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_istream/putback/char/1.cc	Tue Jan  6 13:20:40 2004
--- libstdc++-v3/testsuite/27_io/basic_istream/putback/char/1.cc	Fri Oct  1 18:20:00 2004
*************** test01()
*** 49,59 ****
    is_04.ignore(30);
    is_04.clear();
    state1 = is_04.rdstate();
!   is_04.putback('|');
    VERIFY( is_04.gcount() == 0 );  // DR 60
    state2 = is_04.rdstate();
    VERIFY( state1 == state2 );
!   VERIFY( is_04.peek() == '|' );
  
    // istream& unget()
    is_04.clear();
--- 49,59 ----
    is_04.ignore(30);
    is_04.clear();
    state1 = is_04.rdstate();
!   is_04.putback('t');
    VERIFY( is_04.gcount() == 0 );  // DR 60
    state2 = is_04.rdstate();
    VERIFY( state1 == state2 );
!   VERIFY( is_04.peek() == 't' );
  
    // istream& unget()
    is_04.clear();
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/1.cc	Fri Oct  1 22:24:21 2004
***************
*** 0 ****
--- 1,76 ----
+ // 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+ 
+ // Copyright (C) 2004 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.
+ 
+ // 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+ 
+ #include <sstream>
+ #include <testsuite_hooks.h>
+ 
+ class my_stringbuf : public std::stringbuf
+ {
+ public:
+   my_stringbuf(const std::string& str, std::ios_base::openmode mode)
+   : std::stringbuf(str, mode) { }
+ 
+   int_type 
+   pub_pbackfail(int_type c) 
+   { return this->pbackfail(c); }
+ };
+ 
+ void test01()
+ {
+   bool test __attribute__((unused)) = true;
+   using namespace std;
+ 
+   typedef my_stringbuf::int_type    int_type;
+   typedef my_stringbuf::traits_type traits_type;
+ 
+   my_stringbuf sbuf("any", ios_base::in | ios_base::out);
+ 
+   int_type c = sbuf.sgetc();
+   VERIFY( c == 'a' );
+ 
+   c = sbuf.pub_pbackfail('z');
+   VERIFY( c == traits_type::eof() );
+   c = sbuf.sbumpc();
+   VERIFY( c == 'a' );
+  
+   c = sbuf.pub_pbackfail('a');
+   VERIFY( c == 'a' );
+   c = sbuf.sbumpc();
+   VERIFY( c == 'a' );
+   
+   c = sbuf.pub_pbackfail('x');
+   VERIFY( c == 'x' );
+   c = sbuf.sbumpc();
+   VERIFY( c == 'x' );
+ 
+   const int_type eof = traits_type::eof();
+   c = sbuf.pub_pbackfail(eof);
+   VERIFY( c == traits_type::not_eof(eof) );
+   c = sbuf.sgetc();
+   VERIFY( c == 'x' );
+ }
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/char/2.cc	Fri Oct  1 18:40:40 2004
***************
*** 0 ****
--- 1,64 ----
+ // 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+ 
+ // Copyright (C) 2004 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.
+ 
+ // 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+ 
+ #include <sstream>
+ #include <testsuite_hooks.h>
+ 
+ class my_stringbuf : public std::stringbuf
+ {
+ public:
+   my_stringbuf(const std::string& str, std::ios_base::openmode mode)
+   : std::stringbuf(str, mode) { }
+ 
+   int_type 
+   pub_pbackfail(int_type __c) 
+   { return this->pbackfail(__c); }
+ };
+ 
+ // We weren't enforcing 27.7.1.3/2, bullet 2: "... and if
+ // mode & ios_base::out is nonzero, ..."
+ void test01()
+ {
+   bool test __attribute__((unused)) = true;
+   using namespace std;
+ 
+   typedef my_stringbuf::int_type    int_type;
+   typedef my_stringbuf::traits_type traits_type;
+ 
+   my_stringbuf sbuf("any", ios_base::in);
+   
+   int_type c = sbuf.sbumpc();
+   VERIFY( c == 'a' );
+ 
+   c = sbuf.pub_pbackfail('x');
+   VERIFY( c == traits_type::eof() );
+   VERIFY( sbuf.str() == "any" );
+   c = sbuf.sgetc();
+   VERIFY( c == 'n' );
+ }
+ 
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/1.cc	Fri Oct  1 22:43:35 2004
***************
*** 0 ****
--- 1,76 ----
+ // 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+ 
+ // Copyright (C) 2004 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.
+ 
+ // 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+ 
+ #include <sstream>
+ #include <testsuite_hooks.h>
+ 
+ class my_stringbuf : public std::wstringbuf
+ {
+ public:
+   my_stringbuf(const std::wstring& str, std::ios_base::openmode mode)
+   : std::wstringbuf(str, mode) { }
+ 
+   int_type 
+   pub_pbackfail(int_type c) 
+   { return this->pbackfail(c); }
+ };
+ 
+ void test01()
+ {
+   bool test __attribute__((unused)) = true;
+   using namespace std;
+ 
+   typedef my_stringbuf::int_type    int_type;
+   typedef my_stringbuf::traits_type traits_type;
+ 
+   my_stringbuf sbuf(L"any", ios_base::in | ios_base::out);
+   
+   int_type c = sbuf.sgetc();
+   VERIFY( c == L'a' );
+ 
+   c = sbuf.pub_pbackfail(L'z');
+   VERIFY( c == traits_type::eof() );
+   c = sbuf.sbumpc();
+   VERIFY( c == L'a' );
+  
+   c = sbuf.pub_pbackfail(L'a');
+   VERIFY( c == L'a' );
+   c = sbuf.sbumpc();
+   VERIFY( c == L'a' );
+   
+   c = sbuf.pub_pbackfail(L'x');
+   VERIFY( c == L'x' );
+   c = sbuf.sbumpc();
+   VERIFY( c == L'x' );
+ 
+   const int_type eof = traits_type::eof();
+   c = sbuf.pub_pbackfail(eof);
+   VERIFY( c == traits_type::not_eof(eof) );
+   c = sbuf.sgetc();
+   VERIFY( c == L'x' );
+ }
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_stringbuf/pbackfail/wchar_t/2.cc	Fri Oct  1 18:42:58 2004
***************
*** 0 ****
--- 1,64 ----
+ // 2004-10-01  Paolo Carlini  <pcarlini@suse.de>
+ 
+ // Copyright (C) 2004 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.
+ 
+ // 27.7.1.3  Overridden virtual functions  [lib.stringbuf.virtuals]
+ 
+ #include <sstream>
+ #include <testsuite_hooks.h>
+ 
+ class my_stringbuf : public std::wstringbuf
+ {
+ public:
+   my_stringbuf(const std::wstring& str, std::ios_base::openmode mode)
+   : std::wstringbuf(str, mode) { }
+ 
+   int_type 
+   pub_pbackfail(int_type __c) 
+   { return this->pbackfail(__c); }
+ };
+ 
+ // We weren't enforcing 27.7.1.3/2, bullet 2: "... and if
+ // mode & ios_base::out is nonzero, ..."
+ void test01()
+ {
+   bool test __attribute__((unused)) = true;
+   using namespace std;
+ 
+   typedef my_stringbuf::int_type    int_type;
+   typedef my_stringbuf::traits_type traits_type;
+ 
+   my_stringbuf sbuf(L"any", ios_base::in);
+   
+   int_type c = sbuf.sbumpc();
+   VERIFY( c == L'a' );
+ 
+   c = sbuf.pub_pbackfail(L'x');
+   VERIFY( c == traits_type::eof() );
+   VERIFY( sbuf.str() == L"any" );
+   c = sbuf.sgetc();
+   VERIFY( c == L'n' );
+ }
+ 
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]