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

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;

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

  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);
}