diff options
| author | sin | 2015-01-29 10:47:54 +0000 |
|---|---|---|
| committer | sin | 2015-01-29 10:56:15 +0000 |
| commit | 1e306fd64276aaa68eae4ffa26b68cd84092c37e (patch) | |
| tree | b008c16ac1aeaa2669f308cd0e822c35bcbb3770 /include/string.h | |
| parent | 52d4c979802345f03193258af7f120de4bcae1ff (diff) | |
Trap if memcpy() pointers are overlapping
Maybe this should only be done if _FORTIFY_SOURCE > 1.
Diffstat (limited to '')
| -rw-r--r-- | include/string.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/include/string.h b/include/string.h index 565f70e..60629d1 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -10,7 +10,13 @@ void * | |||
| 10 | __fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) | 10 | __fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n) |
| 11 | { | 11 | { |
| 12 | size_t bos = __builtin_object_size(dest, 0); | 12 | size_t bos = __builtin_object_size(dest, 0); |
| 13 | char *d = dest; | ||
| 14 | const char *s = src; | ||
| 13 | 15 | ||
| 16 | /* trap if pointers are overlapping */ | ||
| 17 | if ((d <= s && d + n > s) || | ||
| 18 | (s <= d && s + n > d)) | ||
| 19 | __builtin_trap(); | ||
| 14 | if (n > bos) | 20 | if (n > bos) |
| 15 | __builtin_trap(); | 21 | __builtin_trap(); |
| 16 | return memcpy(dest, src, n); | 22 | return memcpy(dest, src, n); |
