]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/59531 (string_view overrun in copy operation)
authorEd Smith-Rowland <3dw4rd@verizon.net>
Fri, 24 Jan 2014 20:15:00 +0000 (20:15 +0000)
committerEdward Smith-Rowland <emsr@gcc.gnu.org>
Fri, 24 Jan 2014 20:15:00 +0000 (20:15 +0000)
2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>

PR libstdc++/59531
* testsuite/experimental/string_view/operations/copy/char/1.cc: New.
* testsuite/experimental/string_view/operations/copy/wchar_t/1.cc: New.

2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
    Peter A. Bigot <pab@pabigot.com>

PR libstdc++/59531
* include/experimental/string_view
(copy(_CharT*, size_type, size_type) const): Correct throw string.
Correct copy start location.

2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
    Peter A. Bigot <pab@pabigot.com>

PR libstdc++/59530
* include/experimental/string_view (operator[](size_type) const):
Fix one-off index error in debug check.
* testsuite/experimental/string_view/element_access/char/1.cc: Don't
test basic_string_view at size().
* testsuite/experimental/string_view/element_access/wchar_t/1.cc: Ditto.

2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
    Peter A. Bigot <pab@pabigot.com>

PR libstdc++/59529
* include/experimental/string_view
(basic_string_view(const _CharT*, size_type)): Don't care if len == 0.
* testsuite/experimental/string_view/operations/substr/char/1.cc:
Comment out catch of out_of_range; No terminating null
in basic_string_view.  Check begin == end.
* testsuite/experimental/string_view/operations/substr/wchar_t/1.cc:
Ditto.

Co-Authored-By: Peter A. Bigot <pab@pabigot.com>
From-SVN: r207060

libstdc++-v3/ChangeLog
libstdc++-v3/include/experimental/string_view
libstdc++-v3/testsuite/experimental/string_view/element_access/char/1.cc
libstdc++-v3/testsuite/experimental/string_view/element_access/wchar_t/1.cc
libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/experimental/string_view/operations/substr/char/1.cc
libstdc++-v3/testsuite/experimental/string_view/operations/substr/wchar_t/1.cc

index 30e4f22b8c82ae67020a93db71a3dd1ff12ee751..b575a92f5c4baa3a1e34022c4027323e4e60757c 100644 (file)
@@ -1,3 +1,39 @@
+2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
+
+       PR libstdc++/59531
+       * testsuite/experimental/string_view/operations/copy/char/1.cc: New.
+       * testsuite/experimental/string_view/operations/copy/wchar_t/1.cc: New.
+
+2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
+           Peter A. Bigot <pab@pabigot.com>
+
+       PR libstdc++/59531
+       * include/experimental/string_view
+       (copy(_CharT*, size_type, size_type) const): Correct throw string.
+       Correct copy start location.
+
+2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
+           Peter A. Bigot <pab@pabigot.com>
+
+       PR libstdc++/59530
+       * include/experimental/string_view (operator[](size_type) const):
+       Fix one-off index error in debug check.
+       * testsuite/experimental/string_view/element_access/char/1.cc: Don't
+       test basic_string_view at size().
+       * testsuite/experimental/string_view/element_access/wchar_t/1.cc: Ditto.
+
+2014-01-24  Ed Smith-Rowland  <3dw4rd@verizon.net>
+           Peter A. Bigot <pab@pabigot.com>
+
+       PR libstdc++/59529
+       * include/experimental/string_view
+       (basic_string_view(const _CharT*, size_type)): Don't care if len == 0.
+       * testsuite/experimental/string_view/operations/substr/char/1.cc:
+       Comment out catch of out_of_range; No terminating null
+       in basic_string_view.  Check begin == end.
+       * testsuite/experimental/string_view/operations/substr/wchar_t/1.cc:
+       Ditto.
+
 2014-01-24  Jonathan Wakely  <jwakely@redhat.com>
 
        PR libstdc++/59548
index ab6943d806bce74129c74d9448f57a6fe93c7f48..bebeb6b62ea2f356fcf91d98d351694838703af7 100644 (file)
@@ -117,7 +117,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       constexpr basic_string_view(const _CharT* __str, size_type __len)
       : _M_len{__str == nullptr ? 0 :__len},
-        _M_str{__str == nullptr || __len == 0 ? _S_empty_str : __str}
+        _M_str{__str == nullptr ? _S_empty_str : __str}
       { }
 
       basic_string_view&
@@ -182,7 +182,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator[](size_type __pos) const
       {
        // TODO: Assert to restore in a way compatible with the constexpr.
-       // _GLIBCXX_DEBUG_ASSERT(__pos <= this->_M_len);
+       // _GLIBCXX_DEBUG_ASSERT(__pos < this->_M_len);
        return *(this->_M_str + __pos);
       }
 
@@ -259,14 +259,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       copy(_CharT* __str, size_type __n, size_type __pos = 0) const
       {
        __glibcxx_requires_string_len(__str, __n);
-       if (__pos >= this->_M_len)
-         __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
-                                      "(which is %zu) >= this->size() "
+       if (__pos > this->_M_len)
+         __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos "
+                                      "(which is %zu) > this->size() "
                                       "(which is %zu)"),
                                   __pos, this->size());
        size_type __rlen{std::min(__n, size_type{this->_M_len  - __pos})};
        for (auto __begin = this->_M_str + __pos,
-            __end = this->_M_str + __rlen; __begin != __end;)
+            __end = __begin + __rlen; __begin != __end;)
          *__str++ = *__begin++;
        return __rlen;
       }
@@ -277,11 +277,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       constexpr basic_string_view
       substr(size_type __pos, size_type __n=npos) const
       {
-       return __pos < this->_M_len
+       return __pos <= this->_M_len
             ? basic_string_view{this->_M_str + __pos,
                                std::min(__n, size_type{this->_M_len  - __pos})}
-            : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
-                                            "(which is %zu) >= this->size() "
+            : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
+                                            "(which is %zu) > this->size() "
                                             "(which is %zu)"),
                                     __pos, this->size()), basic_string_view{});
       }
index 562ed20bc53c64865a004ac8ea7e27bfd9371b78..0a0b19f60be99d8c556d1bc77dbeba30b9d16645 100644 (file)
@@ -41,8 +41,9 @@ test01()
   csz01 = str01.size();
   cref cref1 = str01[csz01 - 1];
   VERIFY( cref1 == 'a' );
-  cref cref2 = str01[csz01];
-  VERIFY( cref2 == char() );
+  // Undefined behavior at size().
+  //cref cref2 = str01[csz01];
+  //VERIFY( cref2 == char() );
 
   // const_reference at(size_type pos) const;
   csz01 = str01.size();
index e125c03e5eea2bee95da42553afb38d69afd85ee..e3df84d41f083c2318197017cc26b804197f1547 100644 (file)
@@ -41,8 +41,9 @@ test01()
   csz01 = str01.size();
   cref cref1 = str01[csz01 - 1];
   VERIFY( cref1 == L'a' );
-  cref cref2 = str01[csz01];
-  VERIFY( cref2 == wchar_t() );
+  // Undefined behavior at size().
+  //cref cref2 = str01[csz01];
+  //VERIFY( cref2 == wchar_t() );
 
   // const_reference at(size_type pos) const;
   csz01 = str01.size();
diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc
new file mode 100644 (file)
index 0000000..25b2af1
--- /dev/null
@@ -0,0 +1,50 @@
+// { dg-options "-std=gnu++1y" }
+
+// Copyright (C) 2013 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/>.
+
+// basic_string_view::copy
+
+#include <experimental/string_view>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+bool
+test01()
+{
+  bool test [[gnu::unused]] = true;
+
+  typedef std::experimental::string_view::size_type csize_type;
+
+  const char str_lit01[] = "123456789A";
+  const std::experimental::string_view str01(str_lit01);
+  char buffer[4] = { 0 };
+
+  csize_type len = str01.copy(buffer, sizeof(buffer), 8);
+  VERIFY( 2 == len );
+  VERIFY( '9' == buffer[0] );
+
+  return test;
+}
+
+int
+main()
+{ 
+  test01();
+
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc
new file mode 100644 (file)
index 0000000..0348e1f
--- /dev/null
@@ -0,0 +1,51 @@
+// { dg-options "-std=gnu++1y" }
+
+// Copyright (C) 2013 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/>.
+
+// basic_string_view::copy
+
+#include <experimental/string_view>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+bool
+test01()
+{
+  bool test [[gnu::unused]] = true;
+
+  typedef std::experimental::wstring_view::size_type csize_type;
+  csize_type csz01;
+
+  const wchar_t str_lit01[] = L"123456789A";
+  const std::experimental::wstring_view str01(str_lit01);
+  wchar_t buffer[4] = { 0 };
+
+  csize_type len = str01.copy(buffer, sizeof(buffer), 8);
+  VERIFY( 2 == len );
+  VERIFY( L'9' == buffer[0] );
+
+  return test;
+}
+
+int
+main()
+{ 
+  test01();
+
+  return 0;
+}
index 1cd03f496117c79e5580cbf9cacfee8ec69cf45f..2f430ba165247d6e0611de2b32af06509a89f74a 100644 (file)
@@ -63,10 +63,8 @@ test01()
   {
     str02 = str01.substr(csz01);
     VERIFY( str02.size() == 0 );
-  }
-  catch(std::out_of_range& fail)
-  {
-    VERIFY( true ); // No terminating null in basic_string_view
+    VERIFY( str02.begin() == str01.end() );
+    VERIFY( true );
   }
   catch(...)
   {
index e81844a21f4a4fa5b3d7ed519aafe33284508d72..6be8838e0d2a649c8b007a0d77bea43bfde6dac1 100644 (file)
@@ -63,10 +63,8 @@ test01()
   {
     str02 = str01.substr(csz01);
     VERIFY( str02.size() == 0 );
-  }
-  catch(std::out_of_range& fail)
-  {
-    VERIFY( true ); // No terminating null in basic_string_view
+    VERIFY( str02.begin() == str01.end() );
+    VERIFY( true );
   }
   catch(...)
   {