diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/test_memcpy_overlap.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test_memcpy_overlap.c b/tests/test_memcpy_overlap.c new file mode 100644 index 0000000..f8c6639 --- /dev/null +++ b/tests/test_memcpy_overlap.c | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | /* fortify-headers' memcpy traps when src/dst overlap (but not when src == dst). | ||
| 6 | * This test exercises that overlap-detection branch, which is unique to this | ||
| 7 | * implementation. The pointer offset is hidden behind a volatile so -Wrestrict | ||
| 8 | * cannot see the overlap at compile time. */ | ||
| 9 | |||
| 10 | int main(int argc, char** argv) { | ||
| 11 | static char buffer[16] = "0123456789ABCDE"; | ||
| 12 | volatile int off = 2; /* hidden from the compiler so -Wrestrict won't see */ | ||
| 13 | char *p = buffer; | ||
| 14 | char *q = p + off; | ||
| 15 | |||
| 16 | /* dst == src: must NOT trap */ | ||
| 17 | memcpy(p, p, 8); | ||
| 18 | puts(buffer); | ||
| 19 | |||
| 20 | /* Overlapping src/dst (dst < src): must trap */ | ||
| 21 | CHK_FAIL_START | ||
| 22 | memcpy(p, q, 8); | ||
| 23 | CHK_FAIL_END | ||
| 24 | |||
| 25 | puts(buffer); | ||
| 26 | return ret; | ||
| 27 | } | ||
