From 6bc1236d395187334051b99e2fcd715a54753f33 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sat, 14 Sep 2019 19:55:28 +0000 Subject: [PATCH] Add debug check for null pointers passed to llvm-svn: 371925 --- libcxx/include/__string | 12 ++++++++ libcxx/include/string_view | 2 +- libcxx/test/libcxx/debug/db_string_view.pass.cpp | 36 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 libcxx/test/libcxx/debug/db_string_view.pass.cpp diff --git a/libcxx/include/__string b/libcxx/include/__string index a88b976..b4c8815 100644 --- a/libcxx/include/__string +++ b/libcxx/include/__string @@ -351,6 +351,18 @@ char_traits::compare(const char_type* __s1, const char_type* __s2, size #endif } + +template +_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::length(const char_type* __s) _NOEXCEPT diff --git a/libcxx/include/string_view b/libcxx/include/string_view index 0444831..3a30db8 100644 --- a/libcxx/include/string_view +++ b/libcxx/include/string_view @@ -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 diff --git a/libcxx/test/libcxx/debug/db_string_view.pass.cpp b/libcxx/test/libcxx/debug/db_string_view.pass.cpp new file mode 100644 index 0000000..0753aea --- /dev/null +++ b/libcxx/test/libcxx/debug/db_string_view.pass.cpp @@ -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 + +#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(0))); +} + +int main(int, char**) +{ + test_null_argument(); + + return 0; +} -- 2.7.4