]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/13858 (Bad error handling in basic_filebuf::imbue)
authorPaolo Carlini <pcarlini@suse.de>
Sat, 14 Feb 2004 19:04:00 +0000 (19:04 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sat, 14 Feb 2004 19:04:00 +0000 (19:04 +0000)
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.

From-SVN: r77812

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/fstream.tcc
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/13858.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/9182-2.cc
libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/3.cc
libstdc++-v3/testsuite/27_io/basic_filebuf/seekpos/wchar_t/1.cc
libstdc++-v3/testsuite/27_io/basic_filebuf/sync/char/9182-1.cc

index b4a808445aeca1201b26b9124b8e65867f5dddae..08b5642190b53236c89e75656ed7ded90389e635 100644 (file)
@@ -1,3 +1,20 @@
+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.
+
 2004-02-12  Paolo Carlini  <pcarlini@suse.de>
 
        PR libstdc++/13731 (final part: writev)
index 9022b5825940cf846790c35ca986ef86b28f5820..19530e75c49a918e610b8cf2a3d1af3c1af60eaa 100644 (file)
@@ -397,7 +397,7 @@ namespace std
              // and output.
              if (_M_convert_to_external(this->pbase(),
                                         this->pptr() - this->pbase())
-                 && (!__testeof || (__testeof && !_M_file.sync())))
+                 && (!__testeof || !_M_file.sync()))
                {
                  _M_set_buffer(0);
                  __ret = traits_type::not_eof(__c);
@@ -437,12 +437,12 @@ namespace std
     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
     {
       // Sizes of external and pending output.
-      streamsize __elen = 0;
-      streamsize __plen = 0;
+      streamsize __elen;
+      streamsize __plen;
       if (__check_facet(_M_codecvt).always_noconv())
        {
-         __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
-         __plen += __ilen;
+         __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
+         __plen = __ilen;
        }
       else
        {
@@ -466,19 +466,14 @@ namespace std
              __blen = __ilen;
            }
          else
-           {
-             // Result == error.
-             __blen = 0;
-           }
-
-         if (__blen)
-           {
-             __elen += _M_file.xsputn(__buf, __blen);
-             __plen += __blen;
-           }
+           __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)
+         if (__r == codecvt_base::partial && __elen == __plen)
            {
              const char_type* __iresume = __iend;
              streamsize __rlen = this->pptr() - __iend;
@@ -488,12 +483,15 @@ namespace std
              if (__r != codecvt_base::error)
                {
                  __rlen = __bend - __buf;
-                 __elen += _M_file.xsputn(__buf, __rlen);
-                 __plen += __rlen;
+                 __elen = _M_file.xsputn(__buf, __rlen);
+                 __plen = __rlen;
                }
+             else
+               __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
+                                       "conversion error"));
            }
        }
-      return __elen && __elen == __plen;
+      return __elen == __plen;
     }
 
    template<typename _CharT, typename _Traits>
diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/13858.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/char/13858.cc
new file mode 100644 (file)
index 0000000..1abd6a8
--- /dev/null
@@ -0,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;
+}
index 9904f194109c5de93831f20b6fd2ea88ab6f92c4..862d0fd36079f453a6ff4b3e78d320f59ded49c0 100644 (file)
@@ -1,6 +1,6 @@
 // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
 
-// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+// 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,7 +22,6 @@
 
 #include <fstream>
 #include <locale>
-#include <testsuite_hooks.h>
 
 const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
 
@@ -51,7 +50,6 @@ protected:
 void test14()
 {
   using namespace std;
-  bool test __attribute__((unused)) = true;
   
   locale loc =  locale::classic();
   loc = locale(loc, new errorcvt);
@@ -60,9 +58,15 @@ void test14()
   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();
+
+  try
+    {
+      fbuf1.sputn("onne", 4);
+      fbuf1.close();
+    }
+  catch (exception&)
+    {
+    }
 }
 
 int main() 
diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/13858.cc
new file mode 100644 (file)
index 0000000..57dd0aa
--- /dev/null
@@ -0,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;
+}
index 3088e5ff944b3f552cd0726b37d82a08bd6d783f..03e46d1b588f94fb683ffb2c3f0be260c248c70e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2003 Free Software Foundation, Inc.
+// 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
 // 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;
@@ -33,10 +31,15 @@ void test03()
   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)) );
+  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()
index c543212b19e21ea33e594cdc77ea214bbfafecd1..bb0f4f2d65084b680fb325540dc3c1d36130aea3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2003 Free Software Foundation, Inc.
+// 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
 
 #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;
@@ -35,8 +33,13 @@ void test01()
   streampos pos = fb.pubseekoff(0, ios_base::beg);
   fb.sputc(0xf001);
 
-  streampos ret = fb.pubseekpos(pos);
-  VERIFY( ret == streampos(streamoff(-1)) );
+  try
+    {
+      fb.pubseekpos(pos);
+    }
+  catch (exception&)
+    {
+    }
 }
 
 int main()
index 9448c8490c7fbc323286bbc2f06f6828a544b5d4..e04c9b282c1b715c6aa18415227bd4fc9b4685da 100644 (file)
@@ -1,6 +1,6 @@
 // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
 
-// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+// 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,7 +22,6 @@
 
 #include <fstream>
 #include <locale>
-#include <testsuite_hooks.h>
 
 const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
 
@@ -51,7 +50,6 @@ protected:
 void test13()
 {
   using namespace std;
-  bool test __attribute__((unused)) = true;
 
   locale loc =  locale::classic();
   loc = locale(loc, new errorcvt);
@@ -59,10 +57,16 @@ void test13()
   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();
+
+  try
+    {  
+      fbuf1.sputn("ison", 4); 
+      fbuf1.pubsync();
+      fbuf1.close();
+    }
+  catch (exception&)
+    {
+    }
 }
 
 int main()