Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,8 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
return nullptr;
}
if (nodeType == GotoStmt) {
if (mExtTokens.size() < 2)
return nullptr;
addtoken(tokenList, "goto");
addtoken(tokenList, unquote(mExtTokens[mExtTokens.size() - 2]));
addtoken(tokenList, ";");
Expand Down Expand Up @@ -1228,9 +1230,12 @@ Token *clangimport::AstNode::createTokens(TokenList &tokenList)
if (children.empty())
return nullptr;
const Token *defToken = addtoken(tokenList, "namespace");
const std::string &s = mExtTokens[mExtTokens.size() - 2];
const Token* nameToken = (startsWith(s, "col:") || startsWith(s, "line:")) ?
addtoken(tokenList, mExtTokens.back()) : nullptr;
const Token* nameToken = nullptr;
if (mExtTokens.size() >= 2) {
const std::string &s = mExtTokens[mExtTokens.size() - 2];
if (startsWith(s, "col:") || startsWith(s, "line:"))
nameToken = addtoken(tokenList, mExtTokens.back());
}
Scope *scope = createScope(tokenList, ScopeType::eNamespace, children, defToken);
if (nameToken)
scope->className = nameToken->str();
Expand Down Expand Up @@ -1507,10 +1512,12 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList &tokenList)
const bool isStruct = contains(mExtTokens, "struct");
Token * const classToken = addtoken(tokenList, isStruct ? "struct" : "class");
std::string className;
if (mExtTokens[mExtTokens.size() - 2] == (isStruct?"struct":"class"))
className = mExtTokens.back();
else
className = mExtTokens[mExtTokens.size() - 2];
if (mExtTokens.size() >= 2) {
if (mExtTokens[mExtTokens.size() - 2] == (isStruct?"struct":"class"))
className = mExtTokens.back();
else
className = mExtTokens[mExtTokens.size() - 2];
}
className += getTemplateParameters();
/*Token *nameToken =*/ addtoken(tokenList, className);
// base classes
Expand Down
13 changes: 13 additions & 0 deletions test/testclangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class TestClangImport : public TestFixture {

TEST_CASE(crash);
TEST_CASE(crash2);
TEST_CASE(crash3);
TEST_CASE(nullNodeInvalidLevel);
}

Expand Down Expand Up @@ -1429,6 +1430,18 @@ class TestClangImport : public TestFixture {
"`-<<<NULL>>>\n";
ASSERT_EQUALS("void foo ( ) ;", parse(clang));
}

void crash3() {
// GotoStmt, NamespaceDecl and CXXRecordDecl read mExtTokens[size() - 2]
// without checking the node has two ext tokens, so a crafted node with
// fewer tokens indexed out of bounds.
(void)parse("`-FunctionDecl 0x1 <a.cpp:1:1, col:34> col:6 foo 'void ()'\n"
" `-CompoundStmt 0x2 <col:12, col:34>\n"
" `-GotoStmt 0x3\n"); // don't crash
(void)parse("`-NamespaceDecl 0x1\n"
" `-CXXRecordDecl 0x2\n"); // don't crash
(void)parse("`-CXXRecordDecl 0x1"); // don't crash
}
};

REGISTER_TEST(TestClangImport)
Loading