c-family: ICE with [[gnu::nocf_check]] [PR106937]

Message ID 20220929224945.90798-1-polacek@redhat.com
State New
Headers
Series c-family: ICE with [[gnu::nocf_check]] [PR106937] |

Commit Message

Marek Polacek Sept. 29, 2022, 10:49 p.m. UTC
  When getting the name of an attribute, we ought to use
get_attribute_name, which handles both [[ ]] and __attribute__(())
forms.  Failure to do so may result in an ICE, like here.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c++/106937

gcc/c-family/ChangeLog:

	* c-pretty-print.cc (pp_c_attributes): Use get_attribute_name.
	(pp_c_attributes_display): Likewise.

gcc/testsuite/ChangeLog:

	* gcc.dg/fcf-protection-1.c: New test.
---
 gcc/c-family/c-pretty-print.cc          |  8 ++++----
 gcc/testsuite/gcc.dg/fcf-protection-1.c | 13 +++++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/fcf-protection-1.c


base-commit: c2ee70f20de8133a88553270073226b0f3f55f62
  

Comments

Jason Merrill Sept. 30, 2022, 1:12 p.m. UTC | #1
On 9/29/22 18:49, Marek Polacek wrote:
> When getting the name of an attribute, we ought to use
> get_attribute_name, which handles both [[ ]] and __attribute__(())
> forms.  Failure to do so may result in an ICE, like here.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

How do we print the attributes with this patch?  Don't we also want to 
print the namespace, and use [[]] in the output?

> 	PR c++/106937
> 
> gcc/c-family/ChangeLog:
> 
> 	* c-pretty-print.cc (pp_c_attributes): Use get_attribute_name.
> 	(pp_c_attributes_display): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/fcf-protection-1.c: New test.
> ---
>   gcc/c-family/c-pretty-print.cc          |  8 ++++----
>   gcc/testsuite/gcc.dg/fcf-protection-1.c | 13 +++++++++++++
>   2 files changed, 17 insertions(+), 4 deletions(-)
>   create mode 100644 gcc/testsuite/gcc.dg/fcf-protection-1.c
> 
> diff --git a/gcc/c-family/c-pretty-print.cc b/gcc/c-family/c-pretty-print.cc
> index efa1768f4d6..91f88b830e3 100644
> --- a/gcc/c-family/c-pretty-print.cc
> +++ b/gcc/c-family/c-pretty-print.cc
> @@ -863,7 +863,7 @@ pp_c_attributes (c_pretty_printer *pp, tree attributes)
>     pp_c_left_paren (pp);
>     for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
>       {
> -      pp_tree_identifier (pp, TREE_PURPOSE (attributes));
> +      pp_tree_identifier (pp, get_attribute_name (attributes));
>         if (TREE_VALUE (attributes))
>   	pp_c_call_argument_list (pp, TREE_VALUE (attributes));
>   
> @@ -875,7 +875,7 @@ pp_c_attributes (c_pretty_printer *pp, tree attributes)
>   }
>   
>   /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
> -   marked to be displayed on disgnostic.  */
> +   marked to be displayed on diagnostic.  */
>   
>   void
>   pp_c_attributes_display (c_pretty_printer *pp, tree a)
> @@ -888,7 +888,7 @@ pp_c_attributes_display (c_pretty_printer *pp, tree a)
>     for (; a != NULL_TREE; a = TREE_CHAIN (a))
>       {
>         const struct attribute_spec *as;
> -      as = lookup_attribute_spec (TREE_PURPOSE (a));
> +      as = lookup_attribute_spec (get_attribute_name (a));
>         if (!as || as->affects_type_identity == false)
>           continue;
>         if (c_dialect_cxx ()
> @@ -906,7 +906,7 @@ pp_c_attributes_display (c_pretty_printer *pp, tree a)
>          {
>            pp_separate_with (pp, ',');
>          }
> -      pp_tree_identifier (pp, TREE_PURPOSE (a));
> +      pp_tree_identifier (pp, get_attribute_name (a));
>         if (TREE_VALUE (a))
>          pp_c_call_argument_list (pp, TREE_VALUE (a));
>       }
> diff --git a/gcc/testsuite/gcc.dg/fcf-protection-1.c b/gcc/testsuite/gcc.dg/fcf-protection-1.c
> new file mode 100644
> index 00000000000..9d06feadfd1
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/fcf-protection-1.c
> @@ -0,0 +1,13 @@
> +/* PR c++/106937 */
> +/* { dg-options "-fcf-protection -w" } */
> +
> +[[gnu::nocf_check]] typedef void (*FuncPointerWithNoCfCheck)(void);
> +typedef void (*FuncPointer)(void);
> +[[gnu::nocf_check]] void testNoCfCheck();
> +void testNoCfCheck(){};
> +int [[gnu::nocf_check]] i;
> +void testNoCfCheckImpl(double i [[gnu::nocf_check]]) {}
> +void testNoCfCheckMismatch(FuncPointer f) {
> +  FuncPointerWithNoCfCheck fNoCfCheck = f;
> +  (*fNoCfCheck)();
> +}
> 
> base-commit: c2ee70f20de8133a88553270073226b0f3f55f62
  

Patch

diff --git a/gcc/c-family/c-pretty-print.cc b/gcc/c-family/c-pretty-print.cc
index efa1768f4d6..91f88b830e3 100644
--- a/gcc/c-family/c-pretty-print.cc
+++ b/gcc/c-family/c-pretty-print.cc
@@ -863,7 +863,7 @@  pp_c_attributes (c_pretty_printer *pp, tree attributes)
   pp_c_left_paren (pp);
   for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
     {
-      pp_tree_identifier (pp, TREE_PURPOSE (attributes));
+      pp_tree_identifier (pp, get_attribute_name (attributes));
       if (TREE_VALUE (attributes))
 	pp_c_call_argument_list (pp, TREE_VALUE (attributes));
 
@@ -875,7 +875,7 @@  pp_c_attributes (c_pretty_printer *pp, tree attributes)
 }
 
 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
-   marked to be displayed on disgnostic.  */
+   marked to be displayed on diagnostic.  */
 
 void
 pp_c_attributes_display (c_pretty_printer *pp, tree a)
@@ -888,7 +888,7 @@  pp_c_attributes_display (c_pretty_printer *pp, tree a)
   for (; a != NULL_TREE; a = TREE_CHAIN (a))
     {
       const struct attribute_spec *as;
-      as = lookup_attribute_spec (TREE_PURPOSE (a));
+      as = lookup_attribute_spec (get_attribute_name (a));
       if (!as || as->affects_type_identity == false)
         continue;
       if (c_dialect_cxx ()
@@ -906,7 +906,7 @@  pp_c_attributes_display (c_pretty_printer *pp, tree a)
        {
          pp_separate_with (pp, ',');
        }
-      pp_tree_identifier (pp, TREE_PURPOSE (a));
+      pp_tree_identifier (pp, get_attribute_name (a));
       if (TREE_VALUE (a))
        pp_c_call_argument_list (pp, TREE_VALUE (a));
     }
diff --git a/gcc/testsuite/gcc.dg/fcf-protection-1.c b/gcc/testsuite/gcc.dg/fcf-protection-1.c
new file mode 100644
index 00000000000..9d06feadfd1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fcf-protection-1.c
@@ -0,0 +1,13 @@ 
+/* PR c++/106937 */
+/* { dg-options "-fcf-protection -w" } */
+
+[[gnu::nocf_check]] typedef void (*FuncPointerWithNoCfCheck)(void);
+typedef void (*FuncPointer)(void);
+[[gnu::nocf_check]] void testNoCfCheck();
+void testNoCfCheck(){};
+int [[gnu::nocf_check]] i;
+void testNoCfCheckImpl(double i [[gnu::nocf_check]]) {}
+void testNoCfCheckMismatch(FuncPointer f) {
+  FuncPointerWithNoCfCheck fNoCfCheck = f;
+  (*fNoCfCheck)();
+}