Skip to content

Commit b81083f

Browse files
authored
fixed #13687 - avoid null pointer dereference with incomplete remark comment (#7370)
1 parent 66880df commit b81083f

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

lib/preprocessor.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
168168
}
169169

170170
static std::string getRelativeFilename(const simplecpp::Token* tok, const Settings &settings) {
171+
if (!tok)
172+
return "";
171173
std::string relativeFilename(tok->location.file());
172174
if (settings.relativePaths) {
173175
for (const std::string & basePath : settings.basePaths) {
@@ -1079,6 +1081,8 @@ void Preprocessor::addRemarkComments(const simplecpp::TokenList &tokens, std::ve
10791081
remarkedToken = prev;
10801082
break;
10811083
}
1084+
if (!remarkedToken)
1085+
continue;
10821086

10831087
// Relative filename
10841088
const std::string relativeFilename = getRelativeFilename(remarkedToken, mSettings);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//REMARK /

test/testpreprocessor.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class TestPreprocessor : public TestFixture {
195195
TEST_CASE(remarkComment1);
196196
TEST_CASE(remarkComment2);
197197
TEST_CASE(remarkComment3);
198+
TEST_CASE(remarkComment4);
198199

199200
// Using -D to predefine symbols
200201
TEST_CASE(predefine1);
@@ -1940,6 +1941,12 @@ class TestPreprocessor : public TestFixture {
19401941
ASSERT_EQUALS("assignment with 1 ", remarkComments[0].str);
19411942
}
19421943

1944+
void remarkComment4() {
1945+
const char code[] = "//REMARK /";
1946+
const auto remarkComments = PreprocessorHelper::getRemarkComments(code, *this);
1947+
ASSERT_EQUALS(0, remarkComments.size());
1948+
}
1949+
19431950
void predefine1() {
19441951
const std::string src("#if defined X || Y\n"
19451952
"Fred & Wilma\n"

0 commit comments

Comments
 (0)