public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] ipa/102762 - fix ICE with invalid __builtin_va_arg_pack () use
@ 2021-10-15  7:33 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2021-10-15  7:33 UTC (permalink / raw)
  To: gcc-patches

We have to be careful to not break the argument space calculation.
If there's not enough arguments just do not append any.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2021-10-15  Richard Biener  <rguenther@suse.de>

	PR ipa/102762
	* tree-inline.c (copy_bb): Avoid underflowing nargs.

	* gcc.dg/torture/pr102762.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr102762.c | 11 +++++++++++
 gcc/tree-inline.c                       |  8 +++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr102762.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr102762.c b/gcc/testsuite/gcc.dg/torture/pr102762.c
new file mode 100644
index 00000000000..67c6b00ccea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr102762.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* We fail to diagnose the invalid __builtin_va_arg_pack use with -flto.  */
+/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
+
+void log_bad_request();
+void foo(a, b)
+     int a, b;
+{
+  log_bad_request(0, __builtin_va_arg_pack());  /* { dg-error "invalid use" } */
+  foo(0);
+}
diff --git a/gcc/tree-inline.c b/gcc/tree-inline.c
index e292a144967..b2c58ac4c3b 100644
--- a/gcc/tree-inline.c
+++ b/gcc/tree-inline.c
@@ -2117,7 +2117,13 @@ copy_bb (copy_body_data *id, basic_block bb,
 	      size_t nargs = nargs_caller;
 
 	      for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
-		nargs--;
+		{
+		  /* Avoid crashing on invalid IL that doesn't have a
+		     varargs function or that passes not enough arguments.  */
+		  if (nargs == 0)
+		    break;
+		  nargs--;
+		}
 
 	      /* Create the new array of arguments.  */
 	      size_t nargs_callee = gimple_call_num_args (call_stmt);
-- 
2.31.1

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-15  7:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-15  7:33 [PATCH] ipa/102762 - fix ICE with invalid __builtin_va_arg_pack () use Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).