diff mbox series

c: Error on va_arg with function type [PR105149]

Message ID Yk8I1bPkZArnv0S+@tucnak
State New
Headers show
Series c: Error on va_arg with function type [PR105149] | expand

Commit Message

Jakub Jelinek April 7, 2022, 3:52 p.m. UTC
Hi!

In the PR Joseph said that the C standard for va_arg talks about
pointers to object type and as a function type is not object type,
it is invalid.

The following patch diagnoses it in the FE, instead of ICEing later on
when optimizations are turned on (and with -O0 doing something weird
at runtime).

Bootstrapped/regtested on powerpc64le-linux, ok for trunk?

2022-04-07  Jakub Jelinek  <jakub@redhat.com>

	PR c/105149
	* c-typeck.cc (c_build_va_arg): Reject function types.

	* gcc.dg/pr105149.c: New test.


	Jakub

Comments

Marek Polacek April 7, 2022, 4:05 p.m. UTC | #1
On Thu, Apr 07, 2022 at 05:52:53PM +0200, Jakub Jelinek wrote:
> Hi!
> 
> In the PR Joseph said that the C standard for va_arg talks about
> pointers to object type and as a function type is not object type,
> it is invalid.
> 
> The following patch diagnoses it in the FE, instead of ICEing later on
> when optimizations are turned on (and with -O0 doing something weird
> at runtime).
> 
> Bootstrapped/regtested on powerpc64le-linux, ok for trunk?

Ok, thanks.
 
> 2022-04-07  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c/105149
> 	* c-typeck.cc (c_build_va_arg): Reject function types.
> 
> 	* gcc.dg/pr105149.c: New test.
> 
> --- gcc/c/c-typeck.cc.jj	2022-03-23 10:29:20.172967605 +0100
> +++ gcc/c/c-typeck.cc	2022-04-07 12:26:17.236469809 +0200
> @@ -15896,6 +15896,12 @@ c_build_va_arg (location_t loc1, tree ex
>  		"type %qT", type);
>        return error_mark_node;
>      }
> +  else if (TREE_CODE (type) == FUNCTION_TYPE)
> +    {
> +      error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
> +		type);
> +      return error_mark_node;
> +    }
>    else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
>      warning_at (loc2, OPT_Wc___compat,
>  		"C++ requires promoted type, not enum type, in %<va_arg%>");
> --- gcc/testsuite/gcc.dg/pr105149.c.jj	2022-04-07 12:39:08.202711511 +0200
> +++ gcc/testsuite/gcc.dg/pr105149.c	2022-04-07 12:38:21.418364583 +0200
> @@ -0,0 +1,16 @@
> +/* PR c/105149 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +
> +#include <stdarg.h>
> +
> +void
> +foo (int s, ...)
> +{
> +  int e;
> +  va_list ap;
> +
> +  va_start (ap, s);
> +  e = va_arg (ap, int (void)) ();	/* { dg-error "second argument to 'va_arg' is a function type" } */
> +  va_end (ap);
> +}

Marek
diff mbox series

Patch

--- gcc/c/c-typeck.cc.jj	2022-03-23 10:29:20.172967605 +0100
+++ gcc/c/c-typeck.cc	2022-04-07 12:26:17.236469809 +0200
@@ -15896,6 +15896,12 @@  c_build_va_arg (location_t loc1, tree ex
 		"type %qT", type);
       return error_mark_node;
     }
+  else if (TREE_CODE (type) == FUNCTION_TYPE)
+    {
+      error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
+		type);
+      return error_mark_node;
+    }
   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
     warning_at (loc2, OPT_Wc___compat,
 		"C++ requires promoted type, not enum type, in %<va_arg%>");
--- gcc/testsuite/gcc.dg/pr105149.c.jj	2022-04-07 12:39:08.202711511 +0200
+++ gcc/testsuite/gcc.dg/pr105149.c	2022-04-07 12:38:21.418364583 +0200
@@ -0,0 +1,16 @@ 
+/* PR c/105149 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include <stdarg.h>
+
+void
+foo (int s, ...)
+{
+  int e;
+  va_list ap;
+
+  va_start (ap, s);
+  e = va_arg (ap, int (void)) ();	/* { dg-error "second argument to 'va_arg' is a function type" } */
+  va_end (ap);
+}