This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Fix libstdc++/9875
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 28 Jun 2003 09:56:22 +0200
- Subject: [v3] Fix libstdc++/9875
Hi,
tested x86-linux, committed.
Paolo.
/////////
2003-06-28 Paolo Carlini <pcarlini@unitus.it>
PR libstdc++/9875
* include/bits/fstream.tcc (seekoff): Fix for encoding() > 0.
(seekpos): Likewise.
* testsuite/27_io/basic_filebuf/seekoff/wchar_t/9875_seekoff.cc:
New test.
* testsuite/27_io/basic_filebuf/seekpos/wchar_t/9875_seekpos.cc:
New test.
diff -urN libstdc++-v3-orig/include/bits/fstream.tcc libstdc++-v3/include/bits/fstream.tcc
--- libstdc++-v3-orig/include/bits/fstream.tcc 2003-06-27 14:00:07.000000000 +0200
+++ libstdc++-v3/include/bits/fstream.tcc 2003-06-27 20:28:53.000000000 +0200
@@ -486,7 +486,7 @@
int __width = 0;
if (_M_codecvt)
- __width = _M_codecvt->encoding();
+ __width = _M_codecvt->encoding();
if (__width < 0)
__width = 0;
@@ -497,7 +497,7 @@
_M_destroy_pback();
// Sync the internal and external streams.
- off_type __computed_off = __width * __off;
+ off_type __computed_off = __off;
if (this->pbase() < this->pptr()
|| _M_last_overflowed)
@@ -512,7 +512,8 @@
__computed_off += this->gptr() - this->egptr();
// Return pos_type(off_type(-1)) in case of failure.
- __ret = _M_file.seekoff(__computed_off, __way, __mode);
+ __ret = _M_file.seekoff(__computed_off * __width, __way, __mode);
+
_M_reading = false;
_M_writing = false;
_M_set_buffer(-1);
@@ -528,7 +529,15 @@
{
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 171. Strange seekpos() semantics due to joint position
- return this->seekoff(off_type(__pos), ios_base::beg, __mode);
+ pos_type __ret = pos_type(off_type(-1));
+
+ int __width = 0;
+ if (_M_codecvt)
+ __width = _M_codecvt->encoding();
+ if (__width > 0)
+ __ret = this->seekoff(off_type(__pos) / __width, ios_base::beg, __mode);
+
+ return __ret;
#endif
}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekoff/wchar_t/9875_seekoff.cc libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/9875_seekoff.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekoff/wchar_t/9875_seekoff.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/9875_seekoff.cc 2003-06-27 20:26:22.000000000 +0200
@@ -0,0 +1,100 @@
+// 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 <algorithm>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+class Cvt : public std::codecvt<wchar_t, char, mbstate_t>
+{
+protected:
+ virtual std::codecvt_base::result
+ do_out(std::mbstate_t&, const wchar_t* from, const wchar_t* from_end,
+ const wchar_t*& from_next, char* to, char* to_end,
+ char*& to_next) const
+ {
+ std::size_t from_len = from_end - from;
+ std::size_t to_len = (to_end - to) / sizeof(wchar_t);
+ std::size_t len = std::min(from_len, to_len);
+ std::memcpy(to, from, len * sizeof(wchar_t));
+ from_next = from + len;
+ to_next = to + len * sizeof(wchar_t);
+ return from_next == from_end ? std::codecvt_base::ok :
+ std::codecvt_base::partial;
+ }
+
+ virtual std::codecvt_base::result
+ do_in(std::mbstate_t&, const char* from, const char* from_end,
+ const char*& from_next, wchar_t* to, wchar_t* to_end,
+ wchar_t*& to_next) const
+ {
+ std::size_t from_len =
+ (from_end - from) / sizeof(wchar_t);
+ std::size_t to_len = to_end - to;
+ std::size_t len = std::min(from_len, to_len);
+ std::memcpy(to, from, len * sizeof(wchar_t));
+ from_next = from + len * sizeof(wchar_t);
+ to_next = to + len;
+ return from_next == from_end ? std::codecvt_base::ok :
+ std::codecvt_base::partial;
+ }
+
+ virtual std::codecvt_base::result
+ do_unshift(std::mbstate_t&, char*, char*, char*&) const
+ { return std::codecvt_base::noconv; }
+
+ virtual int do_encoding() const throw() { return sizeof(wchar_t); }
+ virtual bool do_always_noconv() const throw() { return false; }
+
+ virtual int
+ do_length(std::mbstate_t&, const char* from, const char* end,
+ std::size_t max)
+ {
+ std::size_t len = (end - from) / sizeof(wchar_t);
+ return std::min(len, max) * sizeof(wchar_t);
+ }
+
+ virtual int do_max_length() const throw() { return sizeof(wchar_t); }
+};
+
+void test01()
+{
+ using namespace std;
+ bool test = true;
+
+ // seekoff
+ wfilebuf fb;
+ fb.pubimbue(locale(locale::classic(), new Cvt));
+ fb.open("tmp_9875_seekoff", ios_base::out | ios_base::in | ios_base::trunc);
+ fb.sputn(L"0123456789", 10);
+ fb.pubseekoff(-3, ios_base::cur);
+ VERIFY( fb.sgetc() == L'7' );
+ fb.pubseekoff(-3, ios_base::cur);
+ VERIFY( fb.sgetc() == L'4' );
+ fb.close();
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekpos/wchar_t/9875_seekpos.cc libstdc++-v3/testsuite/27_io/basic_filebuf/seekpos/wchar_t/9875_seekpos.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_filebuf/seekpos/wchar_t/9875_seekpos.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/seekpos/wchar_t/9875_seekpos.cc 2003-06-27 20:26:45.000000000 +0200
@@ -0,0 +1,112 @@
+// 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 <algorithm>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+class Cvt : public std::codecvt<wchar_t, char, mbstate_t>
+{
+protected:
+ virtual std::codecvt_base::result
+ do_out(std::mbstate_t&, const wchar_t* from, const wchar_t* from_end,
+ const wchar_t*& from_next, char* to, char* to_end,
+ char*& to_next) const
+ {
+ std::size_t from_len = from_end - from;
+ std::size_t to_len = (to_end - to) / sizeof(wchar_t);
+ std::size_t len = std::min(from_len, to_len);
+ std::memcpy(to, from, len * sizeof(wchar_t));
+ from_next = from + len;
+ to_next = to + len * sizeof(wchar_t);
+ return from_next == from_end ? std::codecvt_base::ok :
+ std::codecvt_base::partial;
+ }
+
+ virtual std::codecvt_base::result
+ do_in(std::mbstate_t&, const char* from, const char* from_end,
+ const char*& from_next, wchar_t* to, wchar_t* to_end,
+ wchar_t*& to_next) const
+ {
+ std::size_t from_len =
+ (from_end - from) / sizeof(wchar_t);
+ std::size_t to_len = to_end - to;
+ std::size_t len = std::min(from_len, to_len);
+ std::memcpy(to, from, len * sizeof(wchar_t));
+ from_next = from + len * sizeof(wchar_t);
+ to_next = to + len;
+ return from_next == from_end ? std::codecvt_base::ok :
+ std::codecvt_base::partial;
+ }
+
+ virtual std::codecvt_base::result
+ do_unshift(std::mbstate_t&, char*, char*, char*&) const
+ { return std::codecvt_base::noconv; }
+
+ virtual int do_encoding() const throw() { return sizeof(wchar_t); }
+ virtual bool do_always_noconv() const throw() { return false; }
+
+ virtual int
+ do_length(std::mbstate_t&, const char* from, const char* end,
+ std::size_t max)
+ {
+ std::size_t len = (end - from) / sizeof(wchar_t);
+ return std::min(len, max) * sizeof(wchar_t);
+ }
+
+ virtual int do_max_length() const throw() { return sizeof(wchar_t); }
+};
+
+void test01()
+{
+ using namespace std;
+ bool test = true;
+
+ // seekpos
+ wfilebuf fb;
+ fb.pubimbue(locale(locale::classic(), new Cvt));
+ fb.open("tmp_9875_seekpos", ios_base::out | ios_base::in | ios_base::trunc);
+ fb.sputn(L"0123456789", 10);
+
+ streampos p1 = fb.pubseekoff(0, ios_base::cur);
+ VERIFY( p1 != streampos(-1) );
+ fb.sputc(L'a');
+
+ streampos p2 = fb.pubseekpos(p1);
+ VERIFY( p2 != streampos(-1) );
+ VERIFY( p2 == p1 );
+ VERIFY( fb.sgetc() == L'a' );
+
+ fb.pubseekoff(0, ios_base::beg);
+ wchar_t buf[11];
+ streamsize s1 = fb.sgetn(buf, 11);
+ VERIFY( s1 == 11 );
+ VERIFY( !wmemcmp(buf, L"0123456789a", 11) );
+
+ fb.close();
+}
+
+int main()
+{
+ test01();
+ return 0;
+}