Skip to content

Commit e4e5af9

Browse files
authored
Merge pull request #64701 from bnbarham/fix-stack-uaf
[Attr] Refactor retrieving original attributes
2 parents bf57882 + ac73d48 commit e4e5af9

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

include/swift/AST/Attr.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,44 +2582,53 @@ class DeclAttributes {
25822582
SourceLoc getStartLoc(bool forModifiers = false) const;
25832583
};
25842584

2585+
/// Predicate used to filter attributes to only the original attributes.
2586+
class OrigDeclAttrFilter {
2587+
ModuleDecl *mod;
2588+
2589+
public:
2590+
OrigDeclAttrFilter() : mod(nullptr) {}
2591+
2592+
OrigDeclAttrFilter(ModuleDecl *mod) : mod(mod) {}
2593+
2594+
Optional<const DeclAttribute *>
2595+
operator()(const DeclAttribute *Attr) const;
2596+
};
2597+
25852598
/// Attributes applied directly to the declaration.
25862599
///
25872600
/// We should really just have \c DeclAttributes and \c SemanticDeclAttributes,
25882601
/// but currently almost all callers expect the latter. Instead of changing all
2589-
/// callers of \c getAttrs, instead provide a way to retrieive the original
2602+
/// callers of \c getAttrs, instead provide a way to retrieve the original
25902603
/// attributes.
25912604
class OrigDeclAttributes {
2592-
SmallVector<DeclAttribute *> attributes;
2593-
using AttrListTy = SmallVectorImpl<DeclAttribute *>;
2594-
25952605
public:
2596-
OrigDeclAttributes() = default;
2606+
using OrigFilteredRange = OptionalTransformRange<iterator_range<DeclAttributes::const_iterator>, OrigDeclAttrFilter>;
25972607

2598-
OrigDeclAttributes(DeclAttributes semanticAttrs, ModuleDecl *mod);
2608+
private:
2609+
OrigFilteredRange origRange;
25992610

2611+
public:
2612+
OrigDeclAttributes() : origRange(make_range(DeclAttributes::const_iterator(nullptr), DeclAttributes::const_iterator(nullptr)), OrigDeclAttrFilter()) {}
26002613

2601-
using iterator = AttrListTy::iterator;
2602-
using const_iterator = AttrListTy::const_iterator;
2614+
OrigDeclAttributes(const DeclAttributes &allAttrs, ModuleDecl *mod) : origRange(make_range(allAttrs.begin(), allAttrs.end()), OrigDeclAttrFilter(mod)) {}
26032615

2604-
iterator begin() { return attributes.begin(); }
2605-
iterator end() { return attributes.end(); }
2606-
const_iterator begin() const { return attributes.begin(); }
2607-
const_iterator end() const { return attributes.end(); }
2616+
OrigFilteredRange::iterator begin() const { return origRange.begin(); }
2617+
OrigFilteredRange::iterator end() const { return origRange.end(); }
26082618

26092619
template <typename AttrType, bool AllowInvalid>
26102620
using AttributeKindRange =
2611-
OptionalTransformRange<iterator_range<const_iterator>,
2612-
ToAttributeKind<AttrType, AllowInvalid>, const_iterator>;
2621+
OptionalTransformRange<OrigFilteredRange, ToAttributeKind<AttrType, AllowInvalid>>;
26132622

26142623
template <typename AttrType, bool AllowInvalid = false>
26152624
AttributeKindRange<AttrType, AllowInvalid> getAttributes() const {
2616-
return AttributeKindRange<AttrType, AllowInvalid>(make_range(begin(), end()), ToAttributeKind<AttrType, AllowInvalid>());
2625+
return AttributeKindRange<AttrType, AllowInvalid>(origRange, ToAttributeKind<AttrType, AllowInvalid>());
26172626
}
26182627

26192628
/// Retrieve the first attribute of the given attribute class.
26202629
template <typename AttrType>
26212630
const AttrType *getAttribute(bool allowInvalid = false) const {
2622-
for (auto *attr : attributes) {
2631+
for (auto *attr : origRange) {
26232632
if (auto *specificAttr = dyn_cast<AttrType>(attr)) {
26242633
if (specificAttr->isValid() || allowInvalid)
26252634
return specificAttr;

lib/AST/Attr.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,11 @@ SourceLoc DeclAttributes::getStartLoc(bool forModifiers) const {
851851
return lastAttr ? lastAttr->getRangeWithAt().Start : SourceLoc();
852852
}
853853

854-
OrigDeclAttributes::OrigDeclAttributes(DeclAttributes allAttributes, ModuleDecl *mod) {
855-
for (auto *attr : allAttributes) {
856-
if (!mod->isInGeneratedBuffer(attr->AtLoc)) {
857-
attributes.emplace_back(attr);
858-
}
859-
}
854+
Optional<const DeclAttribute *>
855+
OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
856+
if (!mod || mod->isInGeneratedBuffer(Attr->AtLoc))
857+
return None;
858+
return Attr;
860859
}
861860

862861
static void printAvailableAttr(const AvailableAttr *Attr, ASTPrinter &Printer,

0 commit comments

Comments
 (0)