From e7c86620bb0c0f8b868d3e4c8dcdebeeffb99631 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 4 May 2026 17:02:12 +0200 Subject: Add a handful of tests --- tests/test_memcpy_overlap.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test_memcpy_overlap.c (limited to 'tests/test_memcpy_overlap.c') 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 @@ +#include "common.h" + +#include + +/* fortify-headers' memcpy traps when src/dst overlap (but not when src == dst). + * This test exercises that overlap-detection branch, which is unique to this + * implementation. The pointer offset is hidden behind a volatile so -Wrestrict + * cannot see the overlap at compile time. */ + +int main(int argc, char** argv) { + static char buffer[16] = "0123456789ABCDE"; + volatile int off = 2; /* hidden from the compiler so -Wrestrict won't see */ + char *p = buffer; + char *q = p + off; + + /* dst == src: must NOT trap */ + memcpy(p, p, 8); + puts(buffer); + + /* Overlapping src/dst (dst < src): must trap */ + CHK_FAIL_START + memcpy(p, q, 8); + CHK_FAIL_END + + puts(buffer); + return ret; +} -- cgit v1.3