From 52a16d0815a08b0af6fba0c24a87917de17e3ef1 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Wed, 1 Oct 2003 16:32:05 +0200 Subject: [PATCH] re PR libstdc++/12439 (Problems in time_put::put) 2003-10-01 Paolo Carlini PR libstdc++/12439 * include/bits/locale_facets.tcc (time_put::put): Deal with the three issues pointed out by the PR. * testsuite/22_locale/time_put/put/char/12439_1.cc: New. * testsuite/22_locale/time_put/put/char/12439_3.cc: New. * testsuite/22_locale/time_put/put/wchar_t/12439_1.cc: New. * testsuite/22_locale/time_put/put/wchar_t/12439_2.cc: New. * testsuite/22_locale/time_put/put/wchar_t/12439_3.cc: New. From-SVN: r71976 --- libstdc++-v3/ChangeLog | 11 ++++ libstdc++-v3/include/bits/locale_facets.tcc | 16 ++--- .../22_locale/time_put/put/char/12439_1.cc | 64 +++++++++++++++++++ .../22_locale/time_put/put/char/12439_3.cc | 62 ++++++++++++++++++ .../22_locale/time_put/put/wchar_t/12439_1.cc | 64 +++++++++++++++++++ .../22_locale/time_put/put/wchar_t/12439_2.cc | 60 +++++++++++++++++ .../22_locale/time_put/put/wchar_t/12439_3.cc | 62 ++++++++++++++++++ 7 files changed, 329 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc create mode 100644 libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc create mode 100644 libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc create mode 100644 libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc create mode 100644 libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2c0b50b01d6..4b776c89e21 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2003-10-01 Paolo Carlini + + PR libstdc++/12439 + * include/bits/locale_facets.tcc (time_put::put): Deal + with the three issues pointed out by the PR. + * testsuite/22_locale/time_put/put/char/12439_1.cc: New. + * testsuite/22_locale/time_put/put/char/12439_3.cc: New. + * testsuite/22_locale/time_put/put/wchar_t/12439_1.cc: New. + * testsuite/22_locale/time_put/put/wchar_t/12439_2.cc: New. + * testsuite/22_locale/time_put/put/wchar_t/12439_3.cc: New. + 2003-09-30 Paolo Carlini * include/bits/stl_algo.h: Minor cosmetic reformattings. diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc index 4c816038281..62127b9e7fd 100644 --- a/libstdc++-v3/include/bits/locale_facets.tcc +++ b/libstdc++-v3/include/bits/locale_facets.tcc @@ -2000,21 +2000,20 @@ namespace std template _OutIter time_put<_CharT, _OutIter>:: - put(iter_type __s, ios_base& __io, char_type, const tm* __tm, + put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm, const _CharT* __beg, const _CharT* __end) const { locale __loc = __io.getloc(); ctype<_CharT> const& __ctype = use_facet >(__loc); while (__beg != __end) { - char __c = __ctype.narrow(*__beg, 0); + const _CharT* __tmp = __beg; ++__beg; - if (__c == '%') + if (__ctype.narrow(*__tmp, 0) == '%' && __beg != __end) { char __format; char __mod = 0; - size_t __len = 1; - __c = __ctype.narrow(*__beg, 0); + const char __c = __ctype.narrow(*__beg, 0); ++__beg; if (__c == 'E' || __c == 'O') { @@ -2024,13 +2023,10 @@ namespace std } else __format = __c; - __s = this->do_put(__s, __io, _CharT(), __tm, __format, __mod); + __s = this->do_put(__s, __io, __fill, __tm, __format, __mod); } else - { - *__s = __c; - ++__s; - } + *__s++ = *__tmp; } return __s; } diff --git a/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc b/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc new file mode 100644 index 00000000000..bc3bb862da9 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc @@ -0,0 +1,64 @@ +// Copyright (C) 2003 Free Software Foundation +// +// 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. + +// 22.2.5.3.1 time_put members + +#include +#include +#include +#include +#include + +class TP : public std::time_put +{ +public: + mutable std::string fill_chars; + +protected: + iter_type do_put(iter_type s, std::ios_base&, char_type fill, + const std::tm* t, char format, char modifier) const + { + fill_chars.push_back(fill); + return s; + } +}; + +// libstdc++/12439 +// time_put::put doesn't pass fill character to do_put +void test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + ostringstream stream; + time_t tt = time(NULL); + + const char* fmt = "%c"; + + TP tp; + tp.put(TP::iter_type(stream), stream, 'W', localtime(&tt), + fmt, fmt + strlen(fmt)); + VERIFY( !tp.fill_chars.empty() ); + VERIFY( tp.fill_chars[tp.fill_chars.length() - 1] == 'W' ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc b/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc new file mode 100644 index 00000000000..c1d0167d340 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2003 Free Software Foundation +// +// 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. + +// 22.2.5.3.1 time_put members + +#include +#include +#include +#include + +class TP : public std::time_put +{ +public: + mutable std::string format_chars; + +protected: + iter_type do_put(iter_type s, std::ios_base&, char_type fill, + const std::tm* t, char format, char modifier) const + { + format_chars.push_back(format); + return s; + } +}; + +// libstdc++/12439 +// time_put::put reads past end of format string +void test03() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + ostringstream stream; + time_t tt = time(NULL); + + const char* fmt = "%c"; + + TP tp; + tp.put(TP::iter_type(stream), stream, stream.fill(), localtime(&tt), + fmt, fmt + 1); + VERIFY( tp.format_chars.empty() ); +} + +int main() +{ + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc new file mode 100644 index 00000000000..ce2d0d54a93 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc @@ -0,0 +1,64 @@ +// Copyright (C) 2003 Free Software Foundation +// +// 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. + +// 22.2.5.3.1 time_put members + +#include +#include +#include +#include +#include + +class TP : public std::time_put +{ +public: + mutable std::wstring fill_chars; + +protected: + iter_type do_put(iter_type s, std::ios_base&, char_type fill, + const std::tm* t, char format, char modifier) const + { + fill_chars.push_back(fill); + return s; + } +}; + +// libstdc++/12439 +// time_put::put doesn't pass fill character to do_put +void test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + wostringstream stream; + time_t tt = time(NULL); + + const wchar_t* fmt = L"%c"; + + TP tp; + tp.put(TP::iter_type(stream), stream, L'W', localtime(&tt), + fmt, fmt + wcslen(fmt)); + VERIFY( !tp.fill_chars.empty() ); + VERIFY( tp.fill_chars[tp.fill_chars.length() - 1] == L'W' ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc new file mode 100644 index 00000000000..6715cc53846 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc @@ -0,0 +1,60 @@ +// Copyright (C) 2003 Free Software Foundation +// +// 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. + +// 22.2.5.3.1 time_put members + +#include +#include +#include +#include +#include + +// libstdc++/12439 +// time_put::put writes narrowed characters to output iterator +void test02() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + typedef time_put tp_type; + + const wchar_t fmt[] = { + 0xa0, 0x103, 0xfc, 0xb3, 0xa0c3, + L'%', L'c' + }; + + const size_t len = sizeof(fmt) / sizeof(fmt[0]); + const size_t cmplen = wcschr(fmt, L'%') - fmt; + + locale loc; + const tp_type& tp = use_facet(loc); + time_t tt = time(NULL); + wostringstream stream; + + tp.put(tp_type::iter_type(stream), stream, stream.fill(), + localtime(&tt), fmt, fmt + len); + wstring str = stream.str(); + VERIFY( str.length() >= cmplen ); + VERIFY( !wmemcmp(str.data(), fmt, cmplen) ); +} + +int main() +{ + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc new file mode 100644 index 00000000000..0bcae335af2 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2003 Free Software Foundation +// +// 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. + +// 22.2.5.3.1 time_put members + +#include +#include +#include +#include + +class TP : public std::time_put +{ +public: + mutable std::string format_chars; + +protected: + iter_type do_put(iter_type s, std::ios_base&, char_type fill, + const std::tm* t, char format, char modifier) const + { + format_chars.push_back(format); + return s; + } +}; + +// libstdc++/12439 +// time_put::put reads past end of format string +void test03() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + wostringstream stream; + time_t tt = time(NULL); + + const wchar_t* fmt = L"%c"; + + TP tp; + tp.put(TP::iter_type(stream), stream, stream.fill(), localtime(&tt), + fmt, fmt + 1); + VERIFY( tp.format_chars.empty() ); +} + +int main() +{ + test03(); + return 0; +} -- 2.30.2