Skip to content

Commit 7c316fb

Browse files
authored
Fixed #12071 (suppressing critical error, no indication to user that analysis of file fails) (cppcheck-opensource#5771)
1 parent 086ceea commit 7c316fb

20 files changed

Lines changed: 109 additions & 24 deletions

.github/workflows/CI-unixish.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ jobs:
423423
run: |
424424
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety addons/test/threadsafety
425425
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety --std=c++03 addons/test/threadsafety
426-
./cppcheck --error-exitcode=1 --inline-suppr --addon=misra addons/test/misra/crash*.c
426+
./cppcheck --error-exitcode=1 --dump addons/test/misra/crash*.c
427+
python3 addons/misra.py --cli addons/test/misra/crash*.c.dump > /dev/null
427428
./cppcheck --error-exitcode=1 --inline-suppr --addon=misra --enable=information addons/test/misra/config*.c
428429
429430
./cppcheck --addon=misra --enable=style --inline-suppr --enable=information --error-exitcode=1 addons/test/misra/misra-ctu-*-test.c

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if(LIBXML2_XMLLINT_EXECUTABLE)
4747
add_custom_target(errorlist-xml $<TARGET_FILE:cppcheck> --errorlist > ${CMAKE_BINARY_DIR}/errorlist.xml
4848
DEPENDS cppcheck)
4949

50-
add_custom_target(example-xml $<TARGET_FILE:cppcheck> --xml --enable=all --inconclusive --max-configs=1 ${CMAKE_SOURCE_DIR}/samples 2> ${CMAKE_BINARY_DIR}/example.xml
50+
add_custom_target(example-xml $<TARGET_FILE:cppcheck> --xml --enable=all --inconclusive --max-configs=1 ${CMAKE_SOURCE_DIR}/samples -i${CMAKE_SOURCE_DIR}/samples/syntaxError/bad.c 2> ${CMAKE_BINARY_DIR}/example.xml
5151
DEPENDS cppcheck)
5252

5353
add_custom_target(createXMLExamples DEPENDS errorlist-xml example-xml)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ validatePlatforms: ${PlatformFilesCHECKED}
439439
/tmp/errorlist.xml: cppcheck
440440
./cppcheck --errorlist >$@
441441
/tmp/example.xml: cppcheck
442-
./cppcheck --xml --enable=all --inconclusive --max-configs=1 samples 2>/tmp/example.xml
442+
./cppcheck --xml --enable=all --inconclusive --max-configs=1 samples -isamples/syntaxError/bad.c 2>/tmp/example.xml
443443
createXMLExamples:/tmp/errorlist.xml /tmp/example.xml
444444
.PHONY: validateXML
445445
validateXML: createXMLExamples

cli/cmdlineparser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
587587
if (!parseNumberArg(argv[i], 17, mSettings.exitCode))
588588
return Result::Fail;
589589
}
590+
// --unsafe-exitcode => for tests, critical errors will not force non-zero exitcode
591+
else if (std::strcmp(argv[i], "--unsafe-exitcode") == 0) {
592+
mSettings.unsafeExitCode = true;
593+
}
590594

591595
// Exception handling inside cppcheck client
592596
else if (std::strcmp(argv[i], "--exception-handling") == 0) {

cli/cppcheckexecutor.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class CppCheckExecutor::StdLogger : public ErrorLogger
118118
*/
119119
void writeCheckersReport() const;
120120

121+
bool hasCriticalErrors() const {
122+
return !mCriticalErrors.empty();
123+
}
124+
121125
private:
122126
/**
123127
* Information about progress is directed here. This should be
@@ -287,9 +291,12 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck) const
287291

288292
mStdLogger->writeCheckersReport();
289293

294+
if (!settings.unsafeExitCode && mStdLogger->hasCriticalErrors())
295+
return settings.exitCode > 0 ? settings.exitCode : EXIT_FAILURE;
296+
290297
if (returnValue)
291298
return settings.exitCode;
292-
return 0;
299+
return EXIT_SUCCESS;
293300
}
294301

295302
void CppCheckExecutor::StdLogger::writeCheckersReport() const
@@ -398,6 +405,10 @@ void CppCheckExecutor::StdLogger::reportErr(const ErrorMessage &msg)
398405
if (!mCriticalErrors.empty())
399406
mCriticalErrors += ", ";
400407
mCriticalErrors += msg.id;
408+
if (msg.severity == Severity::none) {
409+
mCriticalErrors += " (suppressed)";
410+
return;
411+
}
401412
}
402413

403414
if (mSettings.xml)

gui/resultstree.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,11 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
666666
menu.addAction(hideallid);
667667

668668
QAction *suppress = new QAction(tr("Suppress selected id(s)"), &menu);
669+
{
670+
QVariantMap data = mContextItem->data().toMap();
671+
const QString messageId = data[ERRORID].toString();
672+
suppress->setEnabled(!ErrorLogger::isCriticalErrorId(messageId.toStdString()));
673+
}
669674
menu.addAction(suppress);
670675
connect(suppress, &QAction::triggered, this, &ResultsTree::suppressSelectedIds);
671676

gui/resultsview.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ void ResultsView::error(const ErrorItem &item)
168168

169169
handleCriticalError(item);
170170

171+
if (item.severity == Severity::none)
172+
return;
173+
171174
if (mUI->mTree->addErrorItem(item)) {
172175
emit gotResults();
173176
mStatistics->addItem(item.tool(), ShowTypes::SeverityToShowType(item.severity));
@@ -562,10 +565,14 @@ void ResultsView::handleCriticalError(const ErrorItem &item)
562565
if (!mCriticalErrors.isEmpty())
563566
mCriticalErrors += ",";
564567
mCriticalErrors += item.errorId;
568+
if (item.severity == Severity::none)
569+
mCriticalErrors += " (suppressed)";
565570
}
566571
QString msg = tr("There was a critical error with id '%1'").arg(item.errorId);
567572
if (!item.file0.isEmpty())
568573
msg += ", " + tr("when checking %1").arg(item.file0);
574+
else
575+
msg += ", " + tr("when checking a file");
569576
msg += ". " + tr("Analysis was aborted.");
570577
mUI->mLabelCriticalErrors->setText(msg);
571578
mUI->mLabelCriticalErrors->setVisible(true);

htmlreport/check.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ validate_html "$INDEX_HTML"
4040
validate_html "$STATS_HTML"
4141

4242

43-
../cppcheck ../samples --enable=all --inconclusive --xml-version=2 2> "$GUI_TEST_XML"
43+
../cppcheck ../samples -i../samples/syntaxError/bad.c --enable=all --inconclusive --xml-version=2 2> "$GUI_TEST_XML"
4444
xmllint --noout "$GUI_TEST_XML"
4545
$PYTHON cppcheck-htmlreport --file "$GUI_TEST_XML" --title "xml2 + inconclusive test" --report-dir "$REPORT_DIR"
4646
echo ""
@@ -49,7 +49,7 @@ validate_html "$INDEX_HTML"
4949
validate_html "$STATS_HTML"
5050

5151

52-
../cppcheck ../samples --enable=all --inconclusive --verbose --xml-version=2 2> "$GUI_TEST_XML"
52+
../cppcheck ../samples -i../samples/syntaxError/bad.c --enable=all --inconclusive --verbose --xml-version=2 2> "$GUI_TEST_XML"
5353
xmllint --noout "$GUI_TEST_XML"
5454
$PYTHON cppcheck-htmlreport --file "$GUI_TEST_XML" --title "xml2 + inconclusive + verbose test" --report-dir "$REPORT_DIR"
5555
echo -e "\n"

lib/cppcheck.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,19 @@ void CppCheck::reportErr(const ErrorMessage &msg)
15991599
const auto errorMessage = Suppressions::ErrorMessage::fromErrorMessage(msg, macroNames);
16001600

16011601
if (mSettings.nomsg.isSuppressed(errorMessage, mUseGlobalSuppressions)) {
1602+
if (ErrorLogger::isCriticalErrorId(msg.id)) {
1603+
mExitCode = 1;
1604+
1605+
if (mSettings.nomsg.isSuppressedExplicitly(errorMessage, mUseGlobalSuppressions)) {
1606+
ErrorMessage temp;
1607+
temp.severity = Severity::none;
1608+
temp.id = msg.id;
1609+
mErrorLogger.reportErr(temp);
1610+
} else {
1611+
// Report critical error that is not explicitly suppressed
1612+
mErrorLogger.reportErr(msg);
1613+
}
1614+
}
16021615
return;
16031616
}
16041617

lib/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
358358
/** @brief The maximum time in seconds for the typedef simplification */
359359
std::size_t typedefMaxTime{};
360360

361+
/** @brief Unsafe exitcode => do not force non-zero exitcode when there are critical errors */
362+
bool unsafeExitCode = false;
363+
361364
/** @brief defines given by the user */
362365
std::string userDefines;
363366

0 commit comments

Comments
 (0)