re PR libstdc++/12439 (Problems in time_put::put)
authorPaolo Carlini <pcarlini@unitus.it>
Wed, 1 Oct 2003 14:32:05 +0000 (16:32 +0200)
committerPaolo Carlini <paolo@gcc.gnu.org>
Wed, 1 Oct 2003 14:32:05 +0000 (14:32 +0000)
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.

From-SVN: r71976

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/locale_facets.tcc
libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/22_locale/time_put/put/char/12439_3.cc [new file with mode: 0644]
libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_2.cc [new file with mode: 0644]
libstdc++-v3/testsuite/22_locale/time_put/put/wchar_t/12439_3.cc [new file with mode: 0644]

index 2c0b50b01d6157ce1016197259a5ea81e58c4d9c..4b776c89e216a0120e329012bab9a87e8e77ee13 100644 (file)
@@ -1,3 +1,14 @@
+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.
+
 2003-09-30  Paolo Carlini  <pcarlini@unitus.it>
 
        * include/bits/stl_algo.h: Minor cosmetic reformattings.
index 4c8160382811546c5699e4f0f27f753d21b3df67..62127b9e7fd79bcdc53d27ffe0d64af1f20d1f24 100644 (file)
@@ -2000,21 +2000,20 @@ namespace std
   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 @@ 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 (file)
index 0000000..bc3bb86
--- /dev/null
@@ -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 --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 (file)
index 0000000..c1d0167
--- /dev/null
@@ -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 --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 (file)
index 0000000..ce2d0d5
--- /dev/null
@@ -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 --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 (file)
index 0000000..6715cc5
--- /dev/null
@@ -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 --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 (file)
index 0000000..0bcae33
--- /dev/null
@@ -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;
+}