This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch] libstdc++/66327 don't pass null pointers to memcmp


On 29/05/15 19:52 +0000, Jörg Richter wrote:
Jonathan Wakely <jwakely <at> redhat.com> writes:

This is a rather brute force fix, but if noone has any better ideas
I'll commit this to the trunk and 4.9 and 5 branches tomorrow. (I
think it should go on the branches, because 4.9 is known to optimise
away null pointer checks after invalid calls to memcmp like this).

How about comparing only the length against 0?
The pointers must be != 0 if length > 0.

Like so? Yes, that's probably an improvement, because we need that
length anyway.

commit 8accacbfedb5e8b8bb5b232a060bce5166d6d7ef
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri May 29 23:48:55 2015 +0100

    	* include/bits/stl_algobase.h (__equal<true>::equal): Check length
    	instead of checking for null pointers.

diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index db065e2..7b9f257 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -812,11 +812,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         static bool
         equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
         {
-	  if (__first1 == 0 || __first2 == 0)
-	    return __first1 == __last1;
-
-	  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;
 	}
     };
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]