This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] libstdc++/66327 don't pass null pointers to memcmp
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Jörg Richter <joerg dot richter at pdv-fs dot de>
- Cc: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Sat, 30 May 2015 12:44:08 +0100
- Subject: Re: [patch] libstdc++/66327 don't pass null pointers to memcmp
- Authentication-results: sourceware.org; auth=none
- References: <20150528223547 dot GT2985 at redhat dot com> <loom dot 20150529T214824-696 at post dot gmane dot org> <20150529232419 dot GD25933 at redhat dot com> <loom dot 20150530T113338-235 at post dot gmane dot org>
On 30/05/15 09:36 +0000, Jörg Richter wrote:
>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.
Yes, that is what I had in mind.
I think the second case can be handled equally by
precomputing std::min(__len1, __len2).
Yep, that's better too, I've tested and committed this patch. Thanks
for the suggestions.
commit 12ae71c3947824fa096113a418c7fe9e2ba89d19
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.
(__lexicographical_compare<true>::__lc): Only check shorter length.
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index db065e2..12eb7ec 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;
}
};
@@ -920,14 +918,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
const size_t __len1 = __last1 - __first1;
const size_t __len2 = __last2 - __first2;
- if (__len1 && __len2)
- {
- if (int __result = __builtin_memcmp(__first1, __first2,
- std::min(__len1, __len2)))
- {
- return __result < 0;
- }
- }
+ if (const size_t __len = std::min(__len1, __len2))
+ if (int __result = __builtin_memcmp(__first1, __first2, __len))
+ return __result < 0;
return __len1 < __len2;
}
};