Skip to content

Commit 8b97f04

Browse files
committed
Try to address some Coverity issues. Add TODO testcase for cppcheck-opensource#5783. Introduce TODO_ASSERT macro.
1 parent f1f4661 commit 8b97f04

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,21 +595,21 @@ static void PrintCallstack(FILE* f, PEXCEPTION_POINTERS ex)
595595
static void writeMemoryErrorDetails(FILE* f, PEXCEPTION_POINTERS ex, const char* description)
596596
{
597597
fputs(description, f);
598-
fprintf(f, " (instruction: 0x%p) ", ex->ExceptionRecord->ExceptionAddress);
598+
fprintf(f, " (instruction: 0x%X) ", ex->ExceptionRecord->ExceptionAddress);
599599
// Using %p for ULONG_PTR later on, so it must have size identical to size of pointer
600600
// This is not the universally portable solution but good enough for Win32/64
601601
C_ASSERT(sizeof(void*) == sizeof(ex->ExceptionRecord->ExceptionInformation[1]));
602602
switch (ex->ExceptionRecord->ExceptionInformation[0]) {
603603
case 0:
604-
fprintf(f, "reading from 0x%p",
604+
fprintf(f, "reading from 0x%X",
605605
ex->ExceptionRecord->ExceptionInformation[1]);
606606
break;
607607
case 1:
608-
fprintf(f, "writing to 0x%p",
608+
fprintf(f, "writing to 0x%X",
609609
ex->ExceptionRecord->ExceptionInformation[1]);
610610
break;
611611
case 8:
612-
fprintf(f, "data execution prevention at 0x%p",
612+
fprintf(f, "data execution prevention at 0x%X",
613613
ex->ExceptionRecord->ExceptionInformation[1]);
614614
break;
615615
default:

lib/tokenize.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ bool Tokenizer::tokenize(std::istream &code,
17301730
_configuration = configuration;
17311731

17321732
if (!list.createTokens(code, Path::getRelativePath(Path::simplifyPath(FileName), _settings->_basePaths))) {
1733-
cppcheckError(0);
1733+
cppcheckError(nullptr);
17341734
return false;
17351735
}
17361736

@@ -1768,7 +1768,7 @@ bool Tokenizer::tokenizeCondition(const std::string &code)
17681768
{
17691769
std::istringstream istr(code);
17701770
if (!list.createTokens(istr)) {
1771-
cppcheckError(0);
1771+
cppcheckError(nullptr);
17721772
return false;
17731773
}
17741774
}
@@ -8773,9 +8773,10 @@ void Tokenizer::simplifyComma()
87738773
break;
87748774
}
87758775
}
8776-
8776+
if (!startFrom)
8777+
// to be very sure...
8778+
return;
87778779
std::size_t commaCounter = 0;
8778-
87798780
for (Token *tok2 = startFrom->next(); tok2; tok2 = tok2->next()) {
87808781
if (tok2->str() == ";") {
87818782
endAt = tok2;

test/testsuite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extern std::ostringstream warnings;
9090
#define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL)
9191
#define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
9292
#define ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) { } catch (...) { assertThrowFail(__FILE__, __LINE__); }
93+
#define TODO_ASSERT( CONDITION ) { bool condition=CONDITION; todoAssertEquals(__FILE__, __LINE__, true, false, condition); }
9394
#define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
9495
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; }
9596

test/testtokenize.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ class TestTokenizer : public TestFixture {
474474

475475
TEST_CASE(sizeofAddParentheses);
476476
TEST_CASE(incompleteTernary); // #6659
477+
TEST_CASE(noreturn); // #5783
477478
}
478479

479480
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Unspecified, const char* filename = "test.cpp", bool cpp11 = true) {
@@ -8772,6 +8773,31 @@ class TestTokenizer : public TestFixture {
87728773

87738774
tokenizeAndStringify(code, true);
87748775
}
8776+
8777+
// see #5783
8778+
void noreturn() {
8779+
const char code[] = "void myassert() {\n"
8780+
" exit(1);\n"
8781+
"}\n"
8782+
"void f(char *buf) {\n"
8783+
" if(i==0) {\n"
8784+
" free(buf);\n"
8785+
" myassert();\n"
8786+
" }\n"
8787+
" free(buf);\n"
8788+
"}\n";
8789+
Settings settings;
8790+
8791+
// tokenize..
8792+
Tokenizer tokenizer(&settings, this);
8793+
std::istringstream istr(code);
8794+
tokenizer.tokenize(istr, "test.cpp");
8795+
8796+
const Token * func = Token::findsimplematch(tokenizer.tokens(), "myassert");
8797+
8798+
TODO_ASSERT(func && func->isAttributeNoreturn());
8799+
8800+
}
87758801
};
87768802

87778803
REGISTER_TEST(TestTokenizer)

0 commit comments

Comments
 (0)