Skip to content

Commit

Permalink
Merge branch 'danmar:main' into chr_Fix11389
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 2, 2023
2 parents efd551e + 389e446 commit 6d779ab
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/asan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- name: Install clang
run: |
sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- name: Install clang
run: |
sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/iwyu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jobs:
- name: Install clang
run: |
sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 17
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/scriptcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ jobs:

needs: build
# 'ubuntu-22.04' removes Python 2.7, 3.5 and 3.6 so keep the previous LTS version
# 'ubutunu-20.04' no longer works on 2.7 - TODO: re-added in a different way or remove support for it?
runs-on: ubuntu-20.04
container:
image: python:2.7.18-buster
strategy:
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9, '3.10', '3.11']
python-version: [3.5, 3.6, 3.7, 3.8, 3.9, '3.10', '3.11']
fail-fast: false

steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tsan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- name: Install clang
run: |
sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ubsan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- name: Install clang
run: |
sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ Ryan Pavlik
Samir Aguiar
Sam Truscott
Samuel Degrande
Samuel Poláček
Sandeep Dutta
Savvas Etairidis
Scott Furry
Expand Down
2 changes: 1 addition & 1 deletion addons/misra_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def misra_9_x(self, data, rule, rawTokens = None):
has_var = True
continue
unknown_constant = True
cppcheckdata.reportError(sz, 'error', f'Unknown constant {t.str}, please review configuration', 'misra', 'config')
cppcheckdata.reportError(sz, 'error', 'Unknown constant {}, please review configuration'.format(t.str), 'misra', 'config')
has_config_errors = True
if t.isArithmeticalOp:
tokens += [t.astOperand1, t.astOperand2]
Expand Down
6 changes: 3 additions & 3 deletions cfg/posix.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
<leak-ignore/>
<use-retval/>
<returnValue type="long int"/>
<arg nr="1" direction="in">
<arg nr="1" direction="inout">
<not-null/>
<not-uninit/>
<not-bool/>
Expand All @@ -2209,7 +2209,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
<leak-ignore/>
<use-retval/>
<returnValue type="long int"/>
<arg nr="1" direction="in">
<arg nr="1" direction="inout">
<not-null/>
<not-uninit/>
<not-bool/>
Expand All @@ -2221,7 +2221,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
<leak-ignore/>
<use-retval/>
<returnValue type="double"/>
<arg nr="1" direction="in">
<arg nr="1" direction="inout">
<not-null/>
<not-uninit/>
<not-bool/>
Expand Down
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if ((def || mSettings.preprocessOnly) && !maxconfigs)
mSettings.maxConfigs = 1U;

if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1) {
if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1 && mSettings.buildDir.empty()) {
printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
}

Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ T* getTokenArgumentFunctionImpl(T* tok, int& argn)
parent = parent->astParent();

// passing variable to subfunction?
if (Token::Match(parent, "[(,{]"))
if (Token::Match(parent, "[[(,{]"))
;
else if (Token::simpleMatch(parent, ":")) {
while (Token::Match(parent, "[?:]"))
Expand Down
4 changes: 2 additions & 2 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ void CheckClass::noConstructorError(const Token *tok, const std::string &classna
void CheckClass::noExplicitConstructorError(const Token *tok, const std::string &classname, bool isStruct)
{
const std::string message(std::string(isStruct ? "Struct" : "Class") + " '$symbol' has a constructor with 1 argument that is not explicit.");
const std::string verbose(message + " Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.");
const std::string verbose(message + " Such, so called \"Converting constructors\", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.");
reportError(tok, Severity::style, "noExplicitConstructor", "$symbol:" + classname + '\n' + message + '\n' + verbose, CWE398, Certainty::normal);
}

Expand Down Expand Up @@ -2582,7 +2582,7 @@ void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const st
"passed to the function. This change should not cause compiler errors but it does not "
"necessarily make sense conceptually. Think about your design and the task of the function first - "
"is it a function that must not access members of class instances? And maybe it is more appropriate "
"to move this function to a unnamed namespace.", CWE398, Certainty::inconclusive);
"to move this function to an unnamed namespace.", CWE398, Certainty::inconclusive);
}

//---------------------------------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ void CheckOther::checkVariableScope()
}
}

bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used)
bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& used) const
{
const Scope* scope = tok->next()->scope();
bool loopVariable = scope->isLoopScope();
Expand Down Expand Up @@ -1071,6 +1071,18 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (scope->bodyStart && scope->bodyStart->isSimplifiedScope())
return false; // simplified if/for/switch init statement
}
if (var->isArrayOrPointer()) {
int argn{};
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { // var passed to function?
if (ftok->next()->astParent()) { // return value used?
if (ftok->function() && Function::returnsPointer(ftok->function()))
return false;
const std::string ret = mSettings->library.returnValueType(ftok); // assume that var is returned
if (!ret.empty() && ret.back() == '*')
return false;
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class CPPCHECKLIB CheckOther : public Check {

/** @brief %Check scope of variables */
void checkVariableScope();
static bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
bool checkInnerScope(const Token *tok, const Variable* var, bool& used) const;

/** @brief %Check for comma separated statements in return */
void checkCommaSeparatedReturn();
Expand Down
30 changes: 21 additions & 9 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
continue;

bool isTemplateArg = false;
if (tok->next()->str() != "(") {
if (!Token::Match(tok->next(), "(|{")) {
const Token *start = tok;
while (Token::Match(start->tokAt(-2), "%name% ::"))
start = start->tokAt(-2);
Expand Down Expand Up @@ -3490,16 +3490,28 @@ const Token *Type::initBaseInfo(const Token *tok, const Token *tok1)
return tok2;
}

const std::string& Type::name() const
std::string Type::name() const
{
const Token* next = classDef->next();
const Token* start = classDef->next();
if (classScope && classScope->enumClass && isEnumType())
return next->strAt(1);
if (next->str() == "class")
return next->strAt(1);
if (next->isName())
return next->str();
return emptyString;
start = start->tokAt(1);
else if (start->str() == "class")
start = start->tokAt(1);
else if (!start->isName())
return emptyString;
const Token* next = start;
while (Token::Match(next, "::|<|>|(|)|[|]|*|&|&&|%name%")) {
if (Token::Match(next, "<|(|[") && next->link())
next = next->link();
next = next->next();
}
std::string result;
for (const Token* tok = start; tok != next; tok = tok->next()) {
if (!result.empty())
result += ' ';
result += tok->str();
}
return result;
}

void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const
Expand Down
2 changes: 1 addition & 1 deletion lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class CPPCHECKLIB Type {
}
}

const std::string& name() const;
std::string name() const;

const std::string& type() const {
return classDef ? classDef->str() : emptyString;
Expand Down
6 changes: 4 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4759,7 +4759,8 @@ void Tokenizer::setVarIdPass1()
continue;
}

if (!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) {
if ((!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) &&
!Token::simpleMatch(tok->next(), ": ;")) {
const std::map<std::string, nonneg int>::const_iterator it = variableMap.map(globalNamespace).find(tok->str());
if (it != variableMap.map(globalNamespace).end()) {
tok->varId(it->second);
Expand Down Expand Up @@ -5251,7 +5252,8 @@ void Tokenizer::createLinks2()
} else if (token->str() == "<" &&
((token->previous() && (token->previous()->isTemplate() ||
(token->previous()->isName() && !token->previous()->varId()) ||
(token->strAt(-1) == "]" && (!Token::Match(token->linkAt(-1)->previous(), "%name%|)") || token->linkAt(-1)->previous()->isKeyword())))) ||
(token->strAt(-1) == "]" && (!Token::Match(token->linkAt(-1)->previous(), "%name%|)") || token->linkAt(-1)->previous()->isKeyword())) ||
(token->strAt(-1) == ")" && token->linkAt(-1)->strAt(-1) == "operator"))) ||
Token::Match(token->next(), ">|>>"))) {
type.push(token);
if (token->previous()->str() == "template")
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ void TokenList::validateAst() const
continue;
}

if (const Token* lambdaEnd = findLambdaEndToken(tok)) { // skip lambda captures
if (findLambdaEndToken(tok)) { // skip lambda captures
tok = tok->link();
continue;
}
Expand Down
7 changes: 7 additions & 0 deletions test/cfg/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ double nullPointer_erand48(unsigned short xsubi[3])
return erand48(xsubi);
}

struct non_const_parameter_erand48_struct { unsigned short xsubi[3]; };
// No warning is expected that dat can be const
double non_const_parameter_erand48(struct non_const_parameter_erand48_struct *dat)
{
return erand48(dat->xsubi);
}

unsigned short *nullPointer_seed48(unsigned short seed16v[3])
{
// cppcheck-suppress nullPointer
Expand Down
2 changes: 1 addition & 1 deletion test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6061,7 +6061,7 @@ class TestClass : public TestFixture {
" int i{};\n"
" S f() { return S{ &i }; }\n"
"};\n");
TODO_ASSERT_EQUALS("[test.cpp:7]: (style, inconclusive) Technically the member function 'C::f' can be const.\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (style, inconclusive) Technically the member function 'C::f' can be const.\n", errout.str());

checkConst("struct S {\n"
" explicit S(const int* p) : mp(p) {}\n"
Expand Down
39 changes: 39 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TestOther : public TestFixture {
TEST_CASE(varScope32); // #11441
TEST_CASE(varScope33);
TEST_CASE(varScope34);
TEST_CASE(varScope35);

TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
Expand Down Expand Up @@ -1636,6 +1637,37 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void varScope35() { // #11845
check("void f(int err, const char* src) {\n"
" const char* msg = \"Success\";\n"
" char buf[42];\n"
" if (err != 0)\n"
" msg = strcpy(buf, src);\n"
" printf(\"%d: %s\\n\", err, msg);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("char* g(char* dst, const char* src);\n"
"void f(int err, const char* src) {\n"
" const char* msg = \"Success\";\n"
" char buf[42];\n"
" if (err != 0)\n"
" msg = g(buf, src);\n"
" printf(\"%d: %s\\n\", err, msg);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("char* g(char* dst, const char* src);\n"
"void f(int err, const char* src) {\n"
" const char* msg = \"Success\";\n"
" char buf[42];\n"
" if (err != 0)\n"
" g(buf, src);\n"
" printf(\"%d: %s\\n\", err, msg);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str());
}

#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
// Clear the error buffer..
Expand Down Expand Up @@ -3754,6 +3786,13 @@ class TestOther : public TestFixture {
" p = q;\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct S { int a[1]; };\n"
"void f(S* s) {\n"
" if (s->a[0]) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as pointer to const\n",
errout.str());
}

void switchRedundantAssignmentTest() {
Expand Down
19 changes: 19 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3531,6 +3531,25 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS(true, tok1->link() == tok2);
ASSERT_EQUALS(true, tok2->link() == tok1);
}

{
const char code[] = "struct S {\n" // #11840
" template<typename T, typename U>\n"
" void operator() (int);\n"
"};\n"
"void f() {\n"
" S s;\n"
" s.operator()<int, int>(1);\n"
"}\n";
errout.str("");
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
const Token* tok1 = Token::findsimplematch(tokenizer.tokens(), "< int");
const Token* tok2 = Token::findsimplematch(tok1, "> (");
ASSERT_EQUALS(true, tok1->link() == tok2);
ASSERT_EQUALS(true, tok2->link() == tok1);
}
}

void simplifyString() {
Expand Down
8 changes: 8 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6812,6 +6812,14 @@ class TestValueFlow : public TestFixture {
" dummy_resource::log.clear();\n"
"}\n";
valueOfTok(code, "log");

code = "struct D : B<int> {\n"
" D(int i, const std::string& s) : B<int>(i, s) {}\n"
"};\n"
"template<> struct B<int>::S {\n"
" int j;\n"
"};\n";
valueOfTok(code, "B");
}

void valueFlowCrash() {
Expand Down
11 changes: 11 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,17 @@ class TestVarID : public TestFixture {
" break;\n"
" }\n"
"}", "test.c"));

ASSERT_EQUALS("1: int * f ( ) {\n" // #11838
"2: int * label@1 ; label@1 = 0 ;\n"
"3: label : ;\n"
"4: return label@1 ;\n"
"5: }\n",
tokenize("int* f() {\n"
" int* label = 0;\n"
"label:\n"
" return label;\n"
"}"));
}

void varid_structinit() { // #6406
Expand Down

0 comments on commit 6d779ab

Please sign in to comment.