Skip to content

Commit

Permalink
Fix #13523 Mention including cpp file for error in header
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 3, 2025
1 parent dfce062 commit 11a1f22
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ class CPPCHECKLIB Check {
/** Base class used for whole-program analysis */
class CPPCHECKLIB FileInfo {
public:
FileInfo() = default;
explicit FileInfo(const std::string& f0 = {}) : file0(f0) {}
virtual ~FileInfo() = default;
virtual std::string toString() const {
return std::string();
}
std::string file0;
};

virtual FileInfo * getFileInfo(const Tokenizer& /*tokenizer*/, const Settings& /*settings*/) const {
Expand Down
3 changes: 2 additions & 1 deletion lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ namespace
/** data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
using Check::FileInfo::FileInfo;
/** unsafe array index usage */
std::list<CTU::FileInfo::UnsafeUsage> unsafeArrayIndex;

Expand Down Expand Up @@ -951,7 +952,7 @@ Check::FileInfo *CheckBufferOverrun::getFileInfo(const Tokenizer &tokenizer, con
if (unsafeArrayIndex.empty() && unsafePointerArith.empty()) {
return nullptr;
}
auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]);
fileInfo->unsafeArrayIndex = unsafeArrayIndex;
fileInfo->unsafePointerArith = unsafePointerArith;
return fileInfo;
Expand Down
18 changes: 10 additions & 8 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3566,6 +3566,7 @@ namespace
/* multifile checking; one definition rule violations */
class MyFileInfo : public Check::FileInfo {
public:
using Check::FileInfo::FileInfo;
struct NameLoc {
std::string className;
std::string fileName;
Expand Down Expand Up @@ -3727,14 +3728,15 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<C
locationList.emplace_back(nameLoc.fileName, nameLoc.lineNumber, nameLoc.column);
locationList.emplace_back(it->second.fileName, it->second.lineNumber, it->second.column);

const ErrorMessage errmsg(std::move(locationList),
emptyString,
Severity::error,
"$symbol:" + nameLoc.className +
"\nThe one definition rule is violated, different classes/structs have the same name '$symbol'",
"ctuOneDefinitionRuleViolation",
CWE_ONE_DEFINITION_RULE,
Certainty::normal);
ErrorMessage errmsg(std::move(locationList),
emptyString,
Severity::error,
"$symbol:" + nameLoc.className +
"\nThe one definition rule is violated, different classes/structs have the same name '$symbol'",
"ctuOneDefinitionRuleViolation",
CWE_ONE_DEFINITION_RULE,
Certainty::normal);
errmsg.file0 = fi->file0;
errorLogger.reportErr(errmsg);

foundErrors = true;
Expand Down
16 changes: 9 additions & 7 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ namespace
/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
using Check::FileInfo::FileInfo;
/** function arguments that are dereferenced without checking if they are null */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

Expand All @@ -617,7 +618,7 @@ Check::FileInfo *CheckNullPointer::getFileInfo(const Tokenizer &tokenizer, const
if (unsafeUsage.empty())
return nullptr;

auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]);
fileInfo->unsafeUsage = unsafeUsage;
return fileInfo;
}
Expand Down Expand Up @@ -666,12 +667,13 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
if (locationList.empty())
continue;

const ErrorMessage errmsg(locationList,
emptyString,
warning ? Severity::warning : Severity::error,
"Null pointer dereference: " + unsafeUsage.myArgumentName,
"ctunullpointer",
CWE_NULL_POINTER_DEREFERENCE, Certainty::normal);
ErrorMessage errmsg(locationList,
emptyString,
warning ? Severity::warning : Severity::error,
"Null pointer dereference: " + unsafeUsage.myArgumentName,
"ctunullpointer",
CWE_NULL_POINTER_DEREFERENCE, Certainty::normal);
errmsg.file0 = fi->file0;
errorLogger.reportErr(errmsg);

foundErrors = true;
Expand Down
18 changes: 10 additions & 8 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,7 @@ namespace
/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
using Check::FileInfo::FileInfo;
/** function arguments that data are unconditionally read */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

Expand All @@ -1725,7 +1726,7 @@ Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer &tokenizer, const S
if (unsafeUsage.empty())
return nullptr;

auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfo(tokenizer.list.getFiles()[0]);
fileInfo->unsafeUsage = unsafeUsage;
return fileInfo;
}
Expand Down Expand Up @@ -1768,13 +1769,14 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
if (locationList.empty())
continue;

const ErrorMessage errmsg(locationList,
emptyString,
Severity::error,
"Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression,
"ctuuninitvar",
CWE_USE_OF_UNINITIALIZED_VARIABLE,
Certainty::normal);
ErrorMessage errmsg(locationList,
emptyString,
Severity::error,
"Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression,
"ctuuninitvar",
CWE_USE_OF_UNINITIALIZED_VARIABLE,
Certainty::normal);
errmsg.file0 = fi->file0;
errorLogger.reportErr(errmsg);

foundErrors = true;
Expand Down
1 change: 1 addition & 0 deletions test/cli/whole-program/nullpointer1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "nullpointer1.h"
8 changes: 8 additions & 0 deletions test/cli/whole-program/nullpointer1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "nullpointer1_1.h"

template<typename T>
void f(T* p) {
if (sizeof(T) == 4)
p = nullptr;
g(p);
}
4 changes: 4 additions & 0 deletions test/cli/whole-program/nullpointer1_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
template<typename T>
void g(T* p) {
*p = 0;
};
25 changes: 25 additions & 0 deletions test/cli/whole-program_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import shutil
from testutils import cppcheck
import xml.etree.ElementTree as ET

__script_dir = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -358,3 +359,27 @@ def test_checkclass_project_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_checkclass_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])

def __test_nullpointer_file0(extra_args):
args = [
'-q',
'--xml',
'--error-exitcode=1',
'whole-program/nullpointer1.cpp'
]

args += extra_args

ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
results = ET.fromstring(stdout)
file0 = ''
for e in root.findall('errors/error'):
if (e.attrib['id'] == 'ctunullpointer'):
file0 = e.attrib['file0']
assert file0 == 'whole-program/nullpointer1.cpp'
assert stdout == ''
assert ret == 1, stdout


def test_nullpointer_file0():
__test_checkclass(['-j1'])

0 comments on commit 11a1f22

Please sign in to comment.