Skip to content

Commit 24d64ec

Browse files
committed
clang import: reject address-only function and enumerator declarations
1 parent e267a95 commit 24d64ec

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎lib/clangimport.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,10 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
10551055
return nullptr;
10561056
}
10571057
if (nodeType == EnumConstantDecl) {
1058-
Token *nameToken = addtoken(tokenList, getSpelling());
1058+
const std::string name = getSpelling();
1059+
if (name.empty())
1060+
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
1061+
Token *nameToken = addtoken(tokenList, name);
10591062
auto *scope = const_cast<Scope *>(nameToken->scope());
10601063
scope->enumeratorList.emplace_back(nameToken->scope());
10611064
Enumerator *e = &scope->enumeratorList.back();
@@ -1386,6 +1389,10 @@ Token * clangimport::AstNode::createTokensCall(TokenList &tokenList)
13861389

13871390
void clangimport::AstNode::createTokensFunctionDecl(TokenList &tokenList)
13881391
{
1392+
// an address-only node has no name: addtoken("") adds nothing and would return a null back()
1393+
const std::string name = getSpelling();
1394+
if (name.empty())
1395+
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
13891396
const bool prev = contains(mExtTokens, "prev");
13901397
const bool hasBody = !children.empty() && children.back()->nodeType == CompoundStmt;
13911398
const bool isStatic = contains(mExtTokens, "static");
@@ -1407,7 +1414,7 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList &tokenList)
14071414
if (mExtTokens.size() > 4 && mExtTokens[1] == "parent")
14081415
addFullScopeNameTokens(tokenList, mData->getScope(mExtTokens[2]));
14091416

1410-
Token *nameToken = addtoken(tokenList, getSpelling() + getTemplateParameters());
1417+
Token *nameToken = addtoken(tokenList, name + getTemplateParameters());
14111418
auto *nestedIn = const_cast<Scope *>(nameToken->scope());
14121419

14131420
if (prev) {

‎test/testclangimport.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,11 @@ class TestClangImport : public TestFixture {
14401440
ASSERT_THROW_INTERNAL_EQUALS(parse("`-EnumDecl "), AST, "invalid AST node: EnumDecl");
14411441
ASSERT_THROW_INTERNAL_EQUALS(parse("`-VarDecl 0x1"), AST, "invalid AST node: VarDecl");
14421442
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1 <a.cpp:1:1, col:20> col:5 f 'int ()' prev"), AST, "invalid AST node: FunctionDecl");
1443+
// address-only declarations: getSpelling() is empty, addtoken() adds nothing and back() is null
1444+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1"), AST, "invalid AST node: FunctionDecl");
1445+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-CXXConstructorDecl 0x1"), AST, "invalid AST node: CXXConstructorDecl");
1446+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-CXXMethodDecl 0x1"), AST, "invalid AST node: CXXMethodDecl");
1447+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-EnumConstantDecl 0x1"), AST, "invalid AST node: EnumConstantDecl");
14431448
}
14441449
};
14451450

0 commit comments

Comments
 (0)