Commit Graph

320 Commits

Author SHA1 Message Date
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