Commit e002afaa by Jonathan Wakely Committed by Jonathan Wakely

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.

From-SVN: r267651
parent a58fe3c5
2019-01-07 Jonathan Wakely <jwakely@redhat.com> 2019-01-07 Jonathan Wakely <jwakely@redhat.com>
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.
* doc/xml/manual/spine.xml: Update copyright years. * doc/xml/manual/spine.xml: Update copyright years.
* doc/xml/manual/status_cxx2017.xml: Adjust note about -lstdc++fs. * doc/xml/manual/status_cxx2017.xml: Adjust note about -lstdc++fs.
* doc/xml/manual/using.xml: Remove requirement to link with -lstdc++fs * doc/xml/manual/using.xml: Remove requirement to link with -lstdc++fs
......
...@@ -183,6 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -183,6 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
char_traits<_CharT>:: char_traits<_CharT>::
move(char_type* __s1, const char_type* __s2, std::size_t __n) 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, return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
__n * sizeof(char_type))); __n * sizeof(char_type)));
} }
......
...@@ -896,7 +896,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -896,7 +896,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
if (_M_widen_ok == 1) 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; return __hi;
} }
if (!_M_widen_ok) if (!_M_widen_ok)
...@@ -961,7 +962,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -961,7 +962,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
if (__builtin_expect(_M_narrow_ok == 1, true)) 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; return __hi;
} }
if (!_M_narrow_ok) if (!_M_narrow_ok)
...@@ -1100,7 +1102,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1100,7 +1102,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
virtual const char* virtual const char*
do_widen(const char* __lo, const char* __hi, char_type* __to) const 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; return __hi;
} }
...@@ -1153,7 +1156,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1153,7 +1156,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
do_narrow(const char_type* __lo, const char_type* __hi, do_narrow(const char_type* __lo, const char_type* __hi,
char __dfault __attribute__((__unused__)), char* __to) const 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; return __hi;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment