Skip to content

Commit c0a3af7

Browse files
committed
clang import: reject VarDecl/FieldDecl without a name token
1 parent 3b3df10 commit c0a3af7

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎lib/clangimport.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,14 +1569,15 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList &tokenList)
15691569

15701570
Token * clangimport::AstNode::createTokensVarDecl(TokenList &tokenList)
15711571
{
1572-
if (mExtTokens.size() < 2)
1573-
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
15741572
const std::string addr = mExtTokens.front();
15751573
if (contains(mExtTokens, "static"))
15761574
addtoken(tokenList, "static");
15771575
int typeIndex = mExtTokens.size() - 1;
15781576
while (typeIndex > 1 && std::isalpha(mExtTokens[typeIndex][0]))
15791577
typeIndex--;
1578+
// there must be a name token between the address and the type
1579+
if (typeIndex < 2)
1580+
throw InternalError(nullptr, "invalid AST node: " + nodeType, InternalError::AST);
15801581
const std::string type = mExtTokens[typeIndex];
15811582
const std::string name = mExtTokens[typeIndex - 1];
15821583
const Token *startToken = tokenList.back();

‎test/testclangimport.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,9 @@ class TestClangImport : public TestFixture {
14521452
" `-IntegerLiteral \n"), AST, "invalid AST node: IntegerLiteral");
14531453
ASSERT_THROW_INTERNAL_EQUALS(parse("`-EnumDecl "), AST, "invalid AST node: EnumDecl");
14541454
ASSERT_THROW_INTERNAL_EQUALS(parse("`-VarDecl 0x1"), AST, "invalid AST node: VarDecl");
1455+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-VarDecl 0x1 'int'"), AST, "invalid AST node: VarDecl");
1456+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-VarDecl 0x1 <col:1>"), AST, "invalid AST node: VarDecl");
1457+
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FieldDecl 0x1 'int'"), AST, "invalid AST node: FieldDecl");
14551458
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1 <a.cpp:1:1, col:20> col:5 f 'int ()' prev"), AST, "invalid AST node: FunctionDecl");
14561459
// address-only declarations: getSpelling() is empty, addtoken() adds nothing and back() is null
14571460
ASSERT_THROW_INTERNAL_EQUALS(parse("`-FunctionDecl 0x1"), AST, "invalid AST node: FunctionDecl");

0 commit comments

Comments
 (0)