re PR libstdc++/66441 (wstring_convert not working correctly)
authorJonathan Wakely <jwakely@redhat.com>
Mon, 8 Jun 2015 13:03:45 +0000 (14:03 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 8 Jun 2015 13:03:45 +0000 (14:03 +0100)
PR libstdc++/66441
* testsuite/22_locale/conversions/string/66441.cc: New.
* include/bits/locale_conv.h (__do_str_codecvt): Reserve enough space
in the output string for BOM and complete result.

From-SVN: r224222

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/locale_conv.h
libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc [new file with mode: 0644]

index 2aa8f0c3aa956dc6e0a5af9693d1b13be42884d6..bf985ccf7ee5873caaf4738a601430e5131c18c3 100644 (file)
@@ -1,5 +1,10 @@
 2015-06-08  Jonathan Wakely  <jwakely@redhat.com>
 
+       PR libstdc++/66441
+       * testsuite/22_locale/conversions/string/66441.cc: New.
+       * include/bits/locale_conv.h (__do_str_codecvt): Reserve enough space
+       in the output string for BOM and complete result.
+
        PR libstdc++/66417
        * src/c++11/codecvt.cc (write_utf16_code_point): Use adjust_byte_order
        for single UTF-16 units.
index 8b0a77ce317db779c500afacb74517c8528fd5d0..61b535c57de104cd3e174f80a7518eedae63c5e0 100644 (file)
@@ -60,12 +60,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       size_t __outchars = 0;
       auto __next = __first;
-      const auto __maxlen = __cvt.max_length();
+      const auto __maxlen = __cvt.max_length() + 1;
 
       codecvt_base::result __result;
       do
        {
-         __outstr.resize(__outstr.size() + (__last - __next) + __maxlen);
+         __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
          auto __outnext = &__outstr.front() + __outchars;
          auto const __outlast = &__outstr.back() + 1;
          __result = (__cvt.*__fn)(__state, __next, __last, __next,
diff --git a/libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc b/libstdc++-v3/testsuite/22_locale/conversions/string/66441.cc
new file mode 100644 (file)
index 0000000..b72edc8
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright (C) 2015 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 3, 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 COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+
+// libstdc++/66441
+
+#include <locale>
+#include <codecvt>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  // convert from UCS-4 to UTF16BE with BOM.
+  using cvt = std::codecvt_utf16<char32_t, 0x10FFFF, std::generate_header>;
+  std::wstring_convert<cvt, char32_t> conv;
+  auto to = conv.to_bytes(U"ab\u00e7");
+
+  VERIFY( to.length() == 8 );
+  VERIFY( (unsigned char)to[0] == 0xfe );
+  VERIFY( (unsigned char)to[1] == 0xff );
+  VERIFY( (unsigned char)to[2] == 0x00 );
+  VERIFY( (unsigned char)to[3] == 0x61 );
+  VERIFY( (unsigned char)to[4] == 0x00 );
+  VERIFY( (unsigned char)to[5] == 0x62 );
+  VERIFY( (unsigned char)to[6] == 0x00 );
+  VERIFY( (unsigned char)to[7] == 0xe7 );
+}
+
+int
+main()
+{
+  test01();
+}