aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/write-to-string-literal-1.c
blob: 46e907a856091604a524273f8f89aa1cd04eef21 (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
65
66
67
#include <string.h>

#ifdef __cplusplus
#define CONST_CAST(type) const_cast<type>
#else
#define CONST_CAST(type)
#endif

/* PR analyzer/95007.  */

void test_1 (void)
{
  char *s = CONST_CAST(char *)("foo");
  s[0] = 'g'; /* { dg-warning "write to string literal" } */
}

/* PR c/83347.  */

void test_2 (void)
{
  // Technically irrelevant for C++ as fpermissive will warn about invalid conversion.
  memcpy (CONST_CAST(char *)("abc"), "def", 3); /* { dg-warning "write to string literal" } */
}

/* PR c/83347.  */

static char * __attribute__((noinline))
called_by_test_3 (void)
{
  return (char *)"foo";
}

void test_3 (void)
{
  char *s = called_by_test_3 ();
  s[1] = 'a'; /* { dg-warning "write to string literal" } */
}

static char * __attribute__((noinline))
called_by_test_4 (int flag)
{
  if (flag)
    return (char *)"foo";
  else
    return (char *)"bar";
}

void test_4 (void)
{
  char *s = called_by_test_4 (0);
  s[1] = 'z'; /* { dg-warning "write to string literal" } */
}

static char * __attribute__((noinline))
called_by_test_5 (int flag)
{
  if (flag)
    return (char *)"foo";
  else
    return (char *)"bar";
}

void test_5 (int flag)
{
  char *s = called_by_test_5 (flag);
  s[1] = 'z'; /* We miss this one, unless we disable state merging.  */
}