Commit Graph

242 Commits

Author SHA1 Message Date
Daniel Marjamäki 253f2c9e9d Fixed #9043 (false positive & regression: Variable '*s' is reassigned a value before the old one has been used.) 2019-04-16 19:07:26 +02:00
Paul Fultz II a90caa7e5a Fix issue 9006: False positive: Return value of function std::move() is not used.
This is trying to fix the issue by fixing the ast and symbol database. First, the ast nodes will be created for the init list and the symbol database will not mark it as a scope. I am not sure if this is the correct approach as I dont really understand how the AST part works.

It did change the AST for `try {} catch (...) {}` but that is because it incorrectly treats `try {}` as an initializer list.
2019-04-15 06:37:27 +02:00
Daniel Marjamäki 51b64191e5 Fixed slow checking in FwdAnalysis
Credit to OSS-Fuzz for reporting this!
2019-03-30 14:22:24 +01:00
Sebastian 29815b2dd8
Fix #8922 (SIGSEGV below exprDependsOnThis - stack overflow?) (#1761)
This limits the recursion depth as a last line of defense to avoid stack
overflows when there are really huge arrays.
See https://trac.cppcheck.net/ticket/8922
2019-03-26 18:57:01 +01:00
Daniel Marjamäki 7995b2fb86 getArguments: Avoid too deep recursion in array initialization 2019-03-24 11:51:28 +01:00
Daniel Marjamäki 418eb43d45 Fixed #9032 (False-positive detection of reassigned value before used for pointer parameter) 2019-03-23 19:01:02 +01:00
Paul Fultz II 774464eabb Fix issue 8996: False positive duplicateCondition
This fixes issue 8996 by improving the alias checking by using lifetime analysis. It also extends the lifetime checker to handle constructors and initializer lists for containers and arrays.
2019-03-19 06:25:10 +01:00
Sebastian 0934577dda
Library configuration: function argument direction fixes and enhancements (#1722)
* std.cfg: Add further argument directions (in, out, inout).

* testlibrary.cpp: Add test for function argument direction configuration.

* std.cfg: runastyle and add some more direction configurations.

* library.h: Add documentation for function argument direction enum.

* Do not use "direction" library information for pointer arguments.

Also fix further unmatched uninitvar messages in std configuration
tests.

* std.cfg: Add more argument direction configurations.

* test/cfg/std.c: Add test for argument direction configuration.

* astutils.cpp: Only ignore pointer arguments for out/inout arguments.

* library.h: Use suggested documentation for argument direction enum.
2019-03-04 22:57:40 +01:00
Sebastian 9a5fcddb5d
Library configuration: Enable configuring the direction of arguments. (#1717)
This enhances the library configuration so the direction of function
arguments can be specified (in, out, inout).
isVariableChangedByFunctionCall() uses this information now to avoid
guessing.
2019-03-01 15:47:08 +01:00
Daniel Marjamäki 845c407ab7 Fixed pattern matching in isVariableChanged() to handle normal token list better 2019-03-01 13:23:03 +01:00
Daniel Marjamäki 4f5a426fe5 Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference 2019-02-28 17:02:46 +01:00
Daniel Marjamäki 494dd2ba3a Revert "Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference"
This reverts commit 14a0031e88.
2019-02-28 15:56:25 +01:00
Daniel Marjamäki 14a0031e88 Improved isVariableChangedByFunctionCall, better logic when parameter might be passed by reference 2019-02-28 10:26:47 +01:00
Daniel Marjamäki 857681a049 Make quick fix for uninitialized variable false positive. Will look more at this soon. 2019-02-28 09:52:52 +01:00
Paul Fultz II 941dd79f0d Fix FP issue 8986: constArgument check warns for elements of const array (#1683) 2019-02-22 06:39:59 +01:00
Daniel Marjamäki bd7790fd8c Update copyright year 2019-02-09 07:24:06 +01:00
Paul Fultz II d7c20b15e7 Fix FP with const argument when doing a variable and cast (#1649) 2019-02-07 14:59:09 +01:00
Daniel Marjamäki 8c07be136a Fixed #8949 (False Positive: Variable 'f' is assigned a value that is never used.) 2019-01-22 21:16:27 +01:00
Daniel Marjamäki d50c3de740 Remove inline suppression and fix false positive. 2019-01-13 07:59:41 +01:00
Daniel Marjamäki 2b1b8f8fe9 Fix Cppcheck warnings
[ci skip]
2019-01-12 18:29:08 +01:00
Daniel Marjamäki 0f9ce5edd5 Fixed #8365 (False positive on memory leak when assigned inside if statement) 2019-01-08 20:54:39 +01:00
Daniel Marjamäki 2b63997c2c Fixed #8928 (false positive: (style) Variable 'x' is assigned a value that is never used.) 2019-01-02 20:23:02 +01:00
Daniel Marjamäki 236c88151f Fixed #8926 (false positive: (style) Condition 's.x<=y' is always true) 2019-01-02 18:05:55 +01:00
Daniel Marjamäki 115be7dfc8 ValueFlow: better FwdAnalysis for complex expressions 2019-01-01 18:23:47 +01:00
Daniel Marjamäki a73e989d1a isConstVarExpression: Fix FPs when there is C++ cast 2019-01-01 11:34:44 +01:00
Daniel Marjamäki ed514644b8 Renamed FwdAnalysisAllPaths to FwdAnalysis 2018-12-31 18:00:47 +01:00
Daniel Marjamäki 141ce7cd63 ValueFlow: Use FwdAnalysisAllPaths in ValueFlow to track complex expressions 2018-12-31 17:05:46 +01:00
rikardfalkeborn 036fcf7827 Fix warnings (#1541)
* Fix reorder warning

* Fix override warnings

Clang warns about missing overrides.

* Remove unused function

* Add missing argument to function calls
2018-12-28 12:59:05 +01:00
Daniel Marjamäki 6fef02498c Fixed #7263 (False negative: redundant assignment using +=) 2018-12-27 17:27:53 +01:00
Daniel Marjamäki ddbe5c129c Clarify FwdAnalysis. It is useful for checks that need data flow analysis of ALL paths. 2018-12-25 11:56:06 +01:00
Daniel Marjamäki e2c433a0f8 Fixed #8914 (False positive with unary_function argument) 2018-12-19 19:43:05 +01:00
Daniel Marjamäki bf4e850e11 Fixed #4475 (New check: struct member is assigned a value that is not read) 2018-12-17 15:40:15 +01:00
Daniel Marjamäki 858d9a18a7 Fixed #3857 (false negative: (style) Variable 'var' is assigned a value that is never used) 2018-12-17 15:16:47 +01:00
Paul Fultz II 9b973e652c Issue 8830: New check: Function argument evaluates to constant value
Add a check for function arguments that can be constant:

```cpp
extern void bar(int);
void f(int x) {
   bar((x & 0x01) >> 7); // function 'bar' is always called with a '0'-argument
}
```
2018-12-17 06:04:24 +01:00
Daniel Marjamäki c8d688607a Fixed #8901 (Unused value: const variable initialization) 2018-12-16 19:01:05 +01:00
Daniel Marjamäki 21eb1c5e22 FwdAnalysis: Fix false negatives for struct members 2018-12-16 18:32:34 +01:00
Daniel Marjamäki 46a0172480 FwdAnalysis: fix FP in loop 2018-12-16 16:43:04 +01:00
Daniel Marjamäki 97d2075007 FwdAnalysis: better handling of loops 2018-12-16 11:42:11 +01:00
Daniel Marjamäki 3af0d73f82 Unused value: Fixed false negatives for loops 2018-12-16 11:18:37 +01:00
Daniel Marjamäki 1bfe98447a FwdAnalysis: Tweak possiblyAliased 2018-12-15 11:54:00 +01:00
Daniel Marjamäki 37416010ef Unused value: Fix false positive (ast, {}) 2018-12-14 18:56:09 +01:00
Paul Fultz II be6782d386 Fix FP 8891: Incorrect return scope when using uniform initialization
This fixes the FP in:

```cpp

std::string f(const std::string& data)
{
  if (data.empty())
    return {};

  data[0];
}
```
2018-12-14 18:31:10 +01:00
Daniel Marjamäki 8464085535 UnusedVariables: Fix FP for unknown variable 2018-12-13 21:37:21 +01:00
Daniel Marjamäki 092d434f91 UnusedVar: Fix FP for array arguments 2018-12-13 21:08:18 +01:00
Daniel Marjamäki 1f27cd56c0 FwdAnalysis; Code cleanup, isNullOperand 2018-12-13 21:01:33 +01:00
Daniel Marjamäki 0b4e08cac9 Use FwdAnalysis in UnusedVar. This is still work-in-progress. Merging to master branch so it can be tested. 2018-12-13 18:52:56 +01:00
Daniel Marjamäki 452318dfa4 Remove FwdAnalysis::reads: It is not finished 2018-12-04 18:46:23 +01:00
Daniel Marjamäki c03c262058 Preparing FwdAnalysis for ValueFlow usage where we want to fetch read locations 2018-12-02 18:29:16 +01:00
Daniel Marjamäki d8fada6702 Refactoring FwdAnalysis 2018-12-02 17:42:18 +01:00
Daniel Marjamäki 6734571f06 Refactoring: Create FwdAnalysis class in astutils 2018-12-02 17:01:52 +01:00
Daniel Marjamäki ecb3c04fba astutils: Add function that visits nodes in AST tree 2018-11-23 06:53:43 +01:00
Paul Fultz II 7ef119cbfc Fix FPs in lifetime checker
This fixes several FPs in the lifetime checker. It also fixes issue [8846](https://trac.cppcheck.net/ticket/8846):

```cpp
int * f(int a[])
{
        return a;
}
```
2018-11-17 09:41:59 +01:00
Paul Fultz II 54453c5802 Fix FP when copying pointer to string (#1479) 2018-11-14 06:59:25 +01:00
Paul Fultz II 0e11bb07c8 Extend lifetime analysis to pointer usage (#1477)
* Use lifetime analysis for pointers as well

* Fix issue 1143: Pointer to local array

* Update message when using pointers

* Avoid infinite loop in tracing lifetimes
2018-11-12 10:08:17 +01:00
Paul Fultz II 68d6b96878 Diagnose invalid lifetimes (#1475)
* Add check for invalid lifetimes

* Fix FP with member variables

* Dont forward lifetime values in subfunction

* Update message to use out of scope
2018-11-11 16:43:54 +01:00
Daniel Marjamäki d5a478d5c5 astyle formatting
[ci skip]
2018-11-10 21:30:01 +01:00
Paul Fultz II 1ffcc6b730 Add initial lifetime checker (#1448)
* Inital valueflow lifetime checker

* Forward values

* Add initial tests

* Fix deplicate messages

* Fix traversing nested lambdas

* Turn test case into a todo

* Skip if returning a container

* Fix FP when using references

* Add missing header

* Fix FP from broken scopes

* Fix FP with static variable

* Add test for more FPs

* Parse lambda functions

* Check for capture by value

* Add tests for using a container and lambda together

* Fix cppcheck errors

* Add test for nextAfterAstRightmostLeaf

* Add valueflow tests

* Update error message

* Check for correct lambda token

* Improve error path reporting

* Fix hang when parsing arrays that look almlost like lambdas
2018-11-10 16:40:40 +01:00
Paul Fultz II 7373be2bfa Add a pass in valueflow for terminating conditions (#1323)
* Add valueflow for terminating conditions

* Add valueflow test

* Dont check for same expressions for now to avoid double diagnostics

* Check nesting

* Add more tests

* Ensure conditions happen in order

* Check for null

* Add error path

* Support same expression check as well

* Use early continue

* Skip checking the same token

* Avoid double condtion diagnosis

* Fix FP when in switch statements

* Fix FP when time function

* Skip conditional escapes

* Use simpleMatch

* Fix naming

* Fix typo
2018-11-07 06:49:07 +01:00
rikardfalkeborn 88008fedb1 findLambdaEndToken handle explicit type (#1458)
* findLambdaEndToken: Add tests

* Add handling of explicit return in findLambdaEndToken()

* Use AST in findLambdaEndToken()

* Fix ast when lambda is mutable
2018-10-31 12:36:08 +01:00
Kamil Dudka d206047b84 Fixed #8794 - Memory leak false positive triggered by !(x != NULL) (#1450) 2018-10-26 06:21:45 +02:00
Daniel Marjamäki 044c4b7fbb Refactoring: Use range for loop 2018-10-25 06:14:27 +02:00
Daniel Marjamäki b8e8b12783 Fixed #8797 (exprDependsOnThis handle method call in non-inline method) 2018-10-24 20:17:00 +02:00
Daniel Marjamäki 64fbffc90d astyle formatting
[ci skip]
2018-10-21 20:28:46 +02:00
Paul Fultz II 2989c44f59 Enable checking duplicate expressions across associative operators (#1445)
* Enable checking duplicate expressions across associative operators

* Remove bitshift operators and check for streamRead
2018-10-21 07:09:20 +02:00
Daniel Marjamäki e2ea8bc2b0 astyle formatting
[ci skip]
2018-10-20 09:28:28 +02:00
Paul Fultz II 40cb9cb1bc Check conditions in return statements (#1411)
* Identify return conditions in multiconditions

* Improve error messages

* Check return statements are always true or false

* Add more tests for FPs

* Fix FP when returning const like variables

* Fix FP when returning pointers or classes

* Fix FP with member variable access

* Check non-local variables

* Use simplematch

* Check for null
2018-10-18 21:01:47 +02:00
Daniel Marjamäki 58b21e3071 Refactoring: Reuse Token::expressionString() logic for finding right-most leaf in tree 2018-10-18 12:09:55 +02:00
Paul Fultz II 16c62281d0 Use followVar in checking duplicateBranch (#1423)
* Use isSameExpression for duplicate branches

* Add errorPath

* Add another test
2018-10-18 11:56:23 +02:00
Daniel Marjamäki f961324d45 Fixed #8735 (FP oppositeInnerCondition - regression) 2018-10-07 18:30:29 +02:00
Paul Fultz II e170a45230 Enable followVar for opposite expressions (#1404)
Enable followVar for opposite expressions
2018-10-04 21:17:47 +02:00
Paul Fultz II 1fe1ec09a8 Reenable follow var for logical conjunction (#1400) 2018-10-01 14:40:03 +02:00
Daniel Marjamäki 3a186b5bff astyle formatting 2018-10-01 11:53:32 +02:00
Paul Fultz II 4ed22f1ff8 Fix some FPs in mismatchingContainerExpression (#1402) 2018-09-30 14:49:58 +02:00
Paul Fultz II f65cf220ba Fix false positives in unknownEvaluationOrder when using followVar (#1391)
Fix false positives in unknownEvaluationOrder when using followVar
2018-09-28 08:38:24 +02:00
orbitcowboy 2f032d8fa7
Improved const correctness of local variables. (#1396)
* Improved const correctness of local variables.

* Running astyle [ci-skip].

* Removed duplicate 'const'-keyword.
2018-09-24 15:08:16 +02:00
Paul Fultz II 5bebeec224 Fix issue 8775: Dont follow aliased variables (#1390) 2018-09-24 06:37:47 +02:00
Daniel Marjamäki afe09f4d7a Temporarily comment out the followVariable() handling 2018-09-23 16:03:18 +02:00
orbitcowboy a812da2cf1 Formatted the code. 2018-09-17 17:16:32 +02:00
Paul Fultz II 4e7ed9ea6e More robust checking for crashes in followVar (#1375)
More robust checking for crashes in followVar
2018-09-13 09:19:15 +02:00
Paul Fultz II 90a29d986b Fix issue 8730: Dont follow variables with unknown symbols (#1374) 2018-09-12 17:30:18 +02:00
Paul Fultz II f4f3f81e8f Fix issue 8741: Require pure when following variables in isSameExpression (#1373) 2018-09-09 07:08:32 +02:00
Daniel Marjamäki df9df70ba7 astyle formatting
[ci skip]
2018-09-08 09:07:59 +02:00
Paul Fultz II cb03b9883f Fix issue 8737: FP with identical inner condition due to followVar (#1371) 2018-09-07 20:16:38 +02:00
Paul Fultz II b46e25c18e Fix issue 8730: False positive: Opposite expression on both sides of && (#1366) 2018-09-05 18:07:01 +02:00
Paul Fultz II 08f301a0de Reenable followVar (#1363)
Reenable followVar
2018-09-05 05:53:33 +02:00
Daniel Marjamäki 5afb755a3c SymbolDatabase: Fix variable matching 2018-09-04 06:39:02 +02:00
Paul Fultz II 93be440f92 Fix issue 8709: crash with switch statement with followVar (#1362)
* Fix crash with switch statement

* Update comment
2018-09-03 19:54:14 +02:00
Paul Fultz II 0e5fabf4dc Dont follow volatile variables (#1365) 2018-09-03 19:51:48 +02:00
Paul Fultz II 8353f94b93 Fix crashes in followVarExpression (#1358)
* Fix crashes in followVarExpression

* Add a regression test for issue 8717

* Skip reference declarations
2018-09-02 08:28:53 +02:00
Daniel Marjamäki 0a9d417266 add comments 2018-09-01 16:45:14 +02:00
Daniel Marjamäki 3cad3c901f Another patch to temporarily disable the followVariableExpression 2018-09-01 11:39:21 +02:00
Daniel Marjamäki a331206b44 Temporarily disable followVariableExpressions(), there are problems that should be fixed. 2018-09-01 08:32:06 +02:00
Armin Müller b9cd7368f5 Typos found by running "codespell" (#1355)
* Update astutils.cpp

* Update checksizeof.cpp
2018-08-30 19:51:39 +02:00
Daniel Marjamäki 4378327c43 astyle formatting
[ci skip]
2018-08-27 13:48:46 +02:00
Paul Fultz II fc135e1087 Check if member function modifies the variable (#1350)
* Check if member function modifies the variable

* Check for const pointer

* Add test for const condition

* Add more null checking

* Add todo assert for FN
2018-08-27 11:09:09 +02:00
Daniel Marjamäki 212af76367 astyle formatting
[ci skip]
2018-08-18 15:45:50 +02:00
Paul Fultz II fd49112196 Avoid duplicates in the error path (#1346) 2018-08-18 07:32:30 +02:00
Paul Fultz II bbf876256c Add error path to more diagnostics that rely on isSameExpression (#1342) 2018-08-17 09:25:07 +02:00
Paul Fultz II c5154286da Fix issue 8687: false positive with same expression (#1336) 2018-08-13 12:55:41 +02:00
Daniel Marjamäki c3a44ce56b astyle formatting
[ci skip]
2018-08-07 18:06:51 +02:00