aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-22.c
blob: 82eb5851c59b0bb6776de4a67a474cb8805adab0 (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
68
69
70
/* PR tree-optimization/91567 - Spurious -Wformat-overflow warnings building
   glibc (32-bit only)
   { dg-do compile }
   { dg-options "-O2 -Wall -ftrack-macro-expansion=0" }
   { dg-require-effective-target alloca } */

typedef __SIZE_TYPE__ size_t;

extern int sprintf (char*, const char*, ...);
extern size_t strlen (const char*);

void f (char *);

void g (char *s1, char *s2)
{
  char b[1025];
  size_t n = __builtin_strlen (s1), d = __builtin_strlen (s2);
  if (n + d + 1 >= 1025)
    return;

  /* Ranger can find ranges here:
     [1] n_6: size_t [0, 1023]
     [2] d_8: size_t [0, 1023]

     Whereas evrp can't really:
     [1] n_6: size_t [0, 9223372036854775805]
     [2] d_8: size_t [0, 9223372036854775805]

     This is causing the sprintf warning pass to issue a false
     positive here.  */

  sprintf (b, "%s.%s", s1, s2);     // { dg-bogus "\\\[-Wformat-overflow" "" { xfail *-*-* } }

  f (b);
}

/* Extracted from gcc/c-cppbuiltin.c.  */

void cpp_define (char*);

static void
builtin_define_type_minmax (const char *min_macro, const char *max_macro,
			    void *type)
{
  extern const char *suffix;
  char *buf;

  if (type)
    {
      buf = (char *) __builtin_alloca (__builtin_strlen (min_macro) + 2
				       + __builtin_strlen (suffix) + 1);
      sprintf (buf, "%s=0%s", min_macro, suffix);      // { dg-bogus "\\\[-Wformat-overflow" }
    }
  else
    {
      buf = (char *) __builtin_alloca (__builtin_strlen (min_macro) + 3
				       + __builtin_strlen (max_macro) + 6);
      sprintf (buf, "%s=(-%s - 1)", min_macro, max_macro);  // { dg-bogus "\\\[-Wformat-overflow" }
    }

  cpp_define (buf);
}

void
c_cpp_builtins (void *type)
{

  builtin_define_type_minmax ("__WCHAR_MIN__", "__WCHAR_MAX__", type);
  builtin_define_type_minmax ("__WINT_MIN__", "__WINT_MAX__", type);
}