Skip to content

Commit fc57f88

Browse files
authored
[Clang] Fix Undefined Behavior introduced by llvm#91199 (llvm#91718)
We stack allocated an OpaqueExpr that would be used after it was destroyed. e.g https://lab.llvm.org/buildbot/#/builders/57/builds/34909
1 parent 6419496 commit fc57f88

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

clang/lib/Sema/SemaExprCXX.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -5627,10 +5627,9 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
56275627
static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceInfo *Lhs,
56285628
const TypeSourceInfo *Rhs, SourceLocation KeyLoc);
56295629

5630-
static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
5631-
const TypeSourceInfo *Lhs,
5632-
const TypeSourceInfo *Rhs,
5633-
SourceLocation KeyLoc) {
5630+
static ExprResult CheckConvertibilityForTypeTraits(
5631+
Sema &Self, const TypeSourceInfo *Lhs, const TypeSourceInfo *Rhs,
5632+
SourceLocation KeyLoc, llvm::BumpPtrAllocator &OpaqueExprAllocator) {
56345633

56355634
QualType LhsT = Lhs->getType();
56365635
QualType RhsT = Rhs->getType();
@@ -5675,9 +5674,9 @@ static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
56755674

56765675
// Build a fake source and destination for initialization.
56775676
InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
5678-
OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
5679-
Expr::getValueKindForType(LhsT));
5680-
Expr *FromPtr = &From;
5677+
Expr *From = new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>())
5678+
OpaqueValueExpr(KeyLoc, LhsT.getNonLValueExprType(Self.Context),
5679+
Expr::getValueKindForType(LhsT));
56815680
InitializationKind Kind =
56825681
InitializationKind::CreateCopy(KeyLoc, SourceLocation());
56835682

@@ -5687,11 +5686,11 @@ static ExprResult CheckConvertibilityForTypeTraits(Sema &Self,
56875686
Self, Sema::ExpressionEvaluationContext::Unevaluated);
56885687
Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true);
56895688
Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl());
5690-
InitializationSequence Init(Self, To, Kind, FromPtr);
5689+
InitializationSequence Init(Self, To, Kind, From);
56915690
if (Init.Failed())
56925691
return ExprError();
56935692

5694-
ExprResult Result = Init.Perform(Self, To, Kind, FromPtr);
5693+
ExprResult Result = Init.Perform(Self, To, Kind, From);
56955694
if (Result.isInvalid() || SFINAE.hasErrorOccurred())
56965695
return ExprError();
56975696

@@ -5819,7 +5818,8 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind,
58195818
S.Context.getPointerType(T.getNonReferenceType()));
58205819
TypeSourceInfo *UPtr = S.Context.CreateTypeSourceInfo(
58215820
S.Context.getPointerType(U.getNonReferenceType()));
5822-
return !CheckConvertibilityForTypeTraits(S, UPtr, TPtr, RParenLoc)
5821+
return !CheckConvertibilityForTypeTraits(S, UPtr, TPtr, RParenLoc,
5822+
OpaqueExprAllocator)
58235823
.isInvalid();
58245824
}
58255825

@@ -6028,9 +6028,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
60286028
case BTT_IsNothrowConvertible: {
60296029
if (RhsT->isVoidType())
60306030
return LhsT->isVoidType();
6031-
6032-
ExprResult Result =
6033-
CheckConvertibilityForTypeTraits(Self, Lhs, Rhs, KeyLoc);
6031+
llvm::BumpPtrAllocator OpaqueExprAllocator;
6032+
ExprResult Result = CheckConvertibilityForTypeTraits(Self, Lhs, Rhs, KeyLoc,
6033+
OpaqueExprAllocator);
60346034
if (Result.isInvalid())
60356035
return false;
60366036

0 commit comments

Comments
 (0)