Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-25 Thread Jason Merrill via Gcc-patches

On 5/25/22 14:49, Marek Polacek wrote:

On Tue, May 24, 2022 at 08:22:22AM -0400, Jason Merrill wrote:

On 4/29/22 10:12, Marek Polacek wrote:

This patch fixes crashes with invalid attributes.  Arguably it could
make sense to assert seen_error() too.


So in this testcase we have TREE_CHAIN of a TREE_LIST pointing to
error_mark_node?  Can we avoid that?


Yes and yes.  Sorry, my previous fix was lousy.  This one is better:

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


OK, thanks.


-- >8 --
When chaining attributes, attr_chainon should be used rather than plain
chainon, so that we don't end up with a TREE_LIST where one of the elements
is error_mark_node, which causes problems.  parser.cc has already been
fixed to use attr_chainon, but decl.cc has not.  Until now.

PR c++/96637

gcc/cp/ChangeLog:

* cp-tree.h (attr_chainon): Declare.
* decl.cc (start_decl): Use attr_chainon.
(grokdeclarator): Likewise.
* parser.cc (cp_parser_statement): No longer static.

gcc/testsuite/ChangeLog:

* g++.dg/parse/error64.C: New test.
---
  gcc/cp/cp-tree.h |  1 +
  gcc/cp/decl.cc   | 19 ++-
  gcc/cp/parser.cc |  2 +-
  gcc/testsuite/g++.dg/parse/error64.C |  4 
  4 files changed, 16 insertions(+), 10 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/parse/error64.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index ba986e892b6..d77fd1eb8a9 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7235,6 +7235,7 @@ extern void inject_this_parameter (tree, cp_cv_quals);
  extern location_t defparse_location (tree);
  extern void maybe_show_extern_c_location (void);
  extern bool literal_integer_zerop (const_tree);
+extern tree attr_chainon (tree, tree);
  
  /* in pt.cc */

  extern tree canonical_type_parameter  (tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 381259cb9cf..b1ea838ce8b 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -5557,7 +5557,7 @@ start_decl (const cp_declarator *declarator,
*pushed_scope_p = NULL_TREE;
  
if (prefix_attributes != error_mark_node)

-attributes = chainon (attributes, prefix_attributes);
+attributes = attr_chainon (attributes, prefix_attributes);
  
decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,

 );
@@ -12728,9 +12728,10 @@ grokdeclarator (const cp_declarator *declarator,
   as a whole.  */
late_attrs = splice_template_attributes (, type);
  returned_attrs = decl_attributes (,
-   chainon (returned_attrs, attrs),
+   attr_chainon (returned_attrs,
+ attrs),
attr_flags);
- returned_attrs = chainon (late_attrs, returned_attrs);
+ returned_attrs = attr_chainon (late_attrs, returned_attrs);
}
  
inner_declarator = declarator->declarator;

@@ -12781,8 +12782,8 @@ grokdeclarator (const cp_declarator *declarator,
  
  	   The optional attribute-specifier-seq appertains to the

   array.  */
-   returned_attrs = chainon (returned_attrs,
- declarator->std_attributes);
+   returned_attrs = attr_chainon (returned_attrs,
+  declarator->std_attributes);
  break;
  
  	case cdk_function:

@@ -13122,9 +13123,9 @@ grokdeclarator (const cp_declarator *declarator,
/* transaction_safe applies to the type, but
   transaction_safe_dynamic applies to the function.  */
if (is_attribute_p ("transaction_safe", tx_qual))
- attrs = chainon (attrs, att);
+ attrs = attr_chainon (attrs, att);
else
- returned_attrs = chainon (returned_attrs, att);
+ returned_attrs = attr_chainon (returned_attrs, att);
  }
if (attrs)
  /* [dcl.fct]/2:
@@ -13438,7 +13439,7 @@ grokdeclarator (const cp_declarator *declarator,
if (returned_attrs)
  {
if (attrlist)
-   *attrlist = chainon (returned_attrs, *attrlist);
+   *attrlist = attr_chainon (returned_attrs, *attrlist);
else
attrlist = _attrs;
  }
@@ -13451,7 +13452,7 @@ grokdeclarator (const cp_declarator *declarator,
/* [dcl.meaning]/1: The optional attribute-specifier-seq following
 a declarator-id appertains to the entity that is declared.  */
if (declarator->std_attributes != error_mark_node)
-   *attrlist = chainon (*attrlist, declarator->std_attributes);
+   *attrlist = attr_chainon (*attrlist, declarator->std_attributes);
else
/* We should have already diagnosed the issue (c++/78344).  */
gcc_assert (seen_error ());
diff --git 

Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-25 Thread Marek Polacek via Gcc-patches
On Tue, May 24, 2022 at 08:22:22AM -0400, Jason Merrill wrote:
> On 4/29/22 10:12, Marek Polacek wrote:
> > This patch fixes crashes with invalid attributes.  Arguably it could
> > make sense to assert seen_error() too.
> 
> So in this testcase we have TREE_CHAIN of a TREE_LIST pointing to
> error_mark_node?  Can we avoid that?

Yes and yes.  Sorry, my previous fix was lousy.  This one is better:

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

-- >8 --
When chaining attributes, attr_chainon should be used rather than plain
chainon, so that we don't end up with a TREE_LIST where one of the elements
is error_mark_node, which causes problems.  parser.cc has already been
fixed to use attr_chainon, but decl.cc has not.  Until now.

PR c++/96637

gcc/cp/ChangeLog:

* cp-tree.h (attr_chainon): Declare.
* decl.cc (start_decl): Use attr_chainon.
(grokdeclarator): Likewise.
* parser.cc (cp_parser_statement): No longer static.

gcc/testsuite/ChangeLog:

* g++.dg/parse/error64.C: New test.
---
 gcc/cp/cp-tree.h |  1 +
 gcc/cp/decl.cc   | 19 ++-
 gcc/cp/parser.cc |  2 +-
 gcc/testsuite/g++.dg/parse/error64.C |  4 
 4 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/parse/error64.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index ba986e892b6..d77fd1eb8a9 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7235,6 +7235,7 @@ extern void inject_this_parameter (tree, cp_cv_quals);
 extern location_t defparse_location (tree);
 extern void maybe_show_extern_c_location (void);
 extern bool literal_integer_zerop (const_tree);
+extern tree attr_chainon (tree, tree);
 
 /* in pt.cc */
 extern tree canonical_type_parameter   (tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 381259cb9cf..b1ea838ce8b 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -5557,7 +5557,7 @@ start_decl (const cp_declarator *declarator,
   *pushed_scope_p = NULL_TREE;
 
   if (prefix_attributes != error_mark_node)
-attributes = chainon (attributes, prefix_attributes);
+attributes = attr_chainon (attributes, prefix_attributes);
 
   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
 );
@@ -12728,9 +12728,10 @@ grokdeclarator (const cp_declarator *declarator,
   as a whole.  */
late_attrs = splice_template_attributes (, type);
  returned_attrs = decl_attributes (,
-   chainon (returned_attrs, attrs),
+   attr_chainon (returned_attrs,
+ attrs),
attr_flags);
- returned_attrs = chainon (late_attrs, returned_attrs);
+ returned_attrs = attr_chainon (late_attrs, returned_attrs);
}
 
   inner_declarator = declarator->declarator;
@@ -12781,8 +12782,8 @@ grokdeclarator (const cp_declarator *declarator,
 
   The optional attribute-specifier-seq appertains to the
   array.  */
-   returned_attrs = chainon (returned_attrs,
- declarator->std_attributes);
+   returned_attrs = attr_chainon (returned_attrs,
+  declarator->std_attributes);
  break;
 
case cdk_function:
@@ -13122,9 +13123,9 @@ grokdeclarator (const cp_declarator *declarator,
/* transaction_safe applies to the type, but
   transaction_safe_dynamic applies to the function.  */
if (is_attribute_p ("transaction_safe", tx_qual))
- attrs = chainon (attrs, att);
+ attrs = attr_chainon (attrs, att);
else
- returned_attrs = chainon (returned_attrs, att);
+ returned_attrs = attr_chainon (returned_attrs, att);
  }
if (attrs)
  /* [dcl.fct]/2:
@@ -13438,7 +13439,7 @@ grokdeclarator (const cp_declarator *declarator,
   if (returned_attrs)
 {
   if (attrlist)
-   *attrlist = chainon (returned_attrs, *attrlist);
+   *attrlist = attr_chainon (returned_attrs, *attrlist);
   else
attrlist = _attrs;
 }
@@ -13451,7 +13452,7 @@ grokdeclarator (const cp_declarator *declarator,
   /* [dcl.meaning]/1: The optional attribute-specifier-seq following
 a declarator-id appertains to the entity that is declared.  */
   if (declarator->std_attributes != error_mark_node)
-   *attrlist = chainon (*attrlist, declarator->std_attributes);
+   *attrlist = attr_chainon (*attrlist, declarator->std_attributes);
   else
/* We should have already diagnosed the issue (c++/78344).  */
gcc_assert (seen_error ());
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 868b8610d60..62aaccda23d 

Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-24 Thread Marek Polacek via Gcc-patches
On Tue, May 24, 2022 at 05:29:56PM +0530, Prathamesh Kulkarni wrote:
> On Fri, 29 Apr 2022 at 19:44, Marek Polacek via Gcc-patches
>  wrote:
> >
> > This patch fixes crashes with invalid attributes.  Arguably it could
> > make sense to assert seen_error() too.
> >
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk = GCC 13?
> >
> > PR c++/96637
> >
> > gcc/ChangeLog:
> >
> > * attribs.cc (decl_attributes): Check error_mark_node.
> >
> > gcc/cp/ChangeLog:
> >
> > * decl2.cc (cp_check_const_attributes): Check error_mark_node.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * g++.dg/parse/error64.C: New test.
> > ---
> >  gcc/attribs.cc   | 3 +++
> >  gcc/cp/decl2.cc  | 2 ++
> >  gcc/testsuite/g++.dg/parse/error64.C | 4 
> >  3 files changed, 9 insertions(+)
> >  create mode 100644 gcc/testsuite/g++.dg/parse/error64.C
> >
> > diff --git a/gcc/attribs.cc b/gcc/attribs.cc
> > index b219f878042..ff157dcf81c 100644
> > --- a/gcc/attribs.cc
> > +++ b/gcc/attribs.cc
> > @@ -700,6 +700,9 @@ decl_attributes (tree *node, tree attributes, int flags,
> >   in the same order as in the source.  */
> >for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
> >  {
> > +  if (attr == error_mark_node)
> > +   continue;
> Not a comment on the patch specifically, but just wondering if it'd be
> better to use error_operand_p,
> than testing against error_mark_node explicitly ?

Not here, I don't think; it tests more than is needed here.

Marek



Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-24 Thread Jason Merrill via Gcc-patches

On 4/29/22 10:12, Marek Polacek wrote:

This patch fixes crashes with invalid attributes.  Arguably it could
make sense to assert seen_error() too.


So in this testcase we have TREE_CHAIN of a TREE_LIST pointing to 
error_mark_node?  Can we avoid that?



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

PR c++/96637

gcc/ChangeLog:

* attribs.cc (decl_attributes): Check error_mark_node.

gcc/cp/ChangeLog:

* decl2.cc (cp_check_const_attributes): Check error_mark_node.

gcc/testsuite/ChangeLog:

* g++.dg/parse/error64.C: New test.
---
  gcc/attribs.cc   | 3 +++
  gcc/cp/decl2.cc  | 2 ++
  gcc/testsuite/g++.dg/parse/error64.C | 4 
  3 files changed, 9 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/parse/error64.C

diff --git a/gcc/attribs.cc b/gcc/attribs.cc
index b219f878042..ff157dcf81c 100644
--- a/gcc/attribs.cc
+++ b/gcc/attribs.cc
@@ -700,6 +700,9 @@ decl_attributes (tree *node, tree attributes, int flags,
   in the same order as in the source.  */
for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
  {
+  if (attr == error_mark_node)
+   continue;
+
tree ns = get_attribute_namespace (attr);
tree name = get_attribute_name (attr);
tree args = TREE_VALUE (attr);
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index d2b29208ed5..c3ff1962a75 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -1537,6 +1537,8 @@ cp_check_const_attributes (tree attributes)
/* As we implement alignas using gnu::aligned attribute and
 alignas argument is a constant expression, force manifestly
 constant evaluation of aligned attribute argument.  */
+  if (attr == error_mark_node)
+   continue;
bool manifestly_const_eval
= is_attribute_p ("aligned", get_attribute_name (attr));
for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
diff --git a/gcc/testsuite/g++.dg/parse/error64.C 
b/gcc/testsuite/g++.dg/parse/error64.C
new file mode 100644
index 000..87848a58c27
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/error64.C
@@ -0,0 +1,4 @@
+// PR c++/96637
+// { dg-do compile }
+
+void foo(int[] alignas[1] alignas(1)){} // { dg-error "" }

base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2




Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-24 Thread Prathamesh Kulkarni via Gcc-patches
On Fri, 29 Apr 2022 at 19:44, Marek Polacek via Gcc-patches
 wrote:
>
> This patch fixes crashes with invalid attributes.  Arguably it could
> make sense to assert seen_error() too.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk = GCC 13?
>
> PR c++/96637
>
> gcc/ChangeLog:
>
> * attribs.cc (decl_attributes): Check error_mark_node.
>
> gcc/cp/ChangeLog:
>
> * decl2.cc (cp_check_const_attributes): Check error_mark_node.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/parse/error64.C: New test.
> ---
>  gcc/attribs.cc   | 3 +++
>  gcc/cp/decl2.cc  | 2 ++
>  gcc/testsuite/g++.dg/parse/error64.C | 4 
>  3 files changed, 9 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/parse/error64.C
>
> diff --git a/gcc/attribs.cc b/gcc/attribs.cc
> index b219f878042..ff157dcf81c 100644
> --- a/gcc/attribs.cc
> +++ b/gcc/attribs.cc
> @@ -700,6 +700,9 @@ decl_attributes (tree *node, tree attributes, int flags,
>   in the same order as in the source.  */
>for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
>  {
> +  if (attr == error_mark_node)
> +   continue;
Not a comment on the patch specifically, but just wondering if it'd be
better to use error_operand_p,
than testing against error_mark_node explicitly ?

Thanks,
Prathamesh
> +
>tree ns = get_attribute_namespace (attr);
>tree name = get_attribute_name (attr);
>tree args = TREE_VALUE (attr);
> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
> index d2b29208ed5..c3ff1962a75 100644
> --- a/gcc/cp/decl2.cc
> +++ b/gcc/cp/decl2.cc
> @@ -1537,6 +1537,8 @@ cp_check_const_attributes (tree attributes)
>/* As we implement alignas using gnu::aligned attribute and
>  alignas argument is a constant expression, force manifestly
>  constant evaluation of aligned attribute argument.  */
> +  if (attr == error_mark_node)
> +   continue;
>bool manifestly_const_eval
> = is_attribute_p ("aligned", get_attribute_name (attr));
>for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
> diff --git a/gcc/testsuite/g++.dg/parse/error64.C 
> b/gcc/testsuite/g++.dg/parse/error64.C
> new file mode 100644
> index 000..87848a58c27
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/parse/error64.C
> @@ -0,0 +1,4 @@
> +// PR c++/96637
> +// { dg-do compile }
> +
> +void foo(int[] alignas[1] alignas(1)){} // { dg-error "" }
>
> base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2
> --
> 2.35.1
>


Re: [PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-05-24 Thread Marek Polacek via Gcc-patches
Ping.

On Fri, Apr 29, 2022 at 10:12:33AM -0400, Marek Polacek via Gcc-patches wrote:
> This patch fixes crashes with invalid attributes.  Arguably it could
> make sense to assert seen_error() too.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk = GCC 13?
> 
>   PR c++/96637
> 
> gcc/ChangeLog:
> 
>   * attribs.cc (decl_attributes): Check error_mark_node.
> 
> gcc/cp/ChangeLog:
> 
>   * decl2.cc (cp_check_const_attributes): Check error_mark_node.
> 
> gcc/testsuite/ChangeLog:
> 
>   * g++.dg/parse/error64.C: New test.
> ---
>  gcc/attribs.cc   | 3 +++
>  gcc/cp/decl2.cc  | 2 ++
>  gcc/testsuite/g++.dg/parse/error64.C | 4 
>  3 files changed, 9 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/parse/error64.C
> 
> diff --git a/gcc/attribs.cc b/gcc/attribs.cc
> index b219f878042..ff157dcf81c 100644
> --- a/gcc/attribs.cc
> +++ b/gcc/attribs.cc
> @@ -700,6 +700,9 @@ decl_attributes (tree *node, tree attributes, int flags,
>   in the same order as in the source.  */
>for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
>  {
> +  if (attr == error_mark_node)
> + continue;
> +
>tree ns = get_attribute_namespace (attr);
>tree name = get_attribute_name (attr);
>tree args = TREE_VALUE (attr);
> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
> index d2b29208ed5..c3ff1962a75 100644
> --- a/gcc/cp/decl2.cc
> +++ b/gcc/cp/decl2.cc
> @@ -1537,6 +1537,8 @@ cp_check_const_attributes (tree attributes)
>/* As we implement alignas using gnu::aligned attribute and
>alignas argument is a constant expression, force manifestly
>constant evaluation of aligned attribute argument.  */
> +  if (attr == error_mark_node)
> + continue;
>bool manifestly_const_eval
>   = is_attribute_p ("aligned", get_attribute_name (attr));
>for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
> diff --git a/gcc/testsuite/g++.dg/parse/error64.C 
> b/gcc/testsuite/g++.dg/parse/error64.C
> new file mode 100644
> index 000..87848a58c27
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/parse/error64.C
> @@ -0,0 +1,4 @@
> +// PR c++/96637
> +// { dg-do compile }
> +
> +void foo(int[] alignas[1] alignas(1)){} // { dg-error "" }
> 
> base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2
> -- 
> 2.35.1
> 

Marek



[PATCH] c++: fix ICE on invalid attributes [PR96637]

2022-04-29 Thread Marek Polacek via Gcc-patches
This patch fixes crashes with invalid attributes.  Arguably it could
make sense to assert seen_error() too.

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

PR c++/96637

gcc/ChangeLog:

* attribs.cc (decl_attributes): Check error_mark_node.

gcc/cp/ChangeLog:

* decl2.cc (cp_check_const_attributes): Check error_mark_node.

gcc/testsuite/ChangeLog:

* g++.dg/parse/error64.C: New test.
---
 gcc/attribs.cc   | 3 +++
 gcc/cp/decl2.cc  | 2 ++
 gcc/testsuite/g++.dg/parse/error64.C | 4 
 3 files changed, 9 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/parse/error64.C

diff --git a/gcc/attribs.cc b/gcc/attribs.cc
index b219f878042..ff157dcf81c 100644
--- a/gcc/attribs.cc
+++ b/gcc/attribs.cc
@@ -700,6 +700,9 @@ decl_attributes (tree *node, tree attributes, int flags,
  in the same order as in the source.  */
   for (tree attr = attributes; attr; attr = TREE_CHAIN (attr))
 {
+  if (attr == error_mark_node)
+   continue;
+
   tree ns = get_attribute_namespace (attr);
   tree name = get_attribute_name (attr);
   tree args = TREE_VALUE (attr);
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index d2b29208ed5..c3ff1962a75 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -1537,6 +1537,8 @@ cp_check_const_attributes (tree attributes)
   /* As we implement alignas using gnu::aligned attribute and
 alignas argument is a constant expression, force manifestly
 constant evaluation of aligned attribute argument.  */
+  if (attr == error_mark_node)
+   continue;
   bool manifestly_const_eval
= is_attribute_p ("aligned", get_attribute_name (attr));
   for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST;
diff --git a/gcc/testsuite/g++.dg/parse/error64.C 
b/gcc/testsuite/g++.dg/parse/error64.C
new file mode 100644
index 000..87848a58c27
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/error64.C
@@ -0,0 +1,4 @@
+// PR c++/96637
+// { dg-do compile }
+
+void foo(int[] alignas[1] alignas(1)){} // { dg-error "" }

base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2
-- 
2.35.1