fortify-headers

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

test_memcpy_overlap.c (734B)


      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 }