If I got your point correctly here is this patch again with just the
change on '0' becoming 'nullptr' in assertions.
libstdc++: [_GLIBCXX_DEBUG] Review nullptr assertion diagnostics
Review null string checks to show:
_String != nullptr
rather than:
_String != 0
libstdc++-v3/ChangeLog:
* include/debug/debug.h: Use nullptr rather than '0' in
checks in post-C++11.
* include/debug/string: Likewise.
*
testsuite/21_strings/basic_string/operations/ends_with/char.cc: Use
__gnu_test::string.
*
testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc: Likewise.
*
testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc: Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc:
Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc:
Likewise.
*
testsuite/21_strings/basic_string/operations/starts_with/char.cc: Likewise..
Ok to commit ?
François
On 26/08/22 11:31, Jonathan Wakely wrote:
On Sun, 14 Aug 2022 at 16:34, François Dumont via Libstdc++
<libstd...@gcc.gnu.org> wrote:
I think we can add those checks.
Note that I wonder if it was needed as in basic_string_view I see usages
of __attribute__((__nonnull__)). But running the test I saw no impact
even after I try to apply this attribute to the starts_with/ends_with
methods themselves.
That should cause warnings, and does when I try it.
As you say, the relevant string_view constructor already has that anyway:
__attribute__((__nonnull__)) constexpr
basic_string_view(const _CharT* __str) noexcept
And so does string_view::find. The problem is that those only help if
the compiler inlines the calls to those functions and so can propagate
the null value all the way down to a function with the attribute.
Adding the attribute to the relevant starts_with, ends_with and
contains functions makes the diagnostics more likely to be emitted
without optimization.
Also note that several checks like the ones I am adding here are XFAILS
when using 'make check' because of the segfault rather than on a proper
debug checks. Would you prefer to add dg-require-debug-mode to those ?
libstdc++: [_GLIBCXX_DEBUG] Add basic_string::starts_with/ends_with
checks
Add simple checks on C string parameters which should not be null.
Review null string checks to show:
_String != nullptr
rather than:
_String != 0
I don't really like the extra complexity in the macros, but this does
seem like a nice improvement for what users see.
We could use __null for C++98, which is a compiler keyword that
expands to a null pointer constant, but I'm not sure if that would be
clear to all users or not. Maybe 0 is better.
.
diff --git a/libstdc++-v3/include/debug/debug.h b/libstdc++-v3/include/debug/debug.h
index 28e250f0c50..f4233760426 100644
--- a/libstdc++-v3/include/debug/debug.h
+++ b/libstdc++-v3/include/debug/debug.h
@@ -118,10 +118,17 @@ namespace __gnu_debug
__glibcxx_check_heap(_First,_Last)
# define __glibcxx_requires_heap_pred(_First,_Last,_Pred) \
__glibcxx_check_heap_pred(_First,_Last,_Pred)
-# define __glibcxx_requires_string(_String) \
+# if __cplusplus < 201103L
+# define __glibcxx_requires_string(_String) \
_GLIBCXX_DEBUG_PEDASSERT(_String != 0)
-# define __glibcxx_requires_string_len(_String,_Len) \
+# define __glibcxx_requires_string_len(_String,_Len) \
_GLIBCXX_DEBUG_PEDASSERT(_String != 0 || _Len == 0)
+# else
+# define __glibcxx_requires_string(_String) \
+ _GLIBCXX_DEBUG_PEDASSERT(_String != nullptr)
+# define __glibcxx_requires_string_len(_String,_Len) \
+ _GLIBCXX_DEBUG_PEDASSERT(_String != nullptr || _Len == 0)
+# endif
# define __glibcxx_requires_irreflexive(_First,_Last) \
__glibcxx_check_irreflexive(_First,_Last)
# define __glibcxx_requires_irreflexive2(_First,_Last) \
diff --git a/libstdc++-v3/include/debug/string b/libstdc++-v3/include/debug/string
index c32eb41eacd..574a78e3dac 100644
--- a/libstdc++-v3/include/debug/string
+++ b/libstdc++-v3/include/debug/string
@@ -50,14 +50,25 @@
#endif
#ifdef _GLIBCXX_DEBUG_PEDANTIC
-# define __glibcxx_check_string(_String) \
+# if __cplusplus < 201103L
+# define __glibcxx_check_string(_String) \
_GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != 0, \
__FILE__, __LINE__, \
__PRETTY_FUNCTION__);
-# define __glibcxx_check_string_len(_String,_Len) \
+# define __glibcxx_check_string_len(_String,_Len) \
_GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != 0 || _Len == 0, \
__FILE__, __LINE__, \
__PRETTY_FUNCTION__);
+# else
+# define __glibcxx_check_string(_String) \
+ _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != nullptr, \
+ __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__);
+# define __glibcxx_check_string_len(_String,_Len) \
+ _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != nullptr || _Len == 0, \
+ __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__);
+# endif
#else
# define __glibcxx_check_string(_String)
# define __glibcxx_check_string_len(_String,_Len)
@@ -75,8 +86,13 @@ namespace __gnu_debug
const char* __function __attribute__((__unused__)))
{
#ifdef _GLIBCXX_DEBUG_PEDANTIC
+# if __cplusplus < 201103L
_GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != 0 || __n == 0,
__file, __line, __function);
+# else
+ _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != nullptr || __n == 0,
+ __file, __line, __function);
+# endif
#endif
return __s;
}
@@ -90,8 +106,13 @@ namespace __gnu_debug
const char* __function __attribute__((__unused__)))
{
#ifdef _GLIBCXX_DEBUG_PEDANTIC
+# if __cplusplus < 201103L
_GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != 0,
__file, __line, __function);
+# else
+ _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != nullptr,
+ __file, __line, __function);
+# endif
#endif
return __s;
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/char.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/char.cc
index cfe18e0186c..3cf85121e36 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/char.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/char.cc
@@ -20,7 +20,7 @@
// basic_string ends_with
-#include <string>
+#include <testsuite_string.h>
#include <testsuite_hooks.h>
void
@@ -31,7 +31,7 @@ test01()
const char cstr_suf2[] = ".rgb";
const std::string_view sv_suf2(".rgb");
- const std::string s_test("slugs/slimy.jpg");
+ const __gnu_test::string s_test("slugs/slimy.jpg");
const auto cstr_in_slugs = s_test.ends_with(cstr_suf);
VERIFY( cstr_in_slugs );
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc
index 1f2a156bace..70ab867fa4e 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/nonnull.cc
@@ -1,10 +1,10 @@
// { dg-options "-std=gnu++20 -Wnonnull -O0" }
// { dg-do compile { target c++20 } }
-#include <string>
+#include <testsuite_string.h>
void
-test01(const std::string& s)
+test01(const __gnu_test::string& s)
{
s.ends_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
s.ends_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc
index d27e8246fb8..70b522ff69e 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/ends_with/wchar_t.cc
@@ -20,7 +20,7 @@
// basic_string ends_with
-#include <string>
+#include <testsuite_string.h>
#include <testsuite_hooks.h>
void
@@ -31,7 +31,7 @@ test01()
const wchar_t cstr_suf2[] = L".rgb";
const std::wstring_view sv_suf2(L".rgb");
- const std::wstring s_test(L"slugs/slimy.jpg");
+ const __gnu_test::wstring s_test(L"slugs/slimy.jpg");
const auto cstr_in_slugs = s_test.ends_with(cstr_suf);
VERIFY( cstr_in_slugs );
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/char.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/char.cc
index 5c0b3afc0b6..dddf51cee16 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/char.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/char.cc
@@ -20,7 +20,7 @@
// basic_string begins_with
-#include <string>
+#include <testsuite_string.h>
#include <testsuite_hooks.h>
void
@@ -31,7 +31,7 @@ test01()
const char cstr_dir2[] = "worms/";
const std::string_view sv_dir2("worms/");
- const std::string s_test("slugs/slimy.jpg");
+ const __gnu_test::string s_test("slugs/slimy.jpg");
const auto cstr_in_slugs = s_test.starts_with(cstr_dir);
VERIFY( cstr_in_slugs );
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc
index 8514359c877..34614be0ab6 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/nonnull.cc
@@ -1,10 +1,10 @@
// { dg-options "-std=gnu++20 -Wnonnull -O0" }
// { dg-do compile { target c++20 } }
-#include <string>
+#include <testsuite_string.h>
void
-test01(const std::string& s)
+test01(const __gnu_test::string& s)
{
s.starts_with((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
s.starts_with((char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc
index eda73212ea0..747b23ae5c2 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/starts_with/wchar_t.cc
@@ -20,7 +20,7 @@
// basic_string begins_with
-#include <string>
+#include <testsuite_string.h>
#include <testsuite_hooks.h>
void
@@ -31,7 +31,7 @@ test01()
const wchar_t cstr_dir2[] = L"worms/";
const std::wstring_view sv_dir2(L"worms/");
- const std::wstring s_test(L"slugs/slimy.jpg");
+ const __gnu_test::wstring s_test(L"slugs/slimy.jpg");
const auto cstr_in_slugs = s_test.starts_with(cstr_dir);
VERIFY( cstr_in_slugs );