Skip to content

Commit 1f159b8

Browse files
committed
fix: Catalyst crashes
* Default initialize pointers * Gracefully handle invalid `decl`s and show appropriate errors in output * Check for `getDefinition` returning `nullptr` * Fallback to enum's declaration if there's no definition
1 parent af2f334 commit 1f159b8

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/Meta/MetaEntities.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class Meta {
106106
std::string name;
107107
std::string jsName;
108108
std::string fileName;
109-
clang::Module* module;
110-
const clang::Decl* declaration;
109+
clang::Module* module = nullptr;
110+
const clang::Decl* declaration = nullptr;
111111

112112
// Availability
113113
Version introducedIn = UNKNOWN_VERSION;
@@ -185,8 +185,8 @@ class PropertyMeta : public Meta {
185185
this->type = MetaType::Property;
186186
}
187187

188-
MethodMeta* getter;
189-
MethodMeta* setter;
188+
MethodMeta* getter = nullptr;
189+
MethodMeta* setter = nullptr;
190190

191191
virtual void visit(MetaVisitor* visitor) override;
192192
};
@@ -279,7 +279,7 @@ class EnumConstantMeta : public Meta {
279279

280280
std::string value;
281281

282-
bool isScoped;
282+
bool isScoped = false;
283283

284284
virtual void visit(MetaVisitor* visitor) override;
285285
};
@@ -310,8 +310,8 @@ class VarMeta : public Meta {
310310
this->type = MetaType::Var;
311311
}
312312

313-
Type* signature;
314-
bool hasValue;
313+
Type* signature = nullptr;
314+
bool hasValue = false;
315315
std::string value;
316316

317317
virtual void visit(MetaVisitor* visitor) override;

src/Meta/MetaFactory.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ void resetMetaAndAddToMap(std::unique_ptr<Meta>& metaPtrRef, MetaToDeclMap& meta
7878
metaPtrRef.reset(new T());
7979
metaToDecl[metaPtrRef.get()] = &decl;
8080
}
81+
82+
if (decl.isInvalidDecl()) {
83+
std::string declDump;
84+
llvm::raw_string_ostream os(declDump);
85+
decl.dump(os);
86+
throw MetaCreationException(metaPtrRef.get(), CreationException::constructMessage("Invalid decl.", os.str()), true);
87+
}
8188
}
8289

8390
Meta* MetaFactory::create(const clang::Decl& decl, bool resetCached /* = false*/)
@@ -347,7 +354,7 @@ void MetaFactory::createFromInterface(const clang::ObjCInterfaceDecl& interface,
347354

348355
// set base interface
349356
clang::ObjCInterfaceDecl* super = interface.getSuperClass();
350-
interfaceMeta.base = (super == nullptr) ? nullptr : &this->create(*super->getDefinition())->as<InterfaceMeta>();
357+
interfaceMeta.base = (super == nullptr || super->getDefinition() == nullptr) ? nullptr : &this->create(*super->getDefinition())->as<InterfaceMeta>();
351358
}
352359

353360
void MetaFactory::createFromProtocol(const clang::ObjCProtocolDecl& protocol, ProtocolMeta& protocolMeta)
@@ -589,7 +596,8 @@ void MetaFactory::populateBaseClassMetaFields(const clang::ObjCContainerDecl& de
589596
{
590597
for (clang::ObjCProtocolDecl* protocol : this->getProtocols(&decl)) {
591598
Meta* protocolMeta;
592-
if (this->tryCreate(*protocol->getDefinition(), &protocolMeta)) {
599+
600+
if (protocol->getDefinition() != nullptr && this->tryCreate(*protocol->getDefinition(), &protocolMeta)) {
593601
baseClass.protocols.push_back(&protocolMeta->as<ProtocolMeta>());
594602
}
595603
}

src/Meta/TypeFactory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ shared_ptr<Type> TypeFactory::createFromPointerType(const clang::PointerType* ty
356356
shared_ptr<Type> TypeFactory::createFromEnumType(const clang::EnumType* type)
357357
{
358358
Type* innerType = this->create(type->getDecl()->getIntegerType()).get();
359-
EnumMeta* enumMeta = &this->_metaFactory->create(*type->getDecl()->getDefinition())->as<EnumMeta>();
359+
auto& enumDecl = type->getDecl()->getDefinition() ? *type->getDecl()->getDefinition() : *type->getDecl();
360+
EnumMeta* enumMeta = &this->_metaFactory->create(enumDecl)->as<EnumMeta>();
360361
return make_shared<EnumType>(innerType, enumMeta);
361362
}
362363

0 commit comments

Comments
 (0)