From 3461133d7227f31154ca44497a44f1599e4c0aa8 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 27 Jun 2003 14:33:49 +0200 Subject: [PATCH] Nathan C. 2003-06-27 Paolo Carlini Nathan C. Myers 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 Petur Runolfsson 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 Co-Authored-By: Petur Runolfsson From-SVN: r68566 --- libstdc++-v3/ChangeLog | 20 ++ libstdc++-v3/include/bits/fstream.tcc | 20 +- .../basic_filebuf/overflow/wchar_t/11305-1.cc | 46 ++++ .../basic_filebuf/overflow/wchar_t/11305-2.cc | 48 ++++ .../basic_filebuf/overflow/wchar_t/11305-3.cc | 45 ++++ .../basic_filebuf/overflow/wchar_t/11305-4.cc | 47 ++++ .../basic_filebuf/underflow/wchar_t/9178.cc | 205 ++++++++++++++++++ 7 files changed, 422 insertions(+), 9 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc create mode 100644 libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index e8088c409b3c..0d30ceb2f662 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,23 @@ +2003-06-27 Paolo Carlini + Nathan C. Myers + + 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 + Petur Runolfsson + + 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 * config/linker-map.gnu: Remove ; after __numpunct_cache. diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc index 1568b6015fee..3d6fb96e296f 100644 --- a/libstdc++-v3/include/bits/fstream.tcc +++ b/libstdc++-v3/include/bits/fstream.tcc @@ -213,16 +213,19 @@ namespace std } else { - char* __buf = static_cast(__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(__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(__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 index 000000000000..5174c332d8a5 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc @@ -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 +#include +#include + +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 index 000000000000..571d7178bceb --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc @@ -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 +#include +#include + +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 index 000000000000..86c81fa5604b --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-3.cc @@ -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 +#include +#include + +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 index 000000000000..100e6a2b0f43 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-4.cc @@ -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 +#include +#include + +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 index 000000000000..8a5797053fb8 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/9178.cc @@ -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 +#include +#include +#include +#include +#include + +template +class checksumcvt : public std::codecvt +{ + typedef std::codecvt 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::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(from_end - from), + static_cast(to_end - to) / width); + + while (len--) + { + const char* p = reinterpret_cast(from); + unsigned char checksum = 0; + + for (size_t i = 0; i < sizeof(intern_type); ++i) + { + *to++ = p[i]; + checksum ^= static_cast(p[i]); + } + + *to++ = checksum; + ++from; + } + + from_next = from; + to_next = to; + return from_next == from_end ? std::codecvt::ok + : std::codecvt::partial; + } + + virtual typename std::codecvt::result + do_unshift(StateT&, extern_type* to, + extern_type*, extern_type*& to_next) const + { + to_next = to; + return std::codecvt::ok; + } + + virtual typename std::codecvt::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(to_end - to), + static_cast(from_end - from) / width); + + while (len) + { + const char* f = from; + intern_type tmp; + char* p = reinterpret_cast(&tmp); + unsigned char checksum = 0; + + for (size_t i = 0; i < sizeof(intern_type); ++i) + { + p[i] = *f; + checksum ^= static_cast(*f++); + } + + if (*f++ != checksum) + break; + + from = f; + *to++ = tmp; + len--; + } + + from_next = from; + to_next = to; + return len ? std::codecvt::error : + (from_next == from_end ? std::codecvt::ok + : std::codecvt::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(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(*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); + + 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(&fbuf2), + istreambuf_iterator(), + back_inserter(str2)); + + VERIFY( str1 == str2 ); +} + +int main() +{ + test01(); + return 0; +} -- 2.47.2