[libcxx-commits] [libcxx] r371925 - Add debug check for null pointers passed to <string_view>

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Sat Sep 14 12:55:28 PDT 2019


Author: ericwf
Date: Sat Sep 14 12:55:28 2019
New Revision: 371925

URL: http://llvm.org/viewvc/llvm-project?rev=371925&view=rev
Log:
Add debug check for null pointers passed to <string_view>

Added:
    libcxx/trunk/test/libcxx/debug/db_string_view.pass.cpp
Modified:
    libcxx/trunk/include/__string
    libcxx/trunk/include/string_view

Modified: libcxx/trunk/include/__string
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__string?rev=371925&r1=371924&r2=371925&view=diff
==============================================================================
--- libcxx/trunk/include/__string (original)
+++ libcxx/trunk/include/__string Sat Sep 14 12:55:28 2019
@@ -351,6 +351,18 @@ char_traits<wchar_t>::compare(const char
 #endif
 }
 
+
+template <class _Traits>
+_LIBCPP_INLINE_VISIBILITY
+_LIBCPP_CONSTEXPR
+inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
+#if _LIBCPP_DEBUG_LEVEL >= 1
+  return __s ? _Traits::length(__s) : (_VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, "p == nullptr", "null pointer pass to non-null argument of char_traits<...>::length")), 0);
+#else
+  return _Traits::length(__s);
+#endif
+}
+
 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 size_t
 char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT

Modified: libcxx/trunk/include/string_view
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string_view?rev=371925&r1=371924&r2=371925&view=diff
==============================================================================
--- libcxx/trunk/include/string_view (original)
+++ libcxx/trunk/include/string_view Sat Sep 14 12:55:28 2019
@@ -235,7 +235,7 @@ public:
 
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
     basic_string_view(const _CharT* __s)
-        : __data(__s), __size(_Traits::length(__s)) {}
+        : __data(__s), __size(std::__char_traits_length_checked<_Traits>(__s)) {}
 
     // [string.view.iterators], iterators
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY

Added: libcxx/trunk/test/libcxx/debug/db_string_view.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/debug/db_string_view.pass.cpp?rev=371925&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/debug/db_string_view.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/debug/db_string_view.pass.cpp Sat Sep 14 12:55:28 2019
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// UNSUPPORTED: windows
+// UNSUPPORTED: libcpp-no-if-constexpr
+// MODULES_DEFINES: _LIBCPP_DEBUG=1
+
+// Can't test the system lib because this test enables debug mode
+// UNSUPPORTED: with_system_cxx_lib
+
+// test container debugging
+
+#define _LIBCPP_DEBUG 1
+#include <string_view>
+
+#include "test_macros.h"
+#include "debug_mode_helper.h"
+
+void test_null_argument() {
+  EXPECT_DEATH(std::string_view(nullptr));
+  EXPECT_DEATH(std::string_view(NULL));
+  EXPECT_DEATH(std::string_view(static_cast<const char*>(0)));
+}
+
+int main(int, char**)
+{
+  test_null_argument();
+
+  return 0;
+}




More information about the libcxx-commits mailing list