1 /* PR middle-end/78622 - [7 Regression] -Wformat-overflow/-fprintf-return-value
2    incorrect with overflow/wrapping
3    { dg-do compile }
4    { dg-options "-Wformat-overflow=2" }
5    The h and hh length modifiers are a C99 feature (see PR 78959).
6    { dg-require-effective-target c99_runtime }  */
7 
8 char buf[1];
9 
test_uchar_hhd(unsigned char x)10 int test_uchar_hhd (unsigned char x)
11 {
12   if (x < 64 || x > 2U * __SCHAR_MAX__ - 10)
13     return -1;
14 
15   return __builtin_sprintf (buf, "%hhd", x + 1);   /* { dg-warning "directive writing between 1 and 4 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
16 }
17 
test_uint_hhd(unsigned x)18 int test_uint_hhd (unsigned x)
19 {
20   if (x < 64 || x > 2U * __INT_MAX__ - 10)
21     return -1;
22 
23   return __builtin_sprintf (buf, "%hhd", x + 1);   /* { dg-warning "directive writing between 1 and 4 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
24 }
25 
test_schar_hhu(signed char x)26 int test_schar_hhu (signed char x)
27 {
28   if (x < -9 || x > 9)
29     return -1;
30 
31   return __builtin_sprintf (buf, "%hhu", x + 1);   /* { dg-warning "directive writing between 1 and 3 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
32 }
33 
test_ushort_hd(unsigned short x)34 int test_ushort_hd (unsigned short x)
35 {
36   if (x < 64 || x > 2U * __SHRT_MAX__ - 10)
37     return -1;
38 
39   return __builtin_sprintf (buf, "%hd", x + 1);   /* { dg-warning "directive writing between 1 and 6 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
40 }
41 
test_uint_d(unsigned x)42 int test_uint_d (unsigned x)
43 {
44   if (x < 64 || x > 2U * __INT_MAX__ - 10)
45     return -1;
46 
47   return __builtin_sprintf (buf, "%d", x + 1);   /* { dg-warning "directive writing between 1 and 11 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
48 }
49 
test_ulong_ld(unsigned long x)50 int test_ulong_ld (unsigned long x)
51 {
52   if (x < 64 || x > 2LU * __LONG_MAX__ - 10)
53     return -1;
54 
55   return __builtin_sprintf (buf, "%ld", x + 1);   /* { dg-warning "directive writing between 1 and 11 bytes into a region of size 1" "ilp32" { target { ilp32 } } } */
56   /* { dg-warning "directive writing between 1 and 20 bytes into a region of size 1" "lp64" { target { lp64 } } .-1 } */
57 }
58 
test_ullong_lld(unsigned long long x)59 int test_ullong_lld (unsigned long long x)
60 {
61   if (x < 64 || x > 2LLU * __LONG_LONG_MAX__ - 10)
62     return -1;
63 
64   return __builtin_sprintf (buf, "%lld", x + 1);   /* { dg-warning "directive writing between 1 and 20 bytes into a region of size 1" "int32plus" { target { int32plus } } } */
65 }
66