aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2024-01-17 13:57:07 +0100
committerJakub Jelinek <jakub@redhat.com>2024-01-17 13:57:07 +0100
commita98a24ac65c1a0ee598e5b54d9e4c921a9744a08 (patch)
tree9c184fd9d6f609b8992211ea99fa879aad568f20 /gcc
parent2fb78d431ff3c05997ef31837d6eb319d84a4239 (diff)
downloadgcc-a98a24ac65c1a0ee598e5b54d9e4c921a9744a08.zip
gcc-a98a24ac65c1a0ee598e5b54d9e4c921a9744a08.tar.gz
gcc-a98a24ac65c1a0ee598e5b54d9e4c921a9744a08.tar.bz2
gimple-ssa-warn-access: Cast huge params to sizetype before using them in maybe_check_access_sizes [PR113410]
WHen a VLA is created with some very high precision size expression (say __int128, or _BitInt(65535) etc.), we cast it to sizetype, because we can't have arrays longer than what can be expressed in sizetype. But the maybe_check_access_sizes code when trying to determine ranges wasn't doing this but was using fixed buffers for the sizes. While __int128 could still be handled (fit into the buffers), obviously arbitrary _BitInt parameter ranges can't, they can be in the range of up to almost 20KB per number. It doesn't make sense to print such ranges though, no array can be larger than sizetype precision, and ranger's range_of_expr can handle NOP_EXPRs/CONVERT_EXPRs wrapping a PARM_DECL just fine, so the following patch just casts the excessively large counters for the range determination purposes to sizetype. 2024-01-17 Jakub Jelinek <jakub@redhat.com> PR middle-end/113410 * gimple-ssa-warn-access.cc (pass_waccess::maybe_check_access_sizes): If access_nelts is integral with larger precision than sizetype, fold_convert it to sizetype. * gcc.dg/bitint-72.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/gimple-ssa-warn-access.cc9
-rw-r--r--gcc/testsuite/gcc.dg/bitint-72.c16
2 files changed, 25 insertions, 0 deletions
diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index 850aaa0..cd083ab 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -3406,6 +3406,15 @@ pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype,
else
access_nelts = rwm->get (sizidx)->size;
+ /* If access_nelts is e.g. a PARM_DECL with larger precision than
+ sizetype, such as __int128 or _BitInt(34123) parameters,
+ cast it to sizetype. */
+ if (access_nelts
+ && INTEGRAL_TYPE_P (TREE_TYPE (access_nelts))
+ && (TYPE_PRECISION (TREE_TYPE (access_nelts))
+ > TYPE_PRECISION (sizetype)))
+ access_nelts = fold_convert (sizetype, access_nelts);
+
/* Format the value or range to avoid an explosion of messages. */
char sizstr[80];
tree sizrng[2] = { size_zero_node, build_all_ones_cst (sizetype) };
diff --git a/gcc/testsuite/gcc.dg/bitint-72.c b/gcc/testsuite/gcc.dg/bitint-72.c
new file mode 100644
index 0000000..a3c44fe
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bitint-72.c
@@ -0,0 +1,16 @@
+/* PR middle-end/113410 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c23" } */
+
+#if __BITINT_MAXWIDTH__ >= 905
+void bar (_BitInt(905) n, int[n]);
+#else
+void bar (int n, int[n]);
+#endif
+
+void
+foo (int n)
+{
+ int buf[n];
+ bar (n, buf);
+}