diff options
| author | jvoisin | 2026-05-04 17:02:12 +0200 |
|---|---|---|
| committer | jvoisin | 2026-05-10 01:10:32 +0200 |
| commit | e7c86620bb0c0f8b868d3e4c8dcdebeeffb99631 (patch) | |
| tree | dee45e3d0ba90fe49e17df891b4752524ea868ac /tests/test_memcpy_overlap.c | |
| parent | e8e2d1214a49f3c268fb5f3a92e8144b23e35243 (diff) | |
Diffstat (limited to 'tests/test_memcpy_overlap.c')
| -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 | } | ||
