Skip to content

Commit fe25f04

Browse files
committed
[Clang] Fix Undefined Behavior introduced by llvm#91199
We stacked allocated an OpaqueExpr that woukd be used after it was destroyed.
1 parent 5d24217 commit fe25f04

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5630,7 +5630,8 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
56305630
static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
56315631
const TypeSourceInfo *Lhs,
56325632
const TypeSourceInfo *Rhs,
5633-
SourceLocation KeyLoc) {
5633+
SourceLocation KeyLoc,
5634+
llvm::BumpPtrAllocator & OpaqueExprAllocator) {
56345635

56355636
QualType LhsT = Lhs->getType();
56365637
QualType RhsT = Rhs->getType();
@@ -5675,9 +5676,9 @@ static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
56755676

56765677
// Build a fake source and destination for initialization.
56775678
InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
5678-
OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
5679+
Expr* From = new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>())
5680+
OpaqueValueExpr(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
56795681
Expr::getValueKindForType(LhsT));
5680-
Expr *FromPtr = &From;
56815682
InitializationKind Kind =
56825683
InitializationKind::CreateCopy(KeyLoc, SourceLocation());
56835684

@@ -5687,11 +5688,11 @@ static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
56875688
Self, Sema::ExpressionEvaluationContext::Unevaluated);
56885689
Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
56895690
Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
5690-
InitializationSequence Init(Self, To, Kind, FromPtr);
5691+
InitializationSequence Init(Self, To, Kind, From);
56915692
if (Init.Failed())
56925693
return ExprError();
56935694

5694-
ExprResult Result = Init.Perform(Self, To, Kind, FromPtr);
5695+
ExprResult Result = Init.Perform(Self, To, Kind, From);
56955696
if (Result.isInvalid() || SFINAE.hasErrorOccurred())
56965697
return ExprError();
56975698

@@ -5819,7 +5820,7 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind,
58195820
S.Context.getPointerType(T.getNonReferenceType()));
58205821
TypeSourceInfo *UPtr = S.Context.CreateTypeSourceInfo(
58215822
S.Context.getPointerType(U.getNonReferenceType()));
5822-
return !CheckConvertibilityForTypeTraits(S, UPtr, TPtr, RParenLoc)
5823+
return !CheckConvertibilityForTypeTraits(S, UPtr, TPtr, RParenLoc, OpaqueExprAllocator)
58235824
.isInvalid();
58245825
}
58255826

@@ -6028,9 +6029,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
60286029
case BTT_IsNothrowConvertible: {
60296030
if (RhsT->isVoidType())
60306031
return LhsT->isVoidType();
6031-
6032+
llvm::BumpPtrAllocator OpaqueExprAllocator;
60326033
ExprResult Result =
6033-
CheckConvertibilityForTypeTraits(Self, Lhs, Rhs, KeyLoc);
6034+
CheckConvertibilityForTypeTraits(Self, Lhs, Rhs, KeyLoc, OpaqueExprAllocator);
60346035
if (Result.isInvalid())
60356036
return false;
60366037

0 commit comments

Comments
 (0)