From 0ccda1eae5964cbe33556e6a2dc43d180343d92e Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Tue, 19 Jun 2018 00:02:30 +0000 Subject: [PATCH] PR middle-end/82063 - issues with arguments enabled by -Wall gcc/ChangeLog: PR middle-end/82063 * calls.c (alloc_max_size): Correct a logic error/typo. Treat excessive arguments as infinite. Warn for invalid arguments. * doc/invoke.texi (-Walloc-size-larger-than): Update. gcc/testsuite/ChangeLog: PR middle-end/82063 * gcc.dg/Walloc-size-larger-than-1.c: New test. * gcc.dg/Walloc-size-larger-than-10.c: New test. * gcc.dg/Walloc-size-larger-than-11.c: New test. * gcc.dg/Walloc-size-larger-than-12.c: New test. * gcc.dg/Walloc-size-larger-than-13.c: New test. * gcc.dg/Walloc-size-larger-than-14.c: New test. * gcc.dg/Walloc-size-larger-than-15.c: New test. * gcc.dg/Walloc-size-larger-than-16.c: New test. * gcc.dg/Walloc-size-larger-than-2.c: New test. * gcc.dg/Walloc-size-larger-than-3.c: New test. * gcc.dg/Walloc-size-larger-than-4.c: New test. * gcc.dg/Walloc-size-larger-than-5.c: New test. * gcc.dg/Walloc-size-larger-than-6.c: New test. * gcc.dg/Walloc-size-larger-than-7.c: New test. * gcc.dg/Walloc-size-larger-than-8.c: New test. * gcc.dg/Walloc-size-larger-than-9.c: New test. * gcc.dg/Walloc-size-larger-than.c: New test. From-SVN: r261720 --- gcc/ChangeLog | 7 + gcc/calls.c | 122 ++++++++++-------- gcc/doc/invoke.texi | 3 + gcc/testsuite/ChangeLog | 21 +++ .../gcc.dg/Walloc-size-larger-than-1.c | 19 +++ .../gcc.dg/Walloc-size-larger-than-10.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than-11.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than-12.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than-13.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than-14.c | 30 +++++ .../gcc.dg/Walloc-size-larger-than-15.c | 30 +++++ .../gcc.dg/Walloc-size-larger-than-16.c | 32 +++++ .../gcc.dg/Walloc-size-larger-than-2.c | 20 +++ .../gcc.dg/Walloc-size-larger-than-3.c | 19 +++ .../gcc.dg/Walloc-size-larger-than-4.c | 19 +++ .../gcc.dg/Walloc-size-larger-than-5.c | 25 ++++ .../gcc.dg/Walloc-size-larger-than-6.c | 25 ++++ .../gcc.dg/Walloc-size-larger-than-7.c | 25 ++++ .../gcc.dg/Walloc-size-larger-than-8.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than-9.c | 27 ++++ .../gcc.dg/Walloc-size-larger-than.c | 13 ++ 21 files changed, 518 insertions(+), 54 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-1.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-10.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-11.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-12.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-13.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-14.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-15.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-16.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-2.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-3.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-4.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-5.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-6.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-7.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-8.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than-9.c create mode 100644 gcc/testsuite/gcc.dg/Walloc-size-larger-than.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 08d3a90e3a5e..7be04b62b297 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2018-06-18 Martin Sebor + + PR middle-end/82063 + * calls.c (alloc_max_size): Correct a logic error/typo. + Treat excessive arguments as infinite. Warn for invalid arguments. + * doc/invoke.texi (-Walloc-size-larger-than): Update. + 2018-06-14 Sebastian Huber Backport from mainline diff --git a/gcc/calls.c b/gcc/calls.c index b183be63dd4e..31663f4f67b3 100644 --- a/gcc/calls.c +++ b/gcc/calls.c @@ -1196,65 +1196,79 @@ static GTY(()) tree alloc_object_size_limit; static tree alloc_max_size (void) { - if (!alloc_object_size_limit) - { - alloc_object_size_limit = TYPE_MAX_VALUE (ssizetype); + if (alloc_object_size_limit) + return alloc_object_size_limit; - if (warn_alloc_size_limit) - { - char *end = NULL; - errno = 0; - unsigned HOST_WIDE_INT unit = 1; - unsigned HOST_WIDE_INT limit - = strtoull (warn_alloc_size_limit, &end, 10); + alloc_object_size_limit = TYPE_MAX_VALUE (ssizetype); - if (!errno) - { - if (end && *end) - { - /* Numeric option arguments are at most INT_MAX. Make it - possible to specify a larger value by accepting common - suffixes. */ - if (!strcmp (end, "kB")) - unit = 1000; - else if (!strcasecmp (end, "KiB") || strcmp (end, "KB")) - unit = 1024; - else if (!strcmp (end, "MB")) - unit = HOST_WIDE_INT_UC (1000) * 1000; - else if (!strcasecmp (end, "MiB")) - unit = HOST_WIDE_INT_UC (1024) * 1024; - else if (!strcasecmp (end, "GB")) - unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000; - else if (!strcasecmp (end, "GiB")) - unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024; - else if (!strcasecmp (end, "TB")) - unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000; - else if (!strcasecmp (end, "TiB")) - unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024; - else if (!strcasecmp (end, "PB")) - unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000; - else if (!strcasecmp (end, "PiB")) - unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024; - else if (!strcasecmp (end, "EB")) - unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000 - * 1000; - else if (!strcasecmp (end, "EiB")) - unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024 - * 1024; - else - unit = 0; - } + if (!warn_alloc_size_limit) + return alloc_object_size_limit; - if (unit) - { - wide_int w = wi::uhwi (limit, HOST_BITS_PER_WIDE_INT + 64); - w *= unit; - if (wi::ltu_p (w, alloc_object_size_limit)) - alloc_object_size_limit = wide_int_to_tree (ssizetype, w); - } - } + const char *optname = "-Walloc-size-larger-than="; + + char *end = NULL; + errno = 0; + unsigned HOST_WIDE_INT unit = 1; + unsigned HOST_WIDE_INT limit + = strtoull (warn_alloc_size_limit, &end, 10); + + /* If the value is too large to be represented use the maximum + representable value that strtoull sets limit to (setting + errno to ERANGE). */ + + if (end && *end) + { + /* Numeric option arguments are at most INT_MAX. Make it + possible to specify a larger value by accepting common + suffixes. */ + if (!strcmp (end, "kB")) + unit = 1000; + else if (!strcasecmp (end, "KiB") || !strcmp (end, "KB")) + unit = 1024; + else if (!strcmp (end, "MB")) + unit = HOST_WIDE_INT_UC (1000) * 1000; + else if (!strcasecmp (end, "MiB")) + unit = HOST_WIDE_INT_UC (1024) * 1024; + else if (!strcasecmp (end, "GB")) + unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000; + else if (!strcasecmp (end, "GiB")) + unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024; + else if (!strcasecmp (end, "TB")) + unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000; + else if (!strcasecmp (end, "TiB")) + unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024; + else if (!strcasecmp (end, "PB")) + unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000; + else if (!strcasecmp (end, "PiB")) + unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024; + else if (!strcasecmp (end, "EB")) + unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000 + * 1000; + else if (!strcasecmp (end, "EiB")) + unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024 + * 1024; + else + { + /* This could mean an unknown suffix or a bad prefix, like + "+-1". */ + warning_at (UNKNOWN_LOCATION, 0, + "invalid argument %qs to %qs", + warn_alloc_size_limit, optname); + /* Ignore the limit extracted by strtoull. */ + unit = 0; } } + + if (unit) + { + widest_int w = wi::mul (limit, unit); + if (w < wi::to_widest (alloc_object_size_limit)) + alloc_object_size_limit + = wide_int_to_tree (ptrdiff_type_node, w); + else + alloc_object_size_limit = build_all_ones_cst (size_type_node); + } + return alloc_object_size_limit; } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ce970ce34574..639298c85d4f 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -5171,6 +5171,9 @@ precision would exceed @code{SIZE_MAX / 2}. The option argument @var{n} may end in one of the standard suffixes designating a multiple of bytes such as @code{kB} and @code{KiB} for kilobyte and kibibyte, respectively, @code{MB} and @code{MiB} for megabyte and mebibyte, and so on. +@option{-Walloc-size-larger-than=}@var{PTRDIFF_MAX} is enabled by default. +Warnings controlled by the option can be disabled by specifying @var{n} +of @var{SIZE_MAX} or more. @xref{Function Attributes}. @item -Walloca diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2803fe4067d9..48b4f0f7a471 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,24 @@ +2018-06-18 Martin Sebor + + PR middle-end/82063 + * gcc.dg/Walloc-size-larger-than-1.c: New test. + * gcc.dg/Walloc-size-larger-than-10.c: New test. + * gcc.dg/Walloc-size-larger-than-11.c: New test. + * gcc.dg/Walloc-size-larger-than-12.c: New test. + * gcc.dg/Walloc-size-larger-than-13.c: New test. + * gcc.dg/Walloc-size-larger-than-14.c: New test. + * gcc.dg/Walloc-size-larger-than-15.c: New test. + * gcc.dg/Walloc-size-larger-than-16.c: New test. + * gcc.dg/Walloc-size-larger-than-2.c: New test. + * gcc.dg/Walloc-size-larger-than-3.c: New test. + * gcc.dg/Walloc-size-larger-than-4.c: New test. + * gcc.dg/Walloc-size-larger-than-5.c: New test. + * gcc.dg/Walloc-size-larger-than-6.c: New test. + * gcc.dg/Walloc-size-larger-than-7.c: New test. + * gcc.dg/Walloc-size-larger-than-8.c: New test. + * gcc.dg/Walloc-size-larger-than-9.c: New test. + * gcc.dg/Walloc-size-larger-than.c: New test. + 2018-06-13 Steven G. Kargl PR fortran/86110 diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-1.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-1.c new file mode 100644 index 000000000000..2e0b76554f9f --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-1.c @@ -0,0 +1,19 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1KB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + unsigned n = 0; + T (__builtin_malloc (n)); + + n = 1024; /* 1 kibibyte (KB or KiB) */ + T (__builtin_malloc (n)); + + n = 1025; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1025. exceeds maximum object size 1024" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-10.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-10.c new file mode 100644 index 000000000000..559309d55314 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-10.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1PiB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1024 * 1024 * 1024 * 1024 * 1024; /* 1 pebibyte (PiB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1125899906842625. exceeds maximum object size 1125899906842624" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-11.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-11.c new file mode 100644 index 000000000000..41e523c69586 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-11.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1PB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1000 * 1000 * 1000 * 1000 * 1000; /* 1 petabyte (PB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1000000000000001. exceeds maximum object size 1000000000000000" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-12.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-12.c new file mode 100644 index 000000000000..24269a8a102d --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-12.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1EiB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1024 * 1024 * 1024 * 1024 * 1024 * 1024; /* 1 exbibyte (EiB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1152921504606846977. exceeds maximum object size 1152921504606846976" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-13.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-13.c new file mode 100644 index 000000000000..b96e3892bd8f --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-13.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1EB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1000 * 1000 * 1000 * 1000 * 1000 * 1000; /* 1 exabyte (EB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1000000000000000001. exceeds maximum object size 1000000000000000000" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-14.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-14.c new file mode 100644 index 000000000000..e632e2236a35 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-14.c @@ -0,0 +1,30 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=123456789123456789123456789123456789 -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +/* Verify that an exceedingly large -Walloc-size-larger-than argument + with no suffix is accepted and treated as infinite. */ + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); + + n = __SIZE_MAX__ - 1; + T (__builtin_malloc (n)); + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-15.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-15.c new file mode 100644 index 000000000000..b699cc09c22a --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-15.c @@ -0,0 +1,30 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=123456789123456789123456789123456789gb -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +/* Verify that an exceeingly large -Walloc-size-larger-than argument + with a valid suffic is accepted and treated as infinite. */ + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); + + n = __SIZE_MAX__ - 1; + T (__builtin_malloc (n)); + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-16.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-16.c new file mode 100644 index 000000000000..837b69a36d8a --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-16.c @@ -0,0 +1,32 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1zb -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +/* Verify that an invalid -Walloc-size-larger-than argument is diagnosed + and rejected without changing the default setting of PTRDIFF_MAX. */ + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__ - 1; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} + +/* { dg-warning "invalid argument .1zb. to .-Walloc-size-larger-than=." "" { target *-*-* } 0 } */ diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-2.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-2.c new file mode 100644 index 000000000000..1ded37b0b5c1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-2.c @@ -0,0 +1,20 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1KiB -ftrack-macro-expansion=0" } +*/ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + unsigned n = 0; + T (__builtin_malloc (n)); + + n = 1024; /* 1 kibibyte (KB or KiB) */ + T (__builtin_malloc (n)); + + n = 1025; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1025. exceeds maximum object size 1024" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-3.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-3.c new file mode 100644 index 000000000000..500ddbf7c74d --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-3.c @@ -0,0 +1,19 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1kB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + unsigned n = 0; + T (__builtin_malloc (n)); + + n = 1000; /* 1 kilobyte (kB, not to be confused with KB or KiB) */ + T (__builtin_malloc (n)); + + n = 1001; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1001. exceeds maximum object size 1000" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-4.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-4.c new file mode 100644 index 000000000000..e4fde5a8eb90 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-4.c @@ -0,0 +1,19 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1MiB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + unsigned n = 0; + T (__builtin_malloc (n)); + + n = 1024 * 1024; /* 1 mebibyte (MiB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1048577. exceeds maximum object size 1048576" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-5.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-5.c new file mode 100644 index 000000000000..bfea259e0ad9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-5.c @@ -0,0 +1,25 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1MB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + __SIZE_TYPE__ n = 0; + T (__builtin_malloc (n)); + + n = 1000 * 1000; /* 1 megabyte (MB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1000001. exceeds maximum object size 1000000" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size 1000000" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size 1000000" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-6.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-6.c new file mode 100644 index 000000000000..1eb83a5f613e --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-6.c @@ -0,0 +1,25 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1GiB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + __SIZE_TYPE__ n = 0; + T (__builtin_malloc (n)); + + n = 1024 * 1024 * 1024; /* 1 gigibyte (GiB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1073741825. exceeds maximum object size 1073741824" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-7.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-7.c new file mode 100644 index 000000000000..5188203337d3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-7.c @@ -0,0 +1,25 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-O -Walloc-size-larger-than=1GB -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + __SIZE_TYPE__ n = 0; + T (__builtin_malloc (n)); + + n = 1000 * 1000 * 1000; /* 1 gigabyte (GB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1000000001. exceeds maximum object size 1000000000" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-8.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-8.c new file mode 100644 index 000000000000..4f84a027d795 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-8.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1TiB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1024 * 1024 * 1024 * 1024; /* 1 tebibyte (TiB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1099511627777. exceeds maximum object size 1099511627776" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than-9.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-9.c new file mode 100644 index 000000000000..f3927f1456e4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than-9.c @@ -0,0 +1,27 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile { target lp64 } } + { dg-options "-O -Walloc-size-larger-than=1TB -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + size_t n = 0; + T (__builtin_malloc (n)); + + n = (size_t)1000 * 1000 * 1000 * 1000; /* 1 terabyte (TB) */ + T (__builtin_malloc (n)); + + n += 1; + T (__builtin_malloc (n)); /* { dg-warning "argument 1 value .1000000000001. exceeds maximum object size 1000000000000" } */ + + n = __PTRDIFF_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ + + n = __SIZE_MAX__; + T (__builtin_malloc (n)); /* { dg-warning "exceeds maximum object size" } */ +} diff --git a/gcc/testsuite/gcc.dg/Walloc-size-larger-than.c b/gcc/testsuite/gcc.dg/Walloc-size-larger-than.c new file mode 100644 index 000000000000..8096ff1d3dd2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloc-size-larger-than.c @@ -0,0 +1,13 @@ +/* PR middle-end/82063 - issues with arguments enabled by -Wall + { dg-do compile } + { dg-options "-Walloc-size-larger-than=0 -ftrack-macro-expansion=0" } */ + +void sink (void*); + +#define T(x) sink (x) + +void f (void) +{ + T (__builtin_malloc (0)); + T (__builtin_malloc (1)); /* { dg-warning "argument 1 value .1. exceeds maximum object size 0" } */ +} -- 2.47.2