Commit Graph

574 Commits

Author SHA1 Message Date
Ken-Patrick Lehrmann 4296859c1a
Remove usage of std::vector and getArguments from getTokenArgumentFunction (#3435)
getTokenArgumentFunction can be called many many times, and the vector
is not needed, but it can become quite costly.
This patch replaces getArguments with a function simply returning the
position of the token, if it is found in the arguments (thus saving the
cost of std::vector).
2021-09-01 10:51:44 +02:00
Daniel Marjamäki 6541efdb9a fix selfcheck warning 2021-08-27 22:33:03 +02:00
Daniel Marjamäki a55b0c332a Fixed #10095 (False positive: unreadVariable) 2021-08-27 17:37:42 +02:00
Paul Fultz II 865163b2ba
Add library configurations for free functions like std::size, std::empty, etc (#3410) 2021-08-23 09:03:48 +02:00
Paul Fultz II 7f358b2bed
Format with uncrustify (#3388) 2021-08-07 20:51:18 +02:00
Paul Fultz II 8b8ae55490
Fix 10129: false negative: knownConditionTrueFalse (#3382)
* Add symbolic matching

* Check for truncated values

* Dont propagate uninit values

* Update errorpath test

* Add test case for 10129

* Add test case for FP

* Remove symbolic values that are the same as the token

* Fix test messages

* Fix cppcheck issue

* Format
2021-08-04 21:07:31 +02:00
chrchr-github 28c8b00e5c
Minor: fix MSVC shadow warning (#3376) 2021-08-04 08:16:02 +02:00
Paul Fultz II 3a7ba3cd29
Add symbolic values to ValueFlow (#3367) 2021-07-30 21:29:35 +02:00
Paul Fultz II 00eb71fd49
Remove constexpr -> const simplification (#3346) 2021-07-22 07:22:26 +02:00
orbitcowboy 1be5bb8bbc Running astyle [ci skip] 2021-07-18 10:01:22 +02:00
Paul Fultz II 59a1c1a9d8
Refactor: Remove variable analyzer (#3339) 2021-07-18 07:46:31 +02:00
Paul Fultz II 3b9c399f72
Fix 10338: Hang/Crash in valueflow (#3328) 2021-07-07 08:20:32 +02:00
Paul Fultz II dd178c3ad9
Fix 10314: Possible nullPointerRedundantCheck false positive (#3298) 2021-06-19 13:59:48 +02:00
Paul Fultz II 3e78e76fe8
Fix issue 10076: ValueFlow: False positive after address of var is taken 'T t = {{{&var}}};' (#3283) 2021-06-03 07:31:46 +02:00
Paul Fultz II 5f0fe1dfe7
Fix another FP with range for macro (#3276) 2021-05-26 04:52:55 +02:00
Paul Fultz II e2d9aaf23b
Fix 10286: false positive: uninitvar (#3271)
* Fix 10286: false positive: uninitvar

* Remove header
2021-05-23 07:58:19 +02:00
Paul Fultz II 5409fa8afd
Fix disabled valueFlowUninit tests (#3262) 2021-05-21 07:28:34 +02:00
Daniel Marjamäki f1fff5e904 Fixed #10279 (ValueFlow/TokenList: function pointer dereference and call) 2021-05-20 13:54:17 +02:00
Daniel Marjamäki 216918756b Fixed #10276 (FP: (style) Variable '((uint8_t*)(uint16_t)0x1000)[0]' is assigned a value that is never used.) 2021-05-18 20:35:00 +02:00
Daniel Marjamäki 404b82fc21 unused var; fix todo test cases, redundant assignment of array in loop 2021-05-14 19:07:30 +02:00
Daniel Marjamäki d549770b5b updated extracttests.py. fix syntax errors in test cases. 2021-05-13 20:21:02 +02:00
Daniel Marjamäki 75311fba0f Fixed #10218 (FP uninitvar with input from stringstream in for loop) 2021-05-10 19:48:07 +02:00
Daniel Marjamäki 271acf8aee Refactoring; Use isUnaryOp instead of simpleMatch 2021-05-10 18:18:21 +02:00
Daniel Marjamäki 2e2d766e2b Fixed #6766 (Improve check; struct member is assigned value that is never used) 2021-05-09 20:09:10 +02:00
Daniel Marjamäki 32b28d568f Refactoring; Check structured binding in isVariableChanged() 2021-05-08 20:46:46 +02:00
Daniel Marjamäki 2f984b201a AST; Fix wrong AST for initializer list 2021-05-04 19:02:29 +02:00
Daniel Marjamäki 08184f4681 Fixed #10070 (FP warning: Logical disjunction always evaluates to true) 2021-05-04 13:47:09 +02:00
Daniel Marjamäki 4907c7e3e4 Fix compiler warning, non-static function 2021-04-22 18:40:44 +02:00
Paul Fultz II 691f681d86
Fix issue 10235: Regression: oppositeInnerCondition (#3223) 2021-04-19 09:15:03 +02:00
Oliver Stöneberg 5de58c4ddd
added clang-tidy to CI (#3218) 2021-04-18 21:51:47 +02:00
Paul Fultz II 563c9dd9cc
Fix issue 10208: FP: knownConditionTrueFalse in for loop with function that assigns by ref (#3198) 2021-04-18 21:42:27 +02:00
Paul Fultz II 5077663684
Fix issue 9979: false positive: containerOutOfBounds with conditional resize (#3136) 2021-03-30 14:02:28 +02:00
Paul Fultz II 9de976b243
Fix issue 10194: hang with followAllReferences() (#3189)
* Decrease depth faster when there is multiple returns
2021-03-30 11:22:56 +02:00
Daniel Marjamäki 42437277dc Update Copyright year 2021-03-21 20:58:32 +01:00
Daniel Marjamäki fbf63b932e astyle formatting
[ci skip]
2021-02-10 11:42:00 +01:00
Paul Fultz II 0e871c178f
Fix issue 10141: Errors with ref assignment (duplicateValueTenary and knownEmptyContainer) (#3093) 2021-02-09 15:27:46 +01:00
Paul Fultz II a81427f97f
Fix issue 10022: Logical conjunction error between two completely unrelated members (#3094) 2021-01-29 10:26:57 +01:00
IOBYTE 4e1ff86bb2
use nonneg int for varid and exprid (#3085) 2021-01-27 19:49:13 +01:00
Ken-Patrick Lehrmann 00707455be
10110: Fix FP knownConditionTrueFalse (#3053) 2021-01-25 17:23:47 +01:00
Daniel Marjamäki 772b44d11a Fixed compiler warnings 2021-01-23 18:04:28 +01:00
Paul Fultz II c860de8565
Fix issue 8143: valueFlowCondition: before and inside while (#3045) 2021-01-23 17:52:01 +01:00
Paul Fultz II a6f4fbae54
Fix issue 2741: False negative: redundant assignment of x to itself (ref = x) (#3071) 2021-01-22 11:00:57 +01:00
Oliver Stöneberg b2ed372f75
various optimizations (#3054) 2021-01-16 19:05:51 +01:00
Paul Fultz II b1c56d33ac
Fix issue 9133: Invalid iterator; vector::push_back, functions (#3008) 2021-01-11 18:47:38 +01:00
Paul Fultz II 678ee00fe9
Infer variables from conditions in valueFlowSubfunction (#3037) 2021-01-11 08:00:13 +01:00
Rikard Falkeborn 50cdb6cbfc
Astyle: sync windows and linux folders (#3016) 2021-01-06 17:13:44 +01:00
Paul Fultz II a95c931da0
Fix issue 8501: false negative: (style) Opposite expression on both sides of (#3012) 2021-01-05 12:07:27 +01:00
Daniel Marjamäki 1d6c0976e5 astyle formatting
[ci skip]
2021-01-01 08:56:17 +01:00
miltolstoy ee8d5b972a
checkunusedvar: handle array element change using pointer arithmetic (#3000) 2021-01-01 08:06:20 +01:00
Daniel Marjamäki bcf6039558 Fixed #10058 (False positive: redundant assignment, there is break) 2020-12-25 09:08:15 +01:00
Daniel Marjamäki b8f45a5c65 Replace and fix findBreakScope with findNextTokenFromBreak 2020-12-25 08:43:14 +01:00
Daniel Marjamäki 8bd783f820 Refactoring; Added findBreakScope and Scope::isLoopScope() 2020-12-24 22:58:31 +01:00
Paul Fultz II e12a733ee9
Fix issue 10053: False positive: Same value on both branches of ternary operator (#2962) 2020-12-23 13:37:28 +01:00
Paul Fultz II 904d52acac
Fix issue 10004: ValueFlow: pointer value, wrongly set known value (#2931) 2020-12-03 07:15:31 +01:00
Daniel Marjamäki 4330a43acb Fixed #9933 (FP: uninitvar when reading to struct) 2020-11-11 22:47:23 +01:00
Daniel Marjamäki 7182da5c8e astyle formatting 2020-11-11 09:17:54 +01:00
Paul Fultz II bd7e915c20
Add generic reverse valueflow (#2878) 2020-11-10 16:00:55 +01:00
Ken-Patrick Lehrmann 79bdd64689
Fix false positive memoryleak (#2882)
```
int *f() {
  int *p = static_cast<int *>(realloc(nullptr, 10));
  if (!!(!p)) {
    return nullptr;
  }
  return p;
}

```
would give
```
memleak2.cpp:4:5: error: Memory leak: p [memleak]
    return nullptr;
    ^
```
Because of the additional `!!̀ .
2020-11-10 15:59:51 +01:00
Armin Müller 08cef9e815
Typos found by running "codespell" (#2846) 2020-10-15 19:24:13 +02:00
Paul Fultz II 7b6d3f8061
Fix issue 9907: False positive: knownEmptyContainer after function call with :: (#2814) 2020-09-20 22:37:28 +02:00
Daniel Marjamäki c563944fdd astyle formatting 2020-09-20 20:14:30 +02:00
Paul Fultz II 857722f859
Fix issue 9711: FP knownConditionTrueFalse for variable modified via pointer (#2813) 2020-09-20 14:27:09 +02:00
Paul Fultz II c2e8051196
Fix issue 9904: False positive: duplicateCondition when modifying variable in lambda (#2811) 2020-09-18 07:44:26 +02:00
Paul Fultz II 782684a7cc
Fix issue 9530: False positive: Reference to temporary returned when using initializer lists (#2796) 2020-09-17 08:33:16 +02:00
Paul Fultz II 94850fec3f
Fix issue 9897: False positive: nullPointerRedundantCheck (#2805) 2020-09-14 09:17:29 +02:00
Paul c568e8ea78 Format 2020-09-10 22:59:19 -05:00
Paul 71bc79ac28 Fix issue 9889: False positive: Using reference to dangling temporary with function object 2020-09-10 22:58:17 -05:00
Daniel Marjamäki f052d32e31 astyle formatting 2020-09-10 08:02:45 +02:00
Paul 5033fb4418 Fix issue 9869: False positive: knownEmptyContainer when passed to constructor 2020-09-09 22:23:42 -05:00
Paul Fultz II bfe53fce04
Fix issue 9878: false positive: multiCondition (#2787) 2020-09-09 15:39:36 +02:00
Paul 8d7088aa24 Fix issue 9835: False negative: Return reference to temporary with const reference 2020-09-08 18:30:45 -05:00
Daniel Marjamäki 0c6dc84cbb astyle formatting
[ci skip]
2020-09-07 10:56:02 +02:00
Paul Fultz II a4f43fc2ad
Fix issue 8234: false negative: (warning) Opposite inner 'if' condition leads to a dead code block. (#2781) 2020-09-07 07:53:41 +02:00
Paul Fultz II cc2bc74084
Track lifetime for lambdas with explicit capture (#2776) 2020-09-05 07:56:01 +02:00
Paul Fultz II 32df807b22
Fix issue 9783: wrong lifetime analysis temporary assigned to object (#2711) 2020-09-02 07:13:15 +02:00
Paul Fultz II 3e99bff764
Same expression when comparing with zero (#2762) 2020-08-31 08:48:48 +02:00
Paul Fultz II ac846b96d1
New check: Iterating a known empty container (#2740) 2020-08-22 09:16:26 +02:00
Paul Fultz II becdf20310
Fix crash in isSameExpression (#2748) 2020-08-22 07:05:21 +02:00
Daniel Marjamäki 5372e93964 Fixed #9825 (redundantAssignment: Do not warn about volatile data) 2020-08-06 22:18:25 +02:00
Daniel Marjamäki 25ad22c6af astyle formatting
[ci skip]
2020-07-23 10:09:06 +02:00
Daniel Marjamäki df99d8aa0a
Merge pull request #2719 from pfultz2/fp-unreachable-alias
Fix issue 9807: False positive: ValueFlow in unreachable code, || lhs is true
2020-07-23 09:52:54 +02:00
Daniel Marjamäki f4a6f8ad07
Merge pull request #2720 from pfultz2/crash-type-scope
Fix crash when typeScope is missing
2020-07-23 09:31:59 +02:00
Paul 3480aba35b Fix crash when typeScope is missing 2020-07-21 14:09:46 -05:00
Paul dbb410cdae Merge branch 'main' into condition-in-expr 2020-07-21 13:28:59 -05:00
Paul e2a81a382f Track reading aliases during valueflow forward 2020-07-19 23:25:35 -05:00
Daniel Marjamäki e0be224f4e
Merge pull request #2709 from pfultz2/fp-duplicate-cond-this
Fix FP of duplicateCondition when modifying the this variable
2020-07-13 21:14:58 +02:00
Paul 519f2a537a Format 2020-07-13 13:55:45 -05:00
Paul 450bdfedf3 Fix FP of duplicateCondition when modifying the this variable 2020-07-13 12:40:01 -05:00
Daniel Marjamäki 686a6c7862 Fixed compiler warning 2020-07-03 17:54:55 +02:00
Daniel Marjamäki f34ff9325a Fixed testrunner 2020-06-29 21:53:14 +02:00
Daniel Marjamäki a0770f05e1 Reuse 'extractForLoopValues' in ValueFlow 2020-06-29 21:01:43 +02:00
Paul d5b6d49d96 Fix issue 9578: false negative: (style) Condition '...' is always false 2020-06-28 15:28:08 -05:00
Daniel Marjamäki c42c751d61 ExprEngine; Fixed testing 2020-06-28 13:41:27 +02:00
Daniel Marjamäki 262f44e76a astyle formatting
[ci skip]
2020-06-26 12:56:56 +02:00
Paul 1bef6ed268 Another check for null 2020-06-25 19:12:01 -05:00
Paul 263a0364cc Avoid null pointer dereference 2020-06-25 17:23:42 -05:00
Paul 720d882b69 Remove known arg 2020-06-25 17:20:20 -05:00
Paul 60c42b17a6 Fix known conditions 2020-06-25 15:46:30 -05:00
Paul 1676ad5f45 Fix issue 9710 and 9767: Use AST to get function from argument 2020-06-25 12:54:46 -05:00
Ken-Patrick Lehrmann c297ed8204 Better handle noreturn or throwing functions in valueflow
Teaching cppcheck about `BOOST_THROW_EXCEPTION` and
`boost::throw_exception`, and using noreturn information from libraries
in value flow.

This fixes false positive nullPointerRedundantCheck with the following
code:
```
void throwexception(int * buf)
{
    if (!buf)
        boost::throw_exception(std::bad_alloc());
    *buf = 0;
}
```
2020-06-17 19:06:14 +02:00
Paul Fultz II 03b41ac987
Fix issue 9718: False positive: Parameter can be declared const when written to via stream extraction operator (#2677) 2020-06-13 09:20:40 +02:00
Daniel Marjamäki 120c572252 Fixed #9750 (Chained stream operation gives uninitvar error) 2020-06-06 15:24:01 +02:00
Paul Fultz II eb4754b7d9
Fix issue 9587: False positive: parameter can be declared with const (#2667) 2020-05-31 10:10:10 +02:00
Daniel Marjamäki 5c3474ec5c Renamed hasReturnFunction to hasNoreturFunction 2020-05-23 07:30:05 +02:00
Paul Fultz II 8301fa8244
Fix issue 8144: valueFlowBeforeCondition: struct (#2645) 2020-05-21 08:47:48 +02:00
PKEuS 793ed68029 Refactorization: Moved code from header to source
- from utils.h to new utils.cpp
- from token.h to token.cpp
- from valueflow.h to valueflow.cpp
- from errorlogger.h to errorlogger.cpp
2020-05-19 08:35:12 +02:00
Daniel Marjamäki 08ddd84780 Update copyright year 2020-05-10 11:16:32 +02:00
Daniel Marjamäki 3e0218299b Revert "Update copyright year"
This reverts commit 6eec6c4bd5.
2020-05-10 11:13:05 +02:00
Daniel Marjamäki 6eec6c4bd5 Update copyright year 2020-05-10 11:11:34 +02:00
Daniel Marjamäki 54978847c5 Fixed #8916 (FP: followVar does not seem to handle non-const method call properly) 2020-04-18 19:54:55 +02:00
Paul Fultz II 02ae71917a
Fix issue 9536: False positive: Reference to temporary returned when using operator() (#2582)
* Fix issue 9536: False positive: Reference to temporary returned when using operator()

* Add more test cases
2020-04-01 22:35:41 +02:00
Paul Fultz II 921887a281
Use valueFlowGeneric for valueFlowForwardExpression (#2537) 2020-02-16 16:02:22 +01:00
Daniel Marjamäki 67b495fc50 Fixed #9280 (False positive: unreadVariable on assignment to fixed address volatile struct member) 2020-02-15 17:31:47 +01:00
Daniel Marjamäki 8c0ad6a1b9 Fixed #9555 (False positive (unreadVariable): struct is assigned a value that is never used) 2020-02-15 16:43:06 +01:00
Daniel Marjamäki 5f4a900f88 astyle formatting
[ci skip]
2020-02-13 17:04:05 +01:00
Paul Fultz II 7368a54629
Add generic valueflow forward analysis (#2511) 2020-02-13 16:27:06 +01:00
Paul Fultz II d858bfc338
Fix issue 9599: False positive: Using object that points to local variable that may be invalid (#2530)
* Fix issue 9599: False positive: Using object that points to local variable that may be invalid

* Improve tests

* Skip else
2020-02-11 11:45:10 +01:00
Paul Fultz II 8fa7dd0fe0
Fix issue 9595: False positive: Using pointer to temporary doesn't account for const ref extended temporary lifetimes (#2525) 2020-02-10 18:01:11 +01:00
Rikard Falkeborn b1c6f2946a
Fix redundant FP assignment with unsigned zero (#2521)
* Refactor isNullOperand out of FwdAnalysis

* Improve isNullOperand

* Fix redundantAssignment FP with unsigned zero

* isNullValue check number

* Enhance isNullOperand to handle c++ casts

Also handle cast of NULL.
2020-02-09 11:16:08 +01:00
Rikard Falkeborn 488bc9997c
Improve isSameExpression for same valued literals with followvar (#2519)
It allows (for example) cppcheck to detect that the lhs and the rhs are
the same in the following example:

	double g()
	{
        	double a = 1e1
	        return a & 10.0;
	}
2020-02-03 09:35:24 +01:00
Daniel Marjamäki 0e0d88f65e Refactoring; Avoid template<> 2020-02-01 08:39:58 +01:00
Rikard Falkeborn ff9c04dc28
Improve isSameExpression for literals (#2514)
Improve isSameExpression() for literals with same value but different
representation, for example  the following different ways of
representing 9 as double: 9.0, 0.9e1 and 0x1.2p3.

With this change, cppcheck can (for example) correctly detect that the
else if statements are always false in the following example:

	void f(double x) {
		if (x < 9.0) {}
		else if (x < 0x1.2p3) {}
		else if (x < 0.9e1) {}
	}
2020-02-01 07:22:41 +01:00
Dmitry-Me 01e0a61ebe Break loops earlier 2020-01-16 18:37:10 +03:00
Paul Fultz II e1a97c524d Fix issue 9554: False positive: The address of local variable 'x' is accessed at non-zero index. (#2470)
* Fix issue 9554: False positive: The address of local variable 'x' is accessed at non-zero index.

* Format

* Remove unnecesary condition check
2020-01-04 11:39:52 +01:00
orbitcowboy 1e0f8ae9b4 Running astyle [ci skip]. 2020-01-04 00:06:27 +01:00
Paul Fultz II dd05839a7e Fix false positives in knownConditionTrueFalse when using expressions with const variables (#2469) 2020-01-03 19:35:28 +01:00
Daniel Marjamäki 235ef0a01e Fixed #9420 (False positive - redundantInitialization) 2019-12-18 19:39:23 +01:00
Daniel Marjamäki 49d6bfbd7b Fixed crash in harfbuzz found by daca@home 2019-11-12 18:22:11 +01:00
Paul Fultz II c75bbbe253 Fix issue 9404: False positive: Either the condition 'if(x)' is redundant or there is possible null pointer dereference: a->x (#2322)
* Fix issue 9404: False positive: Either the condition 'if(x)' is redundant or there is possible null pointer dereference: a->x

* Use simpleMatch

* Add a test case for the FP

* Check if expression is changed

* Check for no return scope

* Use simpleMatch
2019-11-08 08:11:41 +01:00
Paul Fultz II d1f225b8ee Fix issue 9201: FP: returnDanglingLifetime on pointer to variable of static struct (#2303)
* Fix issue 9201: FP: returnDanglingLifetime on pointer to variable of static struct

* Fix capture of non-local variables in lambdas
2019-11-05 07:10:32 +01:00
Paul Fultz II c38bbb75e4 Fix issue 9448: Check for temporaries from library function calls (#2312) 2019-11-03 22:02:10 +01:00
Daniel Marjamäki 28182084f2 Fix crash when there are too many nested scopes. Bail out after 1000 nested scopes. The crash was in gcc and was seen with daca@home. 2019-11-02 18:23:10 +01:00
Paul Fultz II 694d147097 Refactor ProgramMemory and PathAnalysis (#2311)
* Traverse conditions for container size

* Move program memory to seperate file

* Revert "Traverse conditions for container size"

This reverts commit 914783769f.

* Move pathanalysis to seperate files
2019-10-30 17:57:46 +01:00
Paul Fultz II 1ef85f9229 Fix issue 9432 and 9433: False positive: Reference to temporary returned (#2302) 2019-10-29 19:12:58 +01:00
Daniel Marjamäki 68ea60d207 Try to make Travis happy 2019-10-20 07:44:56 +02:00
Daniel Marjamäki 4943771e41 Fix #9262 (False positive on variable assignment when putting enum in namespace) 2019-10-19 21:41:50 +02:00
Daniel Marjamäki 99ef64459b Fixed #9344 (FP redundantAssignment related to increasing index variable for vector) 2019-10-17 18:31:05 +02:00
Paul Fultz II bf5c90a2be Fix issue 9409: FP returnTempReference (#2266)
* Fix issue 9409: FP returnTempReference

* Format
2019-10-13 19:12:46 +02:00
Paul Fultz II a5c6ae1e08 Fix issue 9400: FP returnTempReference - triggered by cast (#2259) 2019-10-10 06:52:11 +02:00
Paul Fultz II 4eb4762d95 Extend lifetime checking to temporaries (#2242)
* Use lifetimes to check for returning reference to temporaries

* Check for dangling temporaries

* Check for unknown types for returining by reference

* Remove old returnTemporary check

* Format

* Check for deref op

* Ternary operator return an lvalue reference

* Warn when returning temporaries from member functions

* Improve handling of pointer to function

* Extend lifetimes of const references
2019-10-08 09:28:39 +02:00
Paul Fultz II 997803869d Forward values after assignment in valueFlowReverse (#2226)
* Forward values after assignment in valueFlowReverse

* Rename variables

* Format
2019-10-03 09:58:57 +02:00
orbitcowboy f05e21efa8 Formatted the code, there are no functional changes [ci skip]. 2019-10-01 08:39:08 +02:00
Paul Fultz II 166bd2bafc Fix issue 2153: valueFlowAfterCondition: struct member (#2228)
* Fix issue 2153: valueFlowAfterCondition: struct member

* Fix null pointer dereference

* Formatting

* Check for another null pointer

* Initialize variables

* Remove redundant condition

* Format

* Add missing initialization to copy constructor

* Format
2019-09-30 21:04:43 +02:00
SimonCornell1 6453a85363 Fix the following compilation error on VC142 : (#2224)
Error C2039 'inserter': is not a member of 'std' cppcheck C:\Users\simon\__Si\git_projects\cppcheck\lib\astutils.cpp 1229
2019-09-28 06:24:32 +02:00
Daniel Marjamäki b55c587ab2 astyle formatting
[ci skip]
2019-09-26 10:32:49 +02:00
Paul Fultz II 597d0fa35b Support expression in valueFlowAfterCondition (#2219)
* Add valueFlowForwardExpression function to forward values of an expression

* Use token for expression

* Fix name in bailout message

* Handle expressions

* Add more tests for more expressions

* Add more tests

* Solve the expression if possible

* Formatting
2019-09-26 10:32:25 +02:00
Paul Fultz II 068b0b246c Fix issue 9352: FP constParameter and constVariable for auto& in combination with ternary ?: operator (#2173) 2019-09-13 08:33:30 +02:00
Paul Fultz II ba037837c9 Track lifetime across multiple returns
This will now warn when doing something like this:

```cpp
template <class T, class K, class V>
const V& get_default(const T& t, const K& k, const V& v) {
    auto it = t.find(k);
    if (it == t.end()) return v;
    return it->second;
}
const int& bar(const std::unordered_map<int, int>& m, int k) {
    auto x = 0;
    return get_default(m, k, x);
}
```

The lifetime warning is considered inconclusive in this case.

I also updated valueflow to no tinject inconclusive values unless `--inconclusive` flag is passed. This creates some false negatives because library functions are not configured to not modify their input parameters, and there are some checks that do not check if the value is inconclusive or not.
2019-09-11 19:25:09 +02:00
Daniel Marjamäki bee30b0ca2 astyle formatting
[ci skip]
2019-09-10 19:42:17 +02:00
Paul Fultz II dc0b3527ad Fix issue 9311: False positive duplicateCondition "same if condition" with pointer inside array of struct (#2166)
* Check for typeOf through an array

* Handle array constructors

* Format

* Fix compile error on gcc 4.8
2019-09-10 19:41:35 +02:00
Paul Fultz II 2595b82634 Fix issue 9348: FP uninitvar for pointer passed to memcpy (#2167) 2019-09-10 19:40:08 +02:00
Paul Fultz II 27ebff7ae4 Add deeper analysis of when a function changes a containers size (#2149)
* Add deeper analysis of when a function changes a containers size

* Fix issues

* Track addressOf
2019-09-06 21:18:45 +02:00
Paul Fultz II 70cad280ea Fix issue 9319: FP knownConditionTrueFalse related to aggregate initialization of struct (#2147) 2019-09-05 19:36:45 +02:00
Paul Fultz II e657cf4073 Fix issue 9320: False positive knownConditionTrueFalse related to truncation (#2144) 2019-09-05 15:15:58 +02:00
Paul Fultz II 0b9e823fc8 Fix issue 9305: False positive uninitvar - struct initialized via function (#2123) 2019-08-30 18:32:45 +02:00
Paul Fultz II 03fe6795bf Fix issue 9302: FP uninitvar - struct accessed via pointer (#2121) 2019-08-29 08:38:50 +02:00
Daniel Marjamäki 324e5e581b Redundant assignments: Fix false positive when reassignment expression contains assembler 2019-08-24 20:15:52 +02:00
Paul Fultz II c0a8d628b9 Fix issue 6010: Uninitialized inner struct (#2098)
* Fix issue 6010: Uninitialized inner struct

* Show to root variable that is unitialized

* Warn on pointer dereferences
2019-08-23 06:23:20 +02:00
Paul Fultz II 3aef0c9bd3 Fix issue 8715: regression uninitvar not detected (#2092) 2019-08-16 07:48:54 +02:00
Paul Fultz II ef714225bb Use library to track container lifetimes 2019-08-15 21:14:54 +02:00
Paul Fultz II af214e8212 Fix issue 8825: ValueFlow: uninitialized struct member (#2087)
* Pass uninit value across pointers

* Add more testing
2019-08-15 10:44:55 +02:00
Armin Müller 785cc69644 Typos found by running "codespell" (#2072) 2019-08-08 21:05:28 +02:00
Paul Fultz II bd02ca5ccb Fix issue 9207: Not detected 'always true' and unreachable code 2019-08-08 07:46:47 +02:00
Daniel Marjamäki 3ad9d6a1ad Make the code a bit more robust 2019-07-29 20:57:16 +02:00
Daniel Marjamäki 607b3daca8 ValueFlow: Improve bailout for structs etc in loops 2019-07-29 15:51:48 +02:00
Paul Fultz II ee28a45db4 Fix issue 9235: new crash in astutils isVariableChanged from endless recursion (#2040) 2019-07-26 07:03:21 +02:00
Daniel Marjamäki bbcffce529 Fixed #9062 (False positive "condition is always true") 2019-07-24 19:16:35 +02:00
Daniel Marjamäki 6c82685fbf Fixed compiler warnings 2019-07-24 11:01:38 +02:00
Paul Fultz II bb52a63c4e Add check for const variables
When a local reference is declared, this will check if that local reference can be declared as `const`.
2019-07-24 09:59:01 +02:00
Paul Fultz II a08a9c1349 Switch to use lifetime analysis for iterators and pointers to invalid containers
This will diagnose more issues such as:

```cpp
void f(std::vector<int> &v) {
    auto v0 = v.begin();
    v.push_back(123);
    std::cout << *v0 << std::endl;
}
```
2019-07-18 10:56:44 +02:00
Daniel Marjamäki 9973db3a71 Modernize: Use enum class for Library::Action and Library::Yield 2019-07-17 11:39:30 +02:00
Daniel Marjamäki 0014fe880e Mark members and arguments with 'nonneg' 2019-07-14 12:22:33 +02:00
Daniel Marjamäki 5d853bcb58 Avoid 'unsigned' in astutils 2019-07-13 16:13:21 +02:00
Paul Fultz II b0d10273ed Fix issue 3695: Handle class pointers
This switches to use lifetime analysis to check for assigning to non-local variables:

```cpp
class test
{
public:

  void f()
  {
    int x;
    this->ptr = &x;
  }

protected:
  int *ptr;
};
```
2019-07-07 10:16:19 +02:00
Ken-Patrick d6f6e68fa2 Fix false positive 9167 (#1904)
Skip returns from local class/struct definition in FwdAnalysis.
2019-07-03 08:17:06 +02:00
Daniel Marjamäki beea7fa8d2 Token::index(): Created Token member that indicates the Token position in the token list. It can be used to quickly check if tok1 precedes tok2. 2019-06-21 22:16:23 +02:00
Ken-Patrick 44d6066c6f FP on assignment through pointer (#1887)
* Fix FP when assigning through pointers

* Add test case for false positive

cppcheck would faulty warn:
"Condition '*b>0' is always true"
2019-06-17 21:25:15 +02:00
Daniel Marjamäki 5d10b57b04 Fixed #8997 (False positive redundantAssignment when pointer is updated with +=) 2019-05-31 12:24:31 +02:00
orbitcowboy 79ab9cf84d Running astyle [ci skip]. 2019-05-31 10:14:52 +02:00
Paul Fultz II 108cdaa485 Fix FP with unreadVariable (#1859) 2019-05-31 08:06:36 +02:00
Rikard Falkeborn ada881ccdf Fix #9130 (FP memory leak with NULL pointer cast) (#1839) 2019-05-18 06:22:25 +02:00
Daniel Marjamäki fd4e371091 Refactoring: Use stl algorithm 2019-04-28 07:40:00 +02:00
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