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 libstdc++/13858


Hi,

I'm committing the below to mainline and 3_4.

Tested x86-linux.

Paolo.

//////////////
2004-02-14  Paolo Carlini  <pcarlini@suse.de>

	PR libstdc++/13858
	* include/bits/fstream.tcc (basic_filebuf<>::_M_convert_to_external):
	In case of conversion errors, throw ios_failure; simplify.
	* testsuite/27_io/basic_filebuf/overflow/char/13858.cc: New.
	* testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc: Ditto.
	* testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc: Tweak,
	previously we didn't throw in case of conversion errors, instead
	just returned eof().
	* testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc: Ditto.
	* testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc: Ditto.
	* testsuite/27_io/basic_filebuf/sync/char/9182-1.cc: Ditto.

	* include/bits/fstream.tcc (basic_filebuf<>::overflow):
	Trivial simplification of a conditional.
diff -prN libstdc++-v3-orig/include/bits/fstream.tcc libstdc++-v3/include/bits/fstream.tcc
*** libstdc++-v3-orig/include/bits/fstream.tcc	Sun Feb  8 05:46:40 2004
--- libstdc++-v3/include/bits/fstream.tcc	Fri Feb 13 17:49:57 2004
*************** namespace std
*** 397,403 ****
  	      // and output.
  	      if (_M_convert_to_external(this->pbase(),
  					 this->pptr() - this->pbase())
! 		  && (!__testeof || (__testeof && !_M_file.sync())))
  		{
  		  _M_set_buffer(0);
  		  __ret = traits_type::not_eof(__c);
--- 397,403 ----
  	      // and output.
  	      if (_M_convert_to_external(this->pbase(),
  					 this->pptr() - this->pbase())
! 		  && (!__testeof || !_M_file.sync()))
  		{
  		  _M_set_buffer(0);
  		  __ret = traits_type::not_eof(__c);
*************** namespace std
*** 437,448 ****
      _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
      {
        // Sizes of external and pending output.
!       streamsize __elen = 0;
!       streamsize __plen = 0;
        if (__check_facet(_M_codecvt).always_noconv())
  	{
! 	  __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
! 	  __plen += __ilen;
  	}
        else
  	{
--- 437,448 ----
      _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
      {
        // Sizes of external and pending output.
!       streamsize __elen;
!       streamsize __plen;
        if (__check_facet(_M_codecvt).always_noconv())
  	{
! 	  __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
! 	  __plen = __ilen;
  	}
        else
  	{
*************** namespace std
*** 466,484 ****
  	      __blen = __ilen;
  	    }
  	  else
! 	    {
! 	      // Result == error.
! 	      __blen = 0;
! 	    }
! 
! 	  if (__blen)
! 	    {
! 	      __elen += _M_file.xsputn(__buf, __blen);
! 	      __plen += __blen;
! 	    }
  
  	  // Try once more for partial conversions.
! 	  if (__r == codecvt_base::partial)
  	    {
  	      const char_type* __iresume = __iend;
  	      streamsize __rlen = this->pptr() - __iend;
--- 466,479 ----
  	      __blen = __ilen;
  	    }
  	  else
! 	    __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
! 				    "conversion error"));
!   
! 	  __elen = _M_file.xsputn(__buf, __blen);
! 	  __plen = __blen;
  
  	  // Try once more for partial conversions.
! 	  if (__r == codecvt_base::partial && __elen == __plen)
  	    {
  	      const char_type* __iresume = __iend;
  	      streamsize __rlen = this->pptr() - __iend;
*************** namespace std
*** 488,499 ****
  	      if (__r != codecvt_base::error)
  		{
  		  __rlen = __bend - __buf;
! 		  __elen += _M_file.xsputn(__buf, __rlen);
! 		  __plen += __rlen;
  		}
  	    }
  	}
!       return __elen && __elen == __plen;
      }
  
     template<typename _CharT, typename _Traits>
--- 483,497 ----
  	      if (__r != codecvt_base::error)
  		{
  		  __rlen = __bend - __buf;
! 		  __elen = _M_file.xsputn(__buf, __rlen);
! 		  __plen = __rlen;
  		}
+ 	      else
+ 		__throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
+ 					"conversion error"));
  	    }
  	}
!       return __elen == __plen;
      }
  
     template<typename _CharT, typename _Traits>
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/char/13858.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/13858.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/char/13858.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/13858.cc	Fri Feb 13 17:25:59 2004
***************
*** 0 ****
--- 1,70 ----
+ // 2004-02-14  Petur Runolfsson  <peturr02@ru.is>
+ 
+ // 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.8.1.4 Overridden virtual functions
+ 
+ #include <fstream>
+ #include <locale>
+ 
+ class Cvt : public std::codecvt<char, char, std::mbstate_t>
+ {
+ protected:
+   virtual std::codecvt_base::result
+   do_out(std::mbstate_t&, const char* from, const char*,
+ 	 const char*& from_next, char* to, char*, char*& to_next) const
+   {
+     from_next = from;
+     to_next = to;
+     return std::codecvt_base::error;
+   }
+ 
+   virtual bool
+   do_always_noconv() const throw()
+   { return false; }
+ };
+ 
+ // libstdc++/13858
+ void test01()
+ {
+   using namespace std;
+   
+   filebuf fb;
+   fb.pubimbue(locale(locale::classic(), new Cvt));
+   fb.open("tmp_13858_char", ios_base::out);
+   
+   try
+     {
+       fb.sputc('a');
+       fb.sputc('b');
+       fb.pubimbue(locale::classic());
+       fb.sputc('c');
+       fb.pubsync();
+       fb.close();
+     }
+   catch (exception&)
+     {
+     }
+ }
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc	Tue Sep 23 22:02:56 2003
--- libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc	Fri Feb 13 17:29:50 2004
***************
*** 1,6 ****
  // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
  
! // Copyright (C) 2001, 2002, 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
--- 1,6 ----
  // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
  
! // Copyright (C) 2001, 2002, 2003, 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
***************
*** 22,28 ****
  
  #include <fstream>
  #include <locale>
- #include <testsuite_hooks.h>
  
  const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
  
--- 22,27 ----
*************** protected:
*** 51,57 ****
  void test14()
  {
    using namespace std;
-   bool test __attribute__((unused)) = true;
    
    locale loc =  locale::classic();
    loc = locale(loc, new errorcvt);
--- 50,55 ----
*************** void test14()
*** 60,68 ****
    fbuf1.pubimbue(loc);
    fbuf1.pubsetbuf(0, 0);
    fbuf1.open(name_07, ios_base::out | ios_base::trunc);
!   streamsize n = fbuf1.sputn("onne", 4);
!   VERIFY( n == 0 );
!   fbuf1.close();
  }
  
  int main() 
--- 58,72 ----
    fbuf1.pubimbue(loc);
    fbuf1.pubsetbuf(0, 0);
    fbuf1.open(name_07, ios_base::out | ios_base::trunc);
! 
!   try
!     {
!       fbuf1.sputn("onne", 4);
!       fbuf1.close();
!     }
!   catch (exception&)
!     {
!     }
  }
  
  int main() 
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc	Thu Jan  1 01:00:00 1970
--- libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc	Fri Feb 13 17:26:13 2004
***************
*** 0 ****
--- 1,71 ----
+ // 2004-02-14  Petur Runolfsson  <peturr02@ru.is>
+ 
+ // 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.8.1.4 Overridden virtual functions
+ 
+ #include <fstream>
+ #include <locale>
+ 
+ class Cvt : public std::codecvt<wchar_t, char, std::mbstate_t>
+ {
+ protected:
+   virtual std::codecvt_base::result
+   do_out(std::mbstate_t&, const wchar_t* from, const wchar_t*,
+ 	 const wchar_t*& from_next, char* to, char*,
+ 	 char*& to_next) const
+   {
+     from_next = from;
+     to_next = to;
+     return std::codecvt_base::error;
+   }
+   
+   virtual bool
+   do_always_noconv() const throw()
+   { return false; }
+ };
+ 
+ // libstdc++/13858
+ void test01()
+ {
+   using namespace std;
+   
+   wfilebuf fb;
+   fb.pubimbue(locale(locale::classic(), new Cvt));
+   fb.open("tmp_13858_wchar_t", ios_base::out);
+   
+   try
+     {
+       fb.sputc(L'a');
+       fb.sputc(L'b');
+       fb.pubimbue(locale::classic());
+       fb.sputc(L'c');
+       fb.pubsync();
+       fb.close();
+     }
+   catch (exception&)
+     {
+     }
+ }
+ 
+ int main()
+ {
+   test01();
+   return 0;
+ }
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc	Tue Nov  4 06:27:39 2003
--- libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc	Fri Feb 13 17:32:23 2004
***************
*** 1,4 ****
! // 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
--- 1,4 ----
! // Copyright (C) 2003, 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
***************
*** 19,31 ****
  // 27.8.1.4 Overridden virtual functions
  
  #include <fstream>
- #include <testsuite_hooks.h>
  
  void test03()
  {
    using namespace std;
  
-   bool test __attribute__((unused)) = true;
    const char* name = "tmp_seekoff_3";
  
    wfilebuf fb;
--- 19,29 ----
*************** void test03()
*** 33,42 ****
    fb.open(name, ios_base::out);
    fb.sputc(0xf001);
  
!   // seekoff should flush the output sequence, which will fail
!   // if the output buffer contains illegal characters.
!   streampos ret = fb.pubseekoff(0, ios_base::cur);
!   VERIFY( ret == streampos(streamoff(-1)) );
  }
  
  int main()
--- 31,45 ----
    fb.open(name, ios_base::out);
    fb.sputc(0xf001);
  
!   try
!     {
!       // seekoff should flush the output sequence, which will fail
!       // if the output buffer contains illegal characters.
!       fb.pubseekoff(0, ios_base::cur);
!     }
!   catch (exception&)
!     {
!     }
  }
  
  int main()
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc libstdc++-v3/testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc	Tue Nov  4 06:27:40 2003
--- libstdc++-v3/testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc	Fri Feb 13 17:33:31 2004
***************
*** 1,4 ****
! // 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
--- 1,4 ----
! // Copyright (C) 2003, 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
***************
*** 20,32 ****
  
  #include <locale>
  #include <fstream>
- #include <testsuite_hooks.h>
  
  void test01()
  {
    using namespace std;
  
-   bool test __attribute__((unused)) = true;
    const char* name = "tmp_seekpos_1";
  
    wfilebuf fb;
--- 20,30 ----
*************** void test01()
*** 35,42 ****
    streampos pos = fb.pubseekoff(0, ios_base::beg);
    fb.sputc(0xf001);
  
!   streampos ret = fb.pubseekpos(pos);
!   VERIFY( ret == streampos(streamoff(-1)) );
  }
  
  int main()
--- 33,45 ----
    streampos pos = fb.pubseekoff(0, ios_base::beg);
    fb.sputc(0xf001);
  
!   try
!     {
!       fb.pubseekpos(pos);
!     }
!   catch (exception&)
!     {
!     }
  }
  
  int main()
diff -prN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/sync/char/9182-1.cc libstdc++-v3/testsuite/27_io/basic_filebuf/sync/char/9182-1.cc
*** libstdc++-v3-orig/testsuite/27_io/basic_filebuf/sync/char/9182-1.cc	Tue Sep 23 22:03:02 2003
--- libstdc++-v3/testsuite/27_io/basic_filebuf/sync/char/9182-1.cc	Fri Feb 13 17:30:43 2004
***************
*** 1,6 ****
  // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
  
! // Copyright (C) 2001, 2002, 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
--- 1,6 ----
  // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
  
! // Copyright (C) 2001, 2002, 2003, 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
***************
*** 22,28 ****
  
  #include <fstream>
  #include <locale>
- #include <testsuite_hooks.h>
  
  const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
  
--- 22,27 ----
*************** protected:
*** 51,57 ****
  void test13()
  {
    using namespace std;
-   bool test __attribute__((unused)) = true;
  
    locale loc =  locale::classic();
    loc = locale(loc, new errorcvt);
--- 50,55 ----
*************** void test13()
*** 59,68 ****
    filebuf fbuf1;
    fbuf1.pubimbue(loc);
    fbuf1.open(name_07, ios_base::out | ios_base::trunc);
!   fbuf1.sputn("ison", 4); 
!   int r = fbuf1.pubsync();
!   VERIFY( r == -1 );
!   fbuf1.close();
  }
  
  int main() 
--- 57,72 ----
    filebuf fbuf1;
    fbuf1.pubimbue(loc);
    fbuf1.open(name_07, ios_base::out | ios_base::trunc);
! 
!   try
!     {  
!       fbuf1.sputn("ison", 4); 
!       fbuf1.pubsync();
!       fbuf1.close();
!     }
!   catch (exception&)
!     {
!     }
  }
  
  int main() 

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