public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9331] libstdc++: More fixes for null pointers used with std::char_traits Date: Tue, 28 Mar 2023 23:33:59 +0000 (GMT) [thread overview] Message-ID: <20230328233359.084803853567@sourceware.org> (raw) https://gcc.gnu.org/g:19193bf7274fbba33f64d1892a5aa5be072e85cb commit r12-9331-g19193bf7274fbba33f64d1892a5aa5be072e85cb Author: Jonathan Wakely <jwakely@redhat.com> Date: Tue Mar 28 11:12:58 2023 +0100 libstdc++: More fixes for null pointers used with std::char_traits The std::char_traits member functions require that [p,p+n) is a valid range, which is true for p==nullptr iff n==0. But we must not call memcpy, memset etc, in that case, as they require non-null pointers even when n==0. This std::char_traits<char> and std::char_traits<wchar_t> explicit specializations are already correct, but the primary template has some bugs. libstdc++-v3/ChangeLog: * include/bits/char_traits.h (char_traits::copy): Return without using memcpy if n==0. (char_traits::assign): Likewise for memset. (cherry picked from commit cb6f663f9d79d7134ae6ecaff9a25342c40aeb5d) Diff: --- libstdc++-v3/include/bits/char_traits.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h index cac1326d4b5..3c136f930ef 100644 --- a/libstdc++-v3/include/bits/char_traits.h +++ b/libstdc++-v3/include/bits/char_traits.h @@ -255,6 +255,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: copy(char_type* __s1, const char_type* __s2, std::size_t __n) { + if (__n == 0) + return __s1; #if __cplusplus >= 202002L if (std::__is_constant_evaluated()) { @@ -263,7 +265,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __s1; } #endif - __builtin_memcpy(__s1, __s2, __n * sizeof(char_type)); return __s1; } @@ -285,9 +286,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if _GLIBCXX17_CONSTEXPR (sizeof(_CharT) == 1 && __is_trivial(_CharT)) { - unsigned char __c; - __builtin_memcpy(&__c, __builtin_addressof(__a), 1); - __builtin_memset(__s, __c, __n); + if (__n) + { + unsigned char __c; + __builtin_memcpy(&__c, __builtin_addressof(__a), 1); + __builtin_memset(__s, __c, __n); + } } else {
reply other threads:[~2023-03-28 23:33 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=20230328233359.084803853567@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).