Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
02a86e0
Fix #11842 FN constParameterPointer with library function
chrchr-github Jul 20, 2023
e297190
const
chrchr-github Jul 20, 2023
8b89852
const
chrchr-github Jul 20, 2023
0843ce9
suppress
chrchr-github Jul 20, 2023
1907388
suppress, fix
chrchr-github Jul 20, 2023
ea15f12
const
chrchr-github Jul 21, 2023
fd55133
Use getOverloadedFunctions()
chrchr-github Aug 2, 2023
47c5485
Comment
chrchr-github Aug 2, 2023
597cfa5
const
chrchr-github Aug 2, 2023
3463e48
const
chrchr-github Aug 2, 2023
36cd2fe
Merge
chrchr-github Aug 2, 2023
115cc01
const
chrchr-github Aug 2, 2023
bfb8afa
Delete
chrchr-github Aug 2, 2023
7bd1cd6
const / suppress
chrchr-github Aug 2, 2023
a270b79
const
chrchr-github Aug 2, 2023
4d267e6
const
chrchr-github Aug 2, 2023
726dc1d
const
chrchr-github Aug 2, 2023
0418a53
const
chrchr-github Aug 2, 2023
6a19d3f
const
chrchr-github Aug 2, 2023
ae8befb
const
chrchr-github Aug 2, 2023
c953763
const
chrchr-github Aug 2, 2023
9f84088
const
chrchr-github Aug 2, 2023
c956233
const
chrchr-github Aug 2, 2023
846de59
const
chrchr-github Aug 2, 2023
489b17c
const
chrchr-github Aug 2, 2023
99b3caa
const
chrchr-github Aug 2, 2023
46c59f7
const
chrchr-github Aug 2, 2023
b8b067b
const
chrchr-github Aug 2, 2023
9b85ae7
const
chrchr-github Aug 2, 2023
907b248
const
chrchr-github Aug 2, 2023
d8f17e6
Fix cfg
chrchr-github Aug 2, 2023
8ce46a7
unsuppress
chrchr-github Aug 2, 2023
4b406da
const
chrchr-github Aug 2, 2023
dc8eba9
Revert "Delete"
chrchr-github Aug 2, 2023
d7b238a
Merge branch 'danmar:main' into chr_Fix11842
chrchr-github Aug 2, 2023
f915148
undo
chrchr-github Aug 2, 2023
9e0734d
Merge branch 'chr_Fix11842' of https://github.com/chrchr-github/cppch…
chrchr-github Aug 2, 2023
80072c1
undo
chrchr-github Aug 2, 2023
e090a11
Fix cfg
chrchr-github Aug 3, 2023
e900fce
Merge branch 'chr_Fix11842' of https://github.com/chrchr-github/cppch…
chrchr-github Aug 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cfg/posix.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3362,7 +3362,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
<use-retval/>
<returnValue type="int"/>
<noreturn>false</noreturn>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand Down
3 changes: 2 additions & 1 deletion cfg/qt.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,12 @@
<!-- QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = ...) -->
<!-- QAction *QMenu::addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = ...) -->
<!-- QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0) -->
<!-- void QWidget::addAction(QAction *action) -->
<function name="QMenu::addAction">
<noreturn>false</noreturn>
<returnValue type="QAction *"/>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-uninit/>
<not-bool/>
</arg>
Expand Down
22 changes: 11 additions & 11 deletions cfg/std.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@
<returnValue type="int"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand All @@ -1595,7 +1595,7 @@
<returnValue type="int"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand Down Expand Up @@ -2290,7 +2290,7 @@
<returnValue type="int"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand Down Expand Up @@ -3815,7 +3815,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<returnValue type="void *"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<minsize type="argvalue" arg="3"/>
Expand All @@ -3838,7 +3838,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<returnValue type="wchar_t *"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<minsize type="argvalue" arg="3"/>
Expand Down Expand Up @@ -4851,7 +4851,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<valid>0:255</valid>
</arg>
</function>
<!-- wchar_t wcschr(const wchar_t *cs, wchar_t c); -->
<!-- wchar_t* wcschr(const wchar_t *cs, wchar_t c); -->
<function name="wcschr,std::wcschr">
<use-retval/>
<returnValue type="wchar_t"/>
Expand Down Expand Up @@ -5154,7 +5154,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<returnValue type="char *"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<strz/>
Expand All @@ -5172,7 +5172,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<returnValue type="wchar_t *"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<strz/>
Expand Down Expand Up @@ -5429,7 +5429,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<returnValue type="wchar_t *"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<strz/>
Expand Down Expand Up @@ -8263,7 +8263,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<noreturn>false</noreturn>
<returnValue type="void"/>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
<not-bool/>
Expand All @@ -8274,7 +8274,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<noreturn>false</noreturn>
<returnValue type="char *"/>
<use-retval/>
<arg nr="1">
<arg nr="1" direction="in">
<not-null/>
<not-uninit/>
<not-bool/>
Expand Down
14 changes: 7 additions & 7 deletions cfg/windows.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@
<function name="CloseHandle">
<noreturn>false</noreturn>
<returnValue type="BOOL"/>
<arg nr="1" direction="in">
<arg nr="1">
<not-uninit/>
</arg>
</function>
Expand Down Expand Up @@ -3901,7 +3901,7 @@ HFONT CreateFont(
<not-uninit/>
<not-null/>
</arg>
<arg nr="4" direction="in">
<arg nr="4">
<not-uninit/>
</arg>
<arg nr="5" direction="in">
Expand Down Expand Up @@ -4772,7 +4772,7 @@ HFONT CreateFont(
<function name="FreeLibrary">
<noreturn>false</noreturn>
<returnValue type="BOOL"/>
<arg nr="1" direction="in">
<arg nr="1">
<not-uninit/>
<not-bool/>
</arg>
Expand Down Expand Up @@ -4884,7 +4884,7 @@ HFONT CreateFont(
<returnValue type="FARPROC"/>
<use-retval/>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-uninit/>
<not-bool/>
<not-null/>
Expand Down Expand Up @@ -4971,7 +4971,7 @@ HFONT CreateFont(
<noreturn>false</noreturn>
<returnValue type="BOOL"/>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand Down Expand Up @@ -5364,7 +5364,7 @@ HFONT CreateFont(
<returnValue type="BOOL"/>
<noreturn>false</noreturn>
<leak-ignore/>
<arg nr="1" direction="in">
<arg nr="1">
<not-uninit/>
<not-bool/>
</arg>
Expand Down Expand Up @@ -6324,7 +6324,7 @@ HFONT CreateFont(
<function name="LocalFree">
<noreturn>false</noreturn>
<returnValue type="HLOCAL"/>
<arg nr="1" direction="in">
<arg nr="1">
<not-bool/>
</arg>
</function>
Expand Down
4 changes: 2 additions & 2 deletions cfg/wxwidgets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6820,7 +6820,7 @@
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="wxSizerItem*"/>
<arg nr="1" direction="in">
<arg nr="1">
<!-- <not-null/> Deactivated due to FPs with overloaded function: wxSizerItem * Add (int width, int height, int proportion=0, int flag=0, int border=0, wxObject *userData=NULL) -->
<not-uninit/>
</arg>
Expand Down Expand Up @@ -7309,7 +7309,7 @@
<noreturn>false</noreturn>
<leak-ignore/>
<returnValue type="void"/>
<arg nr="1" direction="in">
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
Expand Down
4 changes: 2 additions & 2 deletions gui/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ CodeEditor::CodeEditor(QWidget *parent) :
QShortcut *copyText = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_C),this);
QShortcut *allText = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_A),this);
#else
QShortcut *copyText = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_C),this);
QShortcut *allText = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_A),this);
const QShortcut *copyText = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_C),this);
const QShortcut *allText = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_A),this);
#endif

connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
Expand Down
2 changes: 1 addition & 1 deletion gui/resultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
mContextItem = mModel.itemFromIndex(index);
if (mContextItem && mApplications->getApplicationCount() > 0 && mContextItem->parent()) {
//Disconnect all signals
for (QAction* action : actions) {
for (const QAction* action : actions) {
disconnect(action, SIGNAL(triggered()), signalMapper, SLOT(map()));
}

Expand Down
19 changes: 13 additions & 6 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,13 +1645,20 @@ void CheckOther::checkConstPointer()
if (Token::simpleMatch(parent, "=") && parent->astOperand1() == tok)
continue;
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
if (ftok->function() && !parent->isCast()) {
const Variable* argVar = ftok->function()->getArgumentVar(argn);
if (argVar && argVar->valueType() && argVar->valueType()->isConst(vt->pointer)) {
bool inconclusive{};
if (!isVariableChangedByFunctionCall(ftok, vt->pointer, var->declarationId(), mSettings, &inconclusive) && !inconclusive)
continue;
if (ftok->function()) {
const bool isCastArg = parent->isCast() && !ftok->function()->getOverloadedFunctions().empty(); // assume that cast changes the called function
if (!isCastArg) {
const Variable* argVar = ftok->function()->getArgumentVar(argn);
if (argVar && argVar->valueType() && argVar->valueType()->isConst(vt->pointer)) {
bool inconclusive{};
if (!isVariableChangedByFunctionCall(ftok, vt->pointer, var->declarationId(), mSettings, &inconclusive) && !inconclusive)
continue;
}
}
} else {
const auto dir = mSettings->library.getArgDirection(ftok, argn + 1);
if (dir == Library::ArgumentChecks::Direction::DIR_IN)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this logic should be in isVariableChangedByFunctionCall, but I wonder why we skip it when the parent is a cast.

@chrchr-github chrchr-github Jul 30, 2023

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this test:

void f(int, const int*);
void f(int i, int* p) {
    f(i, const_cast<const int*>(p));
}

Seems not ideal though.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isVariableChangedByFunctionCall() already has handling for library functions, but it is not quite aligned with getTokenArgumentFunction() (constructors, init lists).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isVariableChangedByFunctionCall() already has handling for library functions, but it is not quite aligned with getTokenArgumentFunction() (constructors, init lists).

We should probably have an issue to track that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only rely on isVariableChangedByFunctionCall(), there would be FPs for those:

struct A {
    int* x;
    A(int* y) : x(y) {}
};
void f(int* q) {
    int* a[1] = { q };
}

I'm not convinced that isVariableChangedByFunctionCall() should return true here.

continue;
}
}
else if (Token::simpleMatch(parent, "(")) {
Expand Down
2 changes: 1 addition & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4362,7 +4362,7 @@ std::vector<const Function*> Function::getOverloadedFunctions() const
it != scope->functionMap.end() && it->first == tokenDef->str();
++it) {
const Function* func = it->second;
if (isMemberFunction == func->isStatic())
if (isMemberFunction && isMemberFunction == func->isStatic())
continue;
result.push_back(func);
}
Expand Down
22 changes: 11 additions & 11 deletions test/cfg/gnu.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void unreachableCode_error(void) // #11197
}
#endif

int nullPointer_gethostbyname2_r(const char* name, int af, struct hostent* ret, char* buf, size_t buflen, struct hostent** result, int* h_errnop)
int nullPointer_gethostbyname2_r(const char* name, int af, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
{
// cppcheck-suppress nullPointer
(void) gethostbyname2_r(NULL, af, ret, buf, buflen, result, h_errnop);
Expand All @@ -54,7 +54,7 @@ int nullPointer_gethostbyname2_r(const char* name, int af, struct hostent* ret,
return gethostbyname2_r(name, af, ret, buf, buflen, result, h_errnop);
}

int nullPointer_gethostbyname_r(const char* name, struct hostent* ret, char* buf, size_t buflen, struct hostent** result, int* h_errnop)
int nullPointer_gethostbyname_r(const char* name, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
{
// cppcheck-suppress nullPointer
(void) gethostbyname_r(NULL, ret, buf, buflen, result, h_errnop);
Expand All @@ -70,7 +70,7 @@ int nullPointer_gethostbyname_r(const char* name, struct hostent* ret, char* buf
}


int nullPointer_gethostbyaddr_r(const void* addr, socklen_t len, int type, struct hostent* ret, char* buf, size_t buflen, struct hostent** result, int* h_errnop)
int nullPointer_gethostbyaddr_r(const void* addr, socklen_t len, int type, struct hostent* ret, const char* buf, size_t buflen, struct hostent** result, const int* h_errnop)
{
// cppcheck-suppress nullPointer
(void) gethostbyaddr_r(NULL, len, type, ret, buf, buflen, result, h_errnop);
Expand Down Expand Up @@ -109,7 +109,7 @@ int nullPointer_getopt_long_only(int argc, char* const* argv, const char* optstr
return getopt_long_only(argc, argv, optstring, longopts, longindex);
}

int nullPointer_getservent_r(struct servent *restrict result_buf, char *restrict buf, size_t buflen, struct servent **restrict result)
int nullPointer_getservent_r(struct servent *restrict result_buf, const char *restrict buf, size_t buflen, struct servent **restrict result)
{
// cppcheck-suppress nullPointer
(void) getservent_r(NULL, buf, buflen, result);
Expand All @@ -122,7 +122,7 @@ int nullPointer_getservent_r(struct servent *restrict result_buf, char *restrict

void *bufferAccessOutOfBounds_memrchr(const void *s, int c, size_t n)
{
char buf[42]={0};
const char buf[42]={0};
(void)memrchr(buf,c,42);
// cppcheck-suppress bufferAccessOutOfBounds
(void)memrchr(buf,c,43);
Expand Down Expand Up @@ -275,17 +275,17 @@ int no_resourceLeak_mkostemp_02(char *template, int flags)
return mkostemp(template, flags);
}

void valid_code(int argInt1, va_list valist_arg, int * parg)
void valid_code(int argInt1, va_list valist_arg, const int * parg)
{
char *p;

if (__builtin_expect(argInt1, 0)) {}
if (__builtin_expect_with_probability(argInt1 + 1, 2, 0.5)) {}
if (__glibc_unlikely(argInt1 != 0)) {}
if (__glibc_likely(parg != NULL)) {}
void *ax1 = __builtin_assume_aligned(parg, 16);
const void *ax1 = __builtin_assume_aligned(parg, 16);
printf("%p", ax1);
void *ax2 = __builtin_assume_aligned(parg, 32, 8);
const void *ax2 = __builtin_assume_aligned(parg, 32, 8);
printf("%p", ax2);

p = (char *)malloc(10);
Expand Down Expand Up @@ -316,7 +316,7 @@ void valid_code(int argInt1, va_list valist_arg, int * parg)

if (__alignof__(int) == 4) {}

void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
const void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
printf("%p", p_mmap);
munmap(p_mmap, 1);

Expand Down Expand Up @@ -368,7 +368,7 @@ void memleak_xmalloc()

void memleak_mmap()
{
void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
const void * p_mmap = mmap(NULL, 1, PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
printf("%p", p_mmap);
// cppcheck-suppress memleak
}
Expand All @@ -391,7 +391,7 @@ void bufferAccessOutOfBounds__builtin_memset(void)

void bufferAccessOutOfBounds()
{
char buf[2] = "a";
const char buf[2] = "a";
// This is valid
sethostname(buf, 2);
// cppcheck-suppress bufferAccessOutOfBounds
Expand Down
Loading