From a63c9b25620c8a4da9fcf1e1e8535d3110819ec0 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Wed, 21 Apr 2021 09:25:00 -0400 Subject: [PATCH] Do not pass null attributes to BuildAttributedStmt during instantiation When transforming an attribute during template instantiation, if the transformation fails, it may result in a null attribute being returned. This null attribute should not be passed in as one of the attributes used to create an attributed statement. If all of the attributes fail to transform, we do not create an attributed statement at all. There are no attributes that return null currently, so there is no easy way to test this currently. However, this fixes a crash caused by 8344675908424ee532d4ae30e5043c5a5834e02c. --- clang/lib/Sema/TreeTransform.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index ee661f0..5a2013b 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -7325,7 +7325,8 @@ TreeTransform::TransformAttributedStmt(AttributedStmt *S, for (const auto *I : S->getAttrs()) { const Attr *R = getDerived().TransformAttr(I); AttrsChanged |= (I != R); - Attrs.push_back(R); + if (R) + Attrs.push_back(R); } StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt(), SDK); @@ -7335,6 +7336,11 @@ TreeTransform::TransformAttributedStmt(AttributedStmt *S, if (SubStmt.get() == S->getSubStmt() && !AttrsChanged) return S; + // If transforming the attributes failed for all of the attributes in the + // statement, don't make an AttributedStmt without attributes. + if (Attrs.empty()) + return SubStmt; + return getDerived().RebuildAttributedStmt(S->getAttrLoc(), Attrs, SubStmt.get()); } -- 2.7.4