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++/9178 and 11305


Hi,

tested x86-linux, reviewed by Benjamin.

Paolo.

/////////
2003-06-27  Paolo Carlini  <pcarlini@unitus.it>
	    Nathan C. Myers  <ncm-nospam@cantrip.org>

	PR libstdc++/9178
	* include/bits/fstream.tcc (_M_underflow): Properly estimate
	the worst-case number of external bytes for a given get area.
	* testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc: New.

2003-06-27  Paolo Carlini  <pcarlini@unitus.it>
	    Petur Runolfsson  <peturr02@ru.is>

	PR libstdc++/11305
	* include/bits/fstream.tcc (overflow): Properly estimate the
	worst-case number of external bytes for a given put area
	(by using codecvt::max_length()).
	* testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1: New.
	* testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2: New.
	* testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3: New.
	* testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4: New.
diff -urN libstdc++-v3-orig/include/bits/fstream.tcc libstdc++-v3/include/bits/fstream.tcc
--- libstdc++-v3-orig/include/bits/fstream.tcc	2003-06-26 11:55:23.000000000 +0200
+++ libstdc++-v3/include/bits/fstream.tcc	2003-06-27 14:00:07.000000000 +0200
@@ -213,16 +213,19 @@
 	    }
 	  else
 	    {
-	      char* __buf = static_cast<char*>(__builtin_alloca(__buflen));
-	      __elen = _M_file.xsgetn(__buf, __buflen);
- 
-	      const char* __eend;
+	      // Worst-case number of external bytes.
+	      // XXX Not done encoding() == -1.
+	      const streamsize __blen = __buflen * _M_codecvt->max_length();
+	      char* __buf = static_cast<char*>(__builtin_alloca(__blen));
+	      __elen = _M_file.xsgetn(__buf, __blen);
+
+ 	      const char* __eend;
 	      char_type* __iend;
 	      codecvt_base::result __r;
 	      __r = _M_codecvt->in(_M_state_cur, __buf, __buf + __elen, 
 				   __eend, this->eback(), 
 				   this->eback() + __buflen, __iend);
-	      if (__r == codecvt_base::ok)
+	      if (__r == codecvt_base::ok || __r == codecvt_base::partial)
 		__ilen = __iend - this->eback();
 	      else if (__r == codecvt_base::noconv)
 		{
@@ -391,11 +394,10 @@
       else
 	{
 	  // Worst-case number of external bytes needed.
-	  int __ext_multiplier = _M_codecvt->encoding();
-	  if (__ext_multiplier ==  -1 || __ext_multiplier == 0)
-	    __ext_multiplier = sizeof(char_type);
-	  streamsize __blen = __ilen * __ext_multiplier;
+	  // XXX Not done encoding() == -1.
+	  streamsize __blen = __ilen * _M_codecvt->max_length();
 	  char* __buf = static_cast<char*>(__builtin_alloca(__blen));
+
 	  char* __bend;
 	  const char_type* __iend;
 	  codecvt_base::result __r;
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc	2003-06-26 22:59:05.000000000 +0200
@@ -0,0 +1,46 @@
+// 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.
+
+// 27.8.1.4 Overridden virtual functions
+
+#include <fstream>
+#include <locale>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  bool test = true;
+
+  wfilebuf fb;
+  locale loc(__gnu_cxx_test::try_named_locale("en_US.UTF-8"));
+  fb.pubimbue(loc);
+  fb.pubsetbuf(0, 0);
+  fb.open("tmp_11305-1", ios_base::out);
+  wint_t n1 = fb.sputc(0x20000000);
+  wfilebuf* f = fb.close();
+  
+  VERIFY( n1 != WEOF );
+  VERIFY( f != NULL );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc	2003-06-26 23:04:03.000000000 +0200
@@ -0,0 +1,48 @@
+// 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.
+
+// 27.8.1.4 Overridden virtual functions
+
+#include <fstream>
+#include <locale>
+#include <testsuite_hooks.h>
+
+void test02()
+{
+  using namespace std;
+  bool test = true;
+
+  wfilebuf fb;
+  locale loc(__gnu_cxx_test::try_named_locale("en_US.UTF-8"));
+  fb.pubimbue(loc);
+  fb.pubsetbuf(0, 0);
+  fb.open("tmp_11305-2", ios_base::out);
+  wint_t n1 = fb.sputc(0x20000000);
+  wint_t n2 = fb.sputc(0x40000000);
+  wfilebuf* f = fb.close();
+  
+  VERIFY( n1 != WEOF );
+  VERIFY( n2 != WEOF );
+  VERIFY( f != NULL );
+}
+
+int main()
+{
+  test02();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc	2003-06-26 23:03:37.000000000 +0200
@@ -0,0 +1,45 @@
+// 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.
+
+// 27.8.1.4 Overridden virtual functions
+
+#include <fstream>
+#include <locale>
+#include <testsuite_hooks.h>
+
+void test03()
+{
+  using namespace std;
+  bool test = true;
+
+  wfilebuf fb;
+  locale loc(__gnu_cxx_test::try_named_locale("en_US.UTF-8"));
+  fb.pubimbue(loc);
+  fb.open("tmp_11305-3", ios_base::out);
+  wint_t n1 = fb.sputc(0x20000000);
+  wfilebuf* f = fb.close();
+	
+  VERIFY( n1 != WEOF );
+  VERIFY( f != NULL );
+}
+
+int main()
+{
+  test03();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc	2003-06-26 23:05:45.000000000 +0200
@@ -0,0 +1,47 @@
+// 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.
+
+// 27.8.1.4 Overridden virtual functions
+
+#include <fstream>
+#include <locale>
+#include <testsuite_hooks.h>
+
+void test04()
+{
+  using namespace std;
+  bool test = true;
+
+  wfilebuf fb;
+  locale loc(__gnu_cxx_test::try_named_locale("en_US.UTF-8"));
+  fb.pubimbue(loc);
+  fb.open("tmp_11405-4", ios_base::out);
+  wint_t n1 = fb.sputc(0x20000000);
+  wint_t n2 = fb.sputc(0x40000000);
+  wfilebuf* f = fb.close();
+  
+  VERIFY( n1 != WEOF );
+  VERIFY( n2 != WEOF );
+  VERIFY( f != NULL );
+}
+
+int main()
+{
+  test04();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc	2003-06-27 13:58:23.000000000 +0200
@@ -0,0 +1,205 @@
+// 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.
+
+// 27.8.1.4 Overridden virtual functions
+
+#include <fstream>
+#include <string>
+#include <iterator>
+#include <algorithm>
+#include <locale>
+#include <testsuite_hooks.h>
+
+template <typename InternT, typename StateT = mbstate_t>
+class checksumcvt : public std::codecvt<InternT, char, StateT>
+{
+  typedef std::codecvt<InternT, char, StateT> Base;
+  static const size_t width = sizeof(InternT) + 1;
+
+public:
+  typedef InternT intern_type;
+  typedef char extern_type;
+
+  explicit checksumcvt(size_t refs = 0)
+  : Base(refs)
+  { }
+
+protected:
+  virtual typename std::codecvt<InternT, char, StateT>::result
+  do_out(StateT&, const intern_type* from,
+	 const intern_type* from_end, const intern_type*& from_next,
+	 extern_type* to, extern_type* to_end,
+	 extern_type*& to_next) const
+  {
+    size_t len = std::min(static_cast<size_t>(from_end - from),
+			  static_cast<size_t>(to_end - to) / width);
+
+    while (len--)
+      {
+	const char* p = reinterpret_cast<const char*>(from);
+	unsigned char checksum = 0;
+				
+	for (size_t i = 0; i < sizeof(intern_type); ++i)
+	  {
+	    *to++ = p[i];
+	    checksum ^= static_cast<unsigned char>(p[i]);
+	  }
+
+	*to++ = checksum;
+	++from;
+      }
+
+    from_next = from;
+    to_next = to;
+    return from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
+           : std::codecvt<InternT, char, StateT>::partial;
+  }
+
+  virtual typename std::codecvt<InternT, char, StateT>::result
+  do_unshift(StateT&, extern_type* to,
+	     extern_type*, extern_type*& to_next) const
+  {
+    to_next = to;
+    return std::codecvt<InternT, char, StateT>::ok;
+  }
+
+  virtual typename std::codecvt<InternT, char, StateT>::result
+  do_in(StateT&, const extern_type* from,
+	const extern_type* from_end, const extern_type*& from_next,
+	intern_type* to, intern_type* to_end,
+	intern_type*& to_next) const
+  {
+    size_t len = std::min(static_cast<size_t>(to_end - to),
+			  static_cast<size_t>(from_end - from) / width);
+			
+    while (len)
+      {
+	const char* f = from;
+	intern_type tmp;
+	char* p = reinterpret_cast<char*>(&tmp);
+	unsigned char checksum = 0;
+
+	for (size_t i = 0; i < sizeof(intern_type); ++i)
+	  {
+	    p[i] = *f;
+	    checksum ^= static_cast<unsigned char>(*f++);
+	  }
+
+	if (*f++ != checksum)
+	  break;
+
+	from = f;
+	*to++ = tmp;
+	len--;
+      }
+
+    from_next = from;
+    to_next = to;
+    return len ? std::codecvt<InternT, char, StateT>::error :
+      (from_next == from_end ? std::codecvt<InternT, char, StateT>::ok
+       : std::codecvt<InternT, char, StateT>::partial);
+  }
+
+  virtual int
+  do_encoding() const throw()
+  {
+    return width;
+  }
+
+  virtual int
+  do_length(StateT&, const extern_type* from,
+	    const extern_type* end, size_t max) const
+  {
+    size_t len = std::min(max, static_cast<size_t>(end - from) / width);
+
+    int ret = 0;
+    while (len--)
+      {
+	unsigned char checksum = 0;
+
+	for (size_t i = 0; i < sizeof(intern_type); ++i)
+	  {
+	    checksum ^= static_cast<unsigned char>(*from++);
+	  }
+
+	if (*from++ != checksum)
+	  break;
+
+	ret++;
+      }
+
+    return ret;
+  }
+
+  virtual int
+  do_max_length() const throw()
+  {
+    return width;
+  }
+
+  virtual bool
+  do_always_noconv() const throw()
+  {
+    return false;
+  }
+};
+
+void test01()
+{
+  using namespace std;
+  bool test = true;
+
+  locale loc;
+  loc = locale(loc, new checksumcvt<wchar_t>);
+
+  wfilebuf fbuf1;
+  fbuf1.pubimbue(loc);
+  fbuf1.open("tmp_9178", ios_base::out | ios_base::trunc);
+	
+  string tmpstr = "abcdefghijklmnopqrstuvwxyz0123456789 \t\n";
+
+  wifstream stream;
+  wstring str1;
+
+  while (str1.length() < 20000) 
+    {
+      transform(tmpstr.begin(), tmpstr.end(),
+		back_inserter(str1),
+		bind1st(std::mem_fun(&std::wios::widen), &stream));
+    }
+	
+  fbuf1.sputn(str1.data(), str1.size());
+  fbuf1.close();
+
+  wfilebuf fbuf2;
+  fbuf2.pubimbue(loc);
+  fbuf2.open("tmp_9178", std::ios_base::in);
+	
+  wstring str2;
+  copy(istreambuf_iterator<wchar_t>(&fbuf2),
+       istreambuf_iterator<wchar_t>(),
+       back_inserter(str2));
+
+  VERIFY( str1 == str2 );
+}
+
+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]