From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed 3/4] libstdc++: Fix undefined behaviour in std::string
Date: Tue, 4 May 2021 23:19:48 +0100 [thread overview]
Message-ID: <20210504221948.GN3008@redhat.com> (raw)
In-Reply-To: <YJHIB/bhVbWRUygY@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 708 bytes --]
This fixes a ubsan error when constructing a string with a null pointer:
bits/basic_string.h:534:21: runtime error: applying non-zero offset 18446744073709551615 to null pointer
The _M_construct function only cares whether the second pointer is
non-null, so create a non-null value without undefined arithmetic.
We can also pass the random_access_iterator_tag directly to the
_M_construct function, to avoid going via the tag dispatching
_M_construct_aux, because we know we have pointers not integers here.
libstdc++-v3/ChangeLog:
* include/bits/basic_string.h (basic_string(const CharT*, const A&)):
Do not do arithmetic on null pointer.
Tested x86_64-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1722 bytes --]
commit 789c57bc5fe023fc6dc72ade4afcb0916ff788d3
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue May 4 15:49:38 2021
libstdc++: Fix undefined behaviour in std::string
This fixes a ubsan error when constructing a string with a null pointer:
bits/basic_string.h:534:21: runtime error: applying non-zero offset 18446744073709551615 to null pointer
The _M_construct function only cares whether the second pointer is
non-null, so create a non-null value without undefined arithmetic.
We can also pass the random_access_iterator_tag directly to the
_M_construct function, to avoid going via the tag dispatching
_M_construct_aux, because we know we have pointers not integers here.
libstdc++-v3/ChangeLog:
* include/bits/basic_string.h (basic_string(const CharT*, const A&)):
Do not do arithmetic on null pointer.
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index fba7c6f3354..84356adc7ae 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -531,7 +531,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
#endif
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
- { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
+ {
+ const _CharT* __end = __s ? __s + traits_type::length(__s)
+ // We just need a non-null pointer here to get an exception:
+ : reinterpret_cast<const _CharT*>(__alignof__(_CharT));
+ _M_construct(__s, __end, random_access_iterator_tag());
+ }
/**
* @brief Construct string as multiple characters.
next prev parent reply other threads:[~2021-05-04 22:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-04 22:17 [committed 1/4] libstdc++ Fix undefined behaviour in testsuite Jonathan Wakely
2021-05-04 22:18 ` [committed 2/4] libstdc++: Fix null dereference in pb_ds containers Jonathan Wakely
2021-05-04 22:19 ` Jonathan Wakely [this message]
2021-05-04 22:20 ` [committed 4/4] libstdc++: Fix null dereferences in std::promise Jonathan Wakely
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=20210504221948.GN3008@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).