From: Jonathan Wakely Date: Mon, 8 Jun 2015 14:42:02 +0000 (+0100) Subject: re PR libstdc++/66354 ([UBSAN] stl_algobase.h:708:7: runtime error: null pointer... X-Git-Tag: releases/gcc-4.9.3~92 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=942528fbf250a3986079ea312de0c66928f5f536;p=thirdparty%2Fgcc.git re PR libstdc++/66354 ([UBSAN] stl_algobase.h:708:7: runtime error: null pointer passed as argument) PR libstdc++/66354 * include/bits/stl_algobase.h (__fill_a): Check length before calling memset. PR libstdc++/66327 * include/bits/stl_algobase.h (__equal::equal): Do not call memcmp for empty ranges. (__lexicographical_compare::__lc): Likewise. From-SVN: r224233 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index dc0a5497c5e0..278f4b45d286 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,14 @@ 2015-06-08 Jonathan Wakely + PR libstdc++/66354 + * include/bits/stl_algobase.h (__fill_a): Check length before calling + memset. + + PR libstdc++/66327 + * include/bits/stl_algobase.h (__equal::equal): Do not call + memcmp for empty ranges. + (__lexicographical_compare::__lc): Likewise. + Backport from mainline 2015-04-13 Jonathan Wakely diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index f7c11e931d7d..a8d3cc89ee3f 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -711,8 +711,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c) { const _Tp __tmp = __c; - __builtin_memset(__first, static_cast(__tmp), - __last - __first); + if (const size_t __len = __last - __first) + __builtin_memset(__first, static_cast(__tmp), __len); } /** @@ -818,8 +818,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static bool equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2) { - return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) - * (__last1 - __first1)); + if (const size_t __len = (__last1 - __first1)) + return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len); + return true; } }; @@ -923,9 +924,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { const size_t __len1 = __last1 - __first1; const size_t __len2 = __last2 - __first2; - const int __result = __builtin_memcmp(__first1, __first2, - std::min(__len1, __len2)); - return __result != 0 ? __result < 0 : __len1 < __len2; + if (const size_t __len = std::min(__len1, __len2)) + if (int __result = __builtin_memcmp(__first1, __first2, __len)) + return __result < 0; + return __len1 < __len2; } };