summaryrefslogtreecommitdiff
path: root/tests/test_memcpy_overlap.c
blob: f8c6639b691923f45ef123d0e6b3cbc26963b20c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "common.h"

#include <string.h>

/* 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;
}