This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] Fix libstdc++/12439
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: libstdc++ at gcc dot gnu dot org
- Cc: Benjamin Kosnik <bkoz at redhat dot com>
- Date: Wed, 01 Oct 2003 15:00:08 +0200
- Subject: [Patch] Fix libstdc++/12439
Hi,
seems quite straightforward to me.
Tested x86-linux, if nobody objects will commit later today.
Paolo.
///////////
2003-10-01 Paolo Carlini <pcarlini@unitus.it>
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.
diff -urN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
--- libstdc++-v3-orig/include/bits/locale_facets.tcc 2003-09-30 13:38:47.000000000 +0200
+++ libstdc++-v3/include/bits/locale_facets.tcc 2003-10-01 14:20:52.000000000 +0200
@@ -2000,21 +2000,20 @@
template<typename _CharT, typename _OutIter>
_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<ctype<_CharT> >(__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 @@
}
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 -urN libstdc++-v3-orig/testsuite/22_locale/time_put/put/char/12439_1.cc libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc
--- libstdc++-v3-orig/testsuite/22_locale/time_put/put/char/12439_1.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc 2003-10-01 14:09:49.000000000 +0200
@@ -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 <locale>
+#include <sstream>
+#include <ctime>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+class TP : public std::time_put<char>
+{
+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 -urN libstdc++-v3-orig/testsuite/22_locale/time_put/put/char/12439_3.cc libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc
--- libstdc++-v3-orig/testsuite/22_locale/time_put/put/char/12439_3.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc 2003-10-01 14:11:53.000000000 +0200
@@ -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 <locale>
+#include <sstream>
+#include <ctime>
+#include <testsuite_hooks.h>
+
+class TP : public std::time_put<char>
+{
+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 -urN libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc
--- libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc 2003-10-01 14:38:04.000000000 +0200
@@ -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 <locale>
+#include <sstream>
+#include <ctime>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+class TP : public std::time_put<wchar_t>
+{
+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 -urN libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc
--- libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc 2003-10-01 14:18:00.000000000 +0200
@@ -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 <locale>
+#include <sstream>
+#include <ctime>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+// libstdc++/12439
+// time_put::put writes narrowed characters to output iterator
+void test02()
+{
+ using namespace std;
+ bool test __attribute__((unused)) = true;
+
+ typedef time_put<wchar_t> 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<tp_type>(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 -urN libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc
--- libstdc++-v3-orig/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc 1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc 2003-10-01 14:38:28.000000000 +0200
@@ -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 <locale>
+#include <sstream>
+#include <ctime>
+#include <testsuite_hooks.h>
+
+class TP : public std::time_put<wchar_t>
+{
+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;
+}