From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Disable std::string{, _view} construction from nullptr (P2166R1)
Date: Mon, 4 Oct 2021 15:24:24 +0100 [thread overview]
Message-ID: <YVsOmEoc1tFw1MKC@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 778 bytes --]
Implement this C++23 feature. Because construction from a null pointer
is undefined, we can implement it for C++11 and up, turning undefined
behaviour into a compilation error.
libstdc++-v3/ChangeLog:
* include/bits/basic_string.h (basic_string(nullptr_t)): Define
as deleted.
(operator=(nullptr_t)): Likewise.
* include/bits/cow_string.h (basic_string(nullptr_t)): Likewise.
(operator=(nullptr_t)): Likewise.
* include/std/string_view (basic_string_view(nullptr_t)):
Likewise.
* testsuite/21_strings/basic_string/cons/char/nullptr.cc: New test.
* testsuite/21_strings/basic_string_view/cons/char/nonnull.cc:
Change dg-warning to dg-error.
* testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc:
Likewise.
Tested powerpc64le-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4294 bytes --]
commit cf876562c592193732f869e9f96034a42d0fad89
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Fri Oct 1 21:27:24 2021
libstdc++: Disable std::string{,_view} construction from nullptr (P2166R1)
Implement this C++23 feature. Because construction from a null pointer
is undefined, we can implement it for C++11 and up, turning undefined
behaviour into a compilation error.
libstdc++-v3/ChangeLog:
* include/bits/basic_string.h (basic_string(nullptr_t)): Define
as deleted.
(operator=(nullptr_t)): Likewise.
* include/bits/cow_string.h (basic_string(nullptr_t)): Likewise.
(operator=(nullptr_t)): Likewise.
* include/std/string_view (basic_string_view(nullptr_t)):
Likewise.
* testsuite/21_strings/basic_string/cons/char/nullptr.cc: New test.
* testsuite/21_strings/basic_string_view/cons/char/nonnull.cc:
Change dg-warning to dg-error.
* testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc:
Likewise.
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 24c454d863a..68c388408f0 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -623,6 +623,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_M_construct(__str.begin(), __str.end());
}
+ basic_string(nullptr_t) = delete;
+ basic_string& operator=(nullptr_t) = delete;
#endif // C++11
/**
diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h
index ba4a8cc2e98..f0540128364 100644
--- a/libstdc++-v3/include/bits/cow_string.h
+++ b/libstdc++-v3/include/bits/cow_string.h
@@ -667,6 +667,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
else
_M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
}
+
+ basic_string(nullptr_t) = delete;
+ basic_string& operator=(nullptr_t) = delete;
#endif // C++11
/**
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index d8cbee9bee0..996b03f9346 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -168,6 +168,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif // C++23
#endif // C++20
+ basic_string_view(nullptr_t) = delete;
+
constexpr basic_string_view&
operator=(const basic_string_view&) noexcept = default;
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
new file mode 100644
index 00000000000..fdb24aeeb89
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
@@ -0,0 +1,4 @@
+// { dg-do compile { target c++11 } }
+#include <string>
+
+std::string s = nullptr; // { dg-error "deleted" "P2166R1" }
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
index b33af088a3d..f42f95e2fdd 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
@@ -25,5 +25,5 @@ test01()
{
std::string_view s((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
std::string_view t((char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
- std::string_view u(nullptr); // { dg-warning "\\\[-Wnonnull" }
+ std::string_view u(nullptr); // { dg-error "deleted" }
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
index 4c1013177eb..e480f7c4923 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
@@ -25,5 +25,5 @@ test01()
{
std::wstring_view s((const wchar_t*)nullptr); // { dg-warning "\\\[-Wnonnull" }
std::wstring_view t((wchar_t*)nullptr); // { dg-warning "\\\[-Wnonnull" }
- std::wstring_view u(nullptr); // { dg-warning "\\\[-Wnonnull" }
+ std::wstring_view u(nullptr); // { dg-error "deleted" }
}
reply other threads:[~2021-10-04 14:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YVsOmEoc1tFw1MKC@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).