Skip to content

Commit ca94527

Browse files
authored
TokenList: avoid Path::identify() being called with empty filename from determineCppC() (#6326)
1 parent da380ea commit ca94527

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ test/testtoken.o: test/testtoken.cpp lib/addoninfo.h lib/check.h lib/color.h lib
862862
test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
863863
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenize.cpp
864864

865-
test/testtokenlist.o: test/testtokenlist.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
865+
test/testtokenlist.o: test/testtokenlist.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
866866
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenlist.cpp
867867

868868
test/testtokenrange.o: test/testtokenrange.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/tokenrange.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h

lib/tokenlist.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void TokenList::determineCppC()
9595
{
9696
// only try to determine if it wasn't enforced
9797
if (mLang == Standards::Language::None) {
98+
ASSERT_LANG(!getSourceFilePath().empty());
9899
mLang = Path::identify(getSourceFilePath());
99100
// TODO: cannot enable assert as this might occur for unknown extensions
100101
//ASSERT_LANG(mLang != Standards::Language::None);
@@ -372,11 +373,11 @@ bool TokenList::createTokensInternal(std::istream &code, const std::string& file
372373
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
373374
void TokenList::createTokens(simplecpp::TokenList&& tokenList)
374375
{
375-
if (tokenList.cfront()) {
376+
// tokenList.cfront() might be NULL if the file contained nothing to tokenize so we need to check the files instead
377+
if (!tokenList.getFiles().empty()) {
376378
// this is a copy
377-
// TODO: the same as TokenList.files - move that instead
378379
// TODO: this points to mFiles when called from createTokens(std::istream &, const std::string&)
379-
mOrigFiles = mFiles = tokenList.cfront()->location.files;
380+
mOrigFiles = mFiles = tokenList.getFiles();
380381
}
381382
else
382383
mFiles.clear();

test/testtokenlist.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
#include "fixture.h"
2121
#include "helpers.h"
2222
#include "platform.h"
23+
#include "preprocessor.h"
2324
#include "standards.h"
2425
#include "token.h"
2526
#include "tokenlist.h"
2627

2728
#include <sstream>
2829
#include <string>
2930

31+
#include <simplecpp.h>
32+
3033
class TestTokenList : public TestFixture {
3134
public:
3235
TestTokenList() : TestFixture("TestTokenList") {}
@@ -39,6 +42,7 @@ class TestTokenList : public TestFixture {
3942
TEST_CASE(testaddtoken2);
4043
TEST_CASE(inc);
4144
TEST_CASE(isKeyword);
45+
TEST_CASE(notokens);
4246
}
4347

4448
// inspired by #5895
@@ -144,6 +148,19 @@ class TestTokenList : public TestFixture {
144148
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
145149
}
146150
}
151+
152+
void notokens() {
153+
// analyzing /usr/include/poll.h caused Path::identify() to be called with an empty filename from
154+
// TokenList::determineCppC() because there are no tokens
155+
const char code[] = "#include <sys/poll.h>";
156+
std::istringstream istr(code);
157+
std::vector<std::string> files;
158+
simplecpp::TokenList tokens1(istr, files, "poll.h", nullptr);
159+
Preprocessor preprocessor(settingsDefault, *this);
160+
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, "", files, true);
161+
TokenList tokenlist(&settingsDefault);
162+
tokenlist.createTokens(std::move(tokensP)); // do not assert
163+
}
147164
};
148165

149166
REGISTER_TEST(TestTokenList)

0 commit comments

Comments
 (0)