]> gcc.gnu.org Git - gcc.git/commitdiff
Nathan C.
authorPaolo Carlini <pcarlini@unitus.it>
Fri, 27 Jun 2003 12:33:49 +0000 (14:33 +0200)
committerPaolo Carlini <paolo@gcc.gnu.org>
Fri, 27 Jun 2003 12:33:49 +0000 (12:33 +0000)
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.

Co-Authored-By: Nathan C. Myers <ncm-nospam@cantrip.org>
Co-Authored-By: Petur Runolfsson <peturr02@ru.is>
From-SVN: r68566

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/fstream.tcc
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc [new file with mode: 0644]

index e8088c409b3c03a8883f2d54f19b4551b6df9461..0d30ceb2f66223718bb3d81239c966f3e695a931 100644 (file)
@@ -1,3 +1,23 @@
+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.
+
 2003-06-27  Nathan Sidwell  <nathan@codesourcery.com>
 
        * config/linker-map.gnu: Remove ; after __numpunct_cache.
index 1568b6015fee7541e80a71890b7f40b6d9a9b5a2..3d6fb96e296f8c70bd4f75774e14227d76008216 100644 (file)
@@ -213,16 +213,19 @@ namespace std
            }
          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 @@ namespace std
       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 --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc
new file mode 100644 (file)
index 0000000..5174c33
--- /dev/null
@@ -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 --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc
new file mode 100644 (file)
index 0000000..571d717
--- /dev/null
@@ -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 --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc
new file mode 100644 (file)
index 0000000..86c81fa
--- /dev/null
@@ -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 --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc
new file mode 100644 (file)
index 0000000..100e6a2
--- /dev/null
@@ -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 --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc
new file mode 100644 (file)
index 0000000..8a57970
--- /dev/null
@@ -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;
+}
This page took 0.085401 seconds and 5 git commands to generate.