* [PATCH] c: Error on va_arg with function type [PR105149]
@ 2022-04-07 15:52 Jakub Jelinek
2022-04-07 16:05 ` Marek Polacek
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-04-07 15:52 UTC (permalink / raw)
To: Joseph S. Myers, Marek Polacek; +Cc: gcc-patches
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.
--- 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);
+}
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c: Error on va_arg with function type [PR105149]
2022-04-07 15:52 [PATCH] c: Error on va_arg with function type [PR105149] Jakub Jelinek
@ 2022-04-07 16:05 ` Marek Polacek
0 siblings, 0 replies; 2+ messages in thread
From: Marek Polacek @ 2022-04-07 16:05 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: Joseph S. Myers, gcc-patches
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-04-07 16:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-07 15:52 [PATCH] c: Error on va_arg with function type [PR105149] Jakub Jelinek
2022-04-07 16:05 ` Marek Polacek
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).