fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 1e306fd64276aaa68eae4ffa26b68cd84092c37e
parent 52d4c979802345f03193258af7f120de4bcae1ff
Author: sin <sin@2f30.org>
Date:   Thu, 29 Jan 2015 10:47:54 +0000

Trap if memcpy() pointers are overlapping

Maybe this should only be done if _FORTIFY_SOURCE > 1.

Diffstat:
Minclude/string.h | 6++++++
1 file changed, 6 insertions(+), 0 deletions(-)

diff --git a/include/string.h b/include/string.h @@ -10,7 +10,13 @@ void * __fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) { size_t bos = __builtin_object_size(dest, 0); + char *d = dest; + const char *s = src; + /* trap if pointers are overlapping */ + if ((d <= s && d + n > s) || + (s <= d && s + n > d)) + __builtin_trap(); if (n > bos) __builtin_trap(); return memcpy(dest, src, n);