aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstringop-truncation-5.c
blob: f66c0ce1e3f19124e38b098a2912133ab6dfa3e8 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* PR tree-optimization/87028 - false positive -Wstringop-truncation
   strncpy with global variable source string
   { dg-do compile }
   { dg-options "-O2 -Wstringop-truncation" } */

char *strncpy (char *, const char *, __SIZE_TYPE__);

#define STR   "1234567890"

struct S
{
  char a[5], b[5];
};

const char arr[] = STR;
const char* const ptr = STR;

const char arr2[][10] = { "123", STR };

void test_literal (struct S *s)
{
  strncpy (s->a, STR, sizeof s->a - 1);     /* { dg-bogus "\\\[-Wstringop-truncation]" } */
  s->a[sizeof s->a - 1] = '\0';
}

void test_global_arr (struct S *s)
{
  strncpy (s->a, arr, sizeof s->a - 1);     /* { dg-bogus "\\\[-Wstringop-truncation]" } */
  s->a [sizeof s->a - 1] = '\0';
}

void test_global_arr2 (struct S *s)
{
  strncpy (s->a, arr2[1], sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
  s->a [sizeof s->a - 1] = '\0';

  strncpy (s->b, arr2[0], sizeof s->a - 1);
}

void test_global_ptr (struct S *s)
{
  strncpy (s->a, ptr, sizeof s->a - 1);     /* { dg-bogus "\\\[-Wstringop-truncation]" } */
  s->a [sizeof s->a - 1] = '\0';
}

void test_local_arr (struct S *s)
{
  const char arr[] = STR;
  strncpy (s->a, arr, sizeof s->a - 1);
  s->a [sizeof s->a - 1] = '\0';
}

void test_local_ptr (struct S *s)
{
  const char* const ptr = STR;
  strncpy (s->a, ptr, sizeof s->a - 1);     /* { dg-bogus "\\\[-Wstringop-truncation]" } */
  s->a [sizeof s->a - 1] = '\0';
}

void test_compound_literal (struct S *s)
{
  strncpy (s->a, (char[]){ STR }, sizeof s->a - 1);
  s->a [sizeof s->a - 1] = '\0';
}