File: builtin-sprintf-warn-22.c

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (70 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (2)
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);
}