From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH] PR libstdc++/87787 avoid undefined null args to memcpy and memmove
Date: Mon, 07 Jan 2019 14:58:00 -0000 [thread overview]
Message-ID: <20190107145853.GA27400@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 670 bytes --]
The C++ char_traits and ctype APIs do not disallow null pointer
arguments, so we need explicit checks to ensure we don't forward null
pointers to memcpy or memmove.
PR libstdc++/87787
* include/bits/char_traits.h (char_traits::move): Do not pass null
pointers to memmove.
* include/bits/locale_facets.h
(ctype<char>::widen(const char*, const char*, char*)): Do not
pass null pointers to memcpy.
(ctype<char>::narrow(const char*, const char*, char, char*)):
Likewise.
(ctype<char>::do_widen(const char*, const char*, char*)):
Likewise.
(ctype<char>::do_narrow(const char*, const char*, char, char*)):
Likewise.
Tested powerpc64-linux, committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3015 bytes --]
commit 8322b49ba9dfa3cef33a78e4bc2bab0937c3849a
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Jan 7 14:38:22 2019 +0000
PR libstdc++/87787 avoid undefined null args to memcpy and memmove
The C++ char_traits and ctype APIs do not disallow null pointer
arguments, so we need explicit checks to ensure we don't forward null
pointers to memcpy or memmove.
PR libstdc++/87787
* include/bits/char_traits.h (char_traits::move): Do not pass null
pointers to memmove.
* include/bits/locale_facets.h
(ctype<char>::widen(const char*, const char*, char*)): Do not
pass null pointers to memcpy.
(ctype<char>::narrow(const char*, const char*, char, char*)):
Likewise.
(ctype<char>::do_widen(const char*, const char*, char*)):
Likewise.
(ctype<char>::do_narrow(const char*, const char*, char, char*)):
Likewise.
diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h
index a2ba5da910f..06e04ceaa34 100644
--- a/libstdc++-v3/include/bits/char_traits.h
+++ b/libstdc++-v3/include/bits/char_traits.h
@@ -183,6 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
char_traits<_CharT>::
move(char_type* __s1, const char_type* __s2, std::size_t __n)
{
+ if (__n == 0)
+ return __s1;
return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
__n * sizeof(char_type)));
}
diff --git a/libstdc++-v3/include/bits/locale_facets.h b/libstdc++-v3/include/bits/locale_facets.h
index 33cff652222..66ac9c07a5d 100644
--- a/libstdc++-v3/include/bits/locale_facets.h
+++ b/libstdc++-v3/include/bits/locale_facets.h
@@ -896,7 +896,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
if (_M_widen_ok == 1)
{
- __builtin_memcpy(__to, __lo, __hi - __lo);
+ if (__builtin_expect(__hi != __lo, true))
+ __builtin_memcpy(__to, __lo, __hi - __lo);
return __hi;
}
if (!_M_widen_ok)
@@ -961,7 +962,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
if (__builtin_expect(_M_narrow_ok == 1, true))
{
- __builtin_memcpy(__to, __lo, __hi - __lo);
+ if (__builtin_expect(__hi != __lo, true))
+ __builtin_memcpy(__to, __lo, __hi - __lo);
return __hi;
}
if (!_M_narrow_ok)
@@ -1100,7 +1102,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
virtual const char*
do_widen(const char* __lo, const char* __hi, char_type* __to) const
{
- __builtin_memcpy(__to, __lo, __hi - __lo);
+ if (__builtin_expect(__hi != __lo, true))
+ __builtin_memcpy(__to, __lo, __hi - __lo);
return __hi;
}
@@ -1153,7 +1156,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
do_narrow(const char_type* __lo, const char_type* __hi,
char __dfault __attribute__((__unused__)), char* __to) const
{
- __builtin_memcpy(__to, __lo, __hi - __lo);
+ if (__builtin_expect(__hi != __lo, true))
+ __builtin_memcpy(__to, __lo, __hi - __lo);
return __hi;
}
reply other threads:[~2019-01-07 14:58 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=20190107145853.GA27400@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).