Commit Graph

460 Commits

Author SHA1 Message Date
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
Daniel Marjamäki 049f6475ee astyle formatting
[ci skip]
2019-09-20 15:07:27 +02:00
Paul Fultz II ad8abdb0c3 Add impossible values to ValueFlow (#2186)
* Add impossible category

* Replace values

* Try to adjust known values

* Add ! for impossible values

* Add impossible with possible values

* Remove contradictions

* Add values when the branch is not dead

* Only copy possible values

* Dont bail on while loops

* Load std lib in valueflow

* Check for function calls

* Fix stl errors

* Fix incorrect impossible check

* Fix heap-after-use error

* Remove impossible values when they are lowered

* Show the bound and remove overlaps

* Infer conditions

* Dont push pointer values through dynamic_cast

* Add test for dynamic_cast issue

* Add shifttoomanybits test

* Add test for div by zero

* Add a test for issue 9315

* Dont make impossible value inconclusive

* Fix FP with shift operator

* Improve handleKnownValuesInLoop for impossible values

* Fix cppcheck warning

* Fix impossible values for ctu

* Bailout for streams

* Check equality conditions

* Fix overflows

* Add regression test for 9332

* Remove duplicate conditions

* Skip impossible values for invalid value

* Check for null

* Rename bound to range

* Formatting
2019-09-20 15:06:37 +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 3aef0c9bd3 Fix issue 8715: regression uninitvar not detected (#2092) 2019-08-16 07:48:54 +02:00
Paul Fultz II 68e8253920 Fix issue 8313 and 7326: Track values of pointer aliases in valueflow 2019-08-12 12:58:53 +02:00
Paul Fultz II 9aa97cbb95 Fix issue 8296: ValueFlow: value not set in conditional scope in subfunction (#2071)
* Fix issue 8296: ValueFlow: value not set in conditional scope in subfunction

* Refactor condition checkingg

* Make test case TODO
2019-08-11 15:39:37 +02:00
Paul Fultz II bd02ca5ccb Fix issue 9207: Not detected 'always true' and unreachable code 2019-08-08 07:46:47 +02:00
Paul Fultz II aaeec462e6 Re-enable valueFlowSubFunction (#2063)
* Re-enable valueFlowSubFunction

* Formatting

* Skip ternary operators in subfunctions

* Fix test with iostreams

* Fix FP with multiple parameters
2019-08-05 16:26:32 +02:00
Paul Fultz II ffdd2dc793 Fix issue 8924: Re-enable valueFlowTerminatingCondition 2019-08-05 07:18:06 +02:00
Daniel Marjamäki ce53931d00 Fixed #9251 (False positive: unininitialized variable (multi variables)) 2019-08-03 21:12:34 +02:00
Daniel Marjamäki 607b3daca8 ValueFlow: Improve bailout for structs etc in loops 2019-07-29 15:51:48 +02:00
Daniel Marjamäki e8ec6e6f11 Fixed #8349 (Noisy nullPointerRedundantCheck) 2019-07-27 20:03:06 +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 2da75d5af4 Split up Cppcheck attribute. low and high values can be specified separately and they can be used for variables also. 2019-07-24 15:08:26 +02:00
Daniel Marjamäki cab9f61b79 safe checks: Handle float parameters 2019-07-24 12:09:13 +02:00
Daniel Marjamäki 10be2a1941 Safe checks: container parameters 2019-07-24 11:39:35 +02:00
Daniel Marjamäki 8959c5a9d0 Rename valueFlowAllFunctionParameterValues => valueFlowSafeFunctionParameterValues 2019-07-24 10:57:35 +02:00
Daniel Marjamäki c8bc88e7e2 Fix compiler error 2019-07-23 13:14:08 +02:00
Daniel Marjamäki f9bd589abb testrunner: Make a few tests more 'proper' 2019-07-13 07:29:23 +02:00
Daniel Marjamäki 8ad3e43f92 Add handling of a simple C++ contract 2019-07-12 16:05:35 +02:00
Daniel Marjamäki 68cc7516a1 Annotations: Add annotation __cppcheck_in_range__(low,high) 2019-07-12 11:09:54 +02:00
Daniel Marjamäki 783f7f1648 Rename safeValues to unknownValues 2019-07-11 16:05:48 +02:00
Daniel Marjamäki 05d35b063d Function return: Extra check of safe function return values 2019-07-10 20:00:21 +02:00
Daniel Marjamäki c9906125de Safe functions: Check more possible function argument values 2019-07-10 16:59:05 +02:00
Daniel Marjamäki 9f548efbd3 Refactoring: enum class 2019-07-10 15:27:07 +02:00
Rikard Falkeborn 839fcddd8a Fix #6115 (Add support to realloc to cfg files) (#1953)
* Allow to configure realloc like functions

* memleakonrealloc: Bring back tests.

The old memleak checker was removed, and the tests for it was removed in
commit 9765a2dfab. This also removed the
tests for memleakOnRealloc. Bring back those tests, somewhat modified
since the checker no longer checks for memory leaks.

* Add realloc to mem leak check

* Add tests of realloc buffer size

* Configure realloc functions

* Add test of freopen

* Allow to configure which element is realloc argument

* Fix wrong close in test

cppcheck now warns for this

* Update manual

* Update docs

* Rename alloc/dalloc/realloc functions

Naming the member function realloc caused problems on appveyor. Rename
the alloc and dealloc functions as well for consistency.

* Change comparisson order

* Remove variable and use function call directly

* Create temporary variable to simplify

* Throw mismatchError on mismatching allocation/reallocation

* Refactor to separate function

* Fix potential nullptr dereference

As pointed out by cppcheck.
2019-07-05 12:44:52 +02:00
Daniel Marjamäki 56df6169fb Fixed #8356 (ValueFlow: variable is not changed in for loop) 2019-06-30 17:50:35 +02:00
Rikard Falkeborn d1d622b74c Valueflow: support global static const variables (#1861) 2019-06-29 14:33:55 +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
Rikard Falkeborn b1c8d81bcc Refactoring; Use range for loop (#1900) 2019-06-17 13:17:45 +02:00
Rikard Falkeborn d909ac8565 Bugfix buffer size for strdup like functions (#1893)
strdup() allocates the string length plus one for a terminating null
character. Add one to compensate for this.

Fixes false positive buffer out of bounds on code like this:

	void f() {
		const char *a = "abcd";
		char * b = strdup(a);
		printf("%c", b[4]); // prints the terminating null character
		free(b);
	}

Also, add a testcase for valueFlowDynamicBufferSize() and add tests for
strdup(), malloc() and calloc().
2019-06-16 16:02:27 +02:00
orbitcowboy 55df395a4e Running astyle [ci skip] 2019-05-30 14:41:14 +02:00
Ken-Patrick 3cdc236e10 Fix false positive with several ! (not) operators (#1856)
With the following code
  int f(int x, int y) {
      if (!!(x != 0)) {
        return y/x;
  }

cppcheck would wrongly warn that there might be a division by zero in
"return y/x;".
2019-05-29 09:45:15 +02:00
Paul Fultz II 4e94c64da8 Fix issue 9099 and 9102: Incorrect valueflow for global variables (#1832) 2019-05-14 08:58:27 +02:00
Daniel Marjamäki 6c3c090403 Fixed #6317 (wrong simplification: int i = 1.5; return i; get simplified to: return 1.5;) 2019-05-01 17:05:16 +02:00
Rikard Falkeborn 1cc5f3abe7 Set wchar_t type (#1807)
This is necessary for valueflow to know the size, for example when
calculating sizeof(wchar_t).
2019-05-01 16:34:28 +02:00
Daniel Marjamäki 66064fb2bb Disable valueFlowGlobalConstVar until #9099 is fixed 2019-04-30 20:51:59 +02:00
Gary Leutheuser bca2dfb3f4 Implement #7597 - valueflow: global constant (#1802)
* Implement const global value flow

* Tabs to spaces
2019-04-21 06:54:32 +02:00
Daniel Marjamäki 703e0a0153 Appveyor: Try to #include proper header for std::uint16_t 2019-03-13 21:44:26 +01:00
Daniel Marjamäki 1ca6d9c847 Appveyor: Fix missing #include for uint16_t 2019-03-13 19:01:54 +01:00
Daniel Marjamäki b984897526 ValueFlow: Fix sizeof for array of library type 2019-03-13 18:31:41 +01:00
Daniel Marjamäki 11e32ff445 ValueFlow: Handle compound assignments in execute() 2019-03-12 18:53:58 +01:00
Daniel Marjamäki 7b17b33a49 ValueFlow: fix handling of sizeof(*p) 2019-03-11 20:32:24 +01:00
orbitcowboy a5ac9fcac5 Running astyle [ci skip]. 2019-03-01 15:05:53 +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 5563fef7bb Fixed #9008 (new crash in clang test suite) 2019-02-28 20:34:07 +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 507c7a4388 Improvement to lifetime tracking of addressof and derefencing
This will now warn for cases like this:

```cpp
auto& f() {
    std::vector<int> x;
    return x[0];
}
```

It also improves the handling of address of operator, so it can now warn across some function calls, like this:

```cpp
int& f(int& a) {
    return a;
}
int* hello() {
    int x = 0;
    return &f(x);
}
```
2019-02-22 06:38:56 +01:00
Daniel Marjamäki bd7790fd8c Update copyright year 2019-02-09 07:24:06 +01:00
Daniel Marjamäki ae001d4336 Fixed #8957 (Tokenizer::setVarId: varid not set when lambda function is used) 2019-02-03 08:57:04 +01:00
amai2012 738fef6c27 Run astyle 2019-01-12 21:51:39 +01:00
Daniel Marjamäki 5276fd68b2 Remove unused test functions
[ci skip]
2019-01-12 18:32:18 +01:00
Daniel Marjamäki 8dd641b8be Use OVERRIDE in test 2019-01-12 15:45:25 +01:00
Daniel Marjamäki 5636497c0b Fixed #8863 (false positive: (warning) Accessing an item in container 's'. Either the condition 's.empty()' is redundant or 's' can be empty.) 2019-01-06 12:21:55 +01:00
Paul Fultz II bba6dfb8b2 Fix issue 4744: ValueFlow: known integer result
This fixes valueflow to have a value for `||` operator here:

```cpp
bool f()
{
	bool a = (4 == 3); // <-- 0
	bool b = (3 == 3); // <-- 1
	return a || b; // <-- 1
}
```
2019-01-03 07:05:31 +01:00
Daniel Marjamäki 39a96a5a16 ValueFlow: Temporarily comment out valueFlowTerminatingCondition 2019-01-02 19:42:08 +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 be7afac875 ValueFlow: remove handling of == for complex expressions it did not work properly 2019-01-01 17:23:46 +01:00
rikardfalkeborn 13ffefc8b8 Valueflow: Fix right shift with more than 31 bits (#1553)
When comparing if the shift is large enough to make the result zero, use
an unsigned long long to make sure the result fits. Also, a check that
avoids setting the value if the shift is equal to or larger than the
number of bits in the operand (this is undefined behaviour). Finally,
add a check to make sure the calculated value is not too large to store.

Add test cases to cover this.

This was detected by an MSVC warning.

valueflow.cpp(1350): warning C4334: '<<' : result of 32-bit shift implicitly
                     converted to 64 bits (was 64-bit shift intended?)
2019-01-01 14:15:50 +01:00
Daniel Marjamäki 4918a18bfb ValueFlow: Value of expression after condition 2018-12-31 17:37:38 +01:00
Daniel Marjamäki 141ce7cd63 ValueFlow: Use FwdAnalysisAllPaths in ValueFlow to track complex expressions 2018-12-31 17:05:46 +01:00
Daniel Marjamäki a40fe3c67f Fixed #7872 (ValueFlow: static_cast) 2018-12-27 21:33:01 +01:00
Daniel Marjamäki bc34f0239d Disable the subfunction value flow analysis. It does not work well and needs to be rewritten. There are false positives. 2018-12-18 14:36:49 +01:00
Paul Fultz II 3262a3bebe Add isSameExpression to valueflow analysis
Check for same expressions in valueflow analysis.
2018-12-16 07:35:27 +01:00
Daniel Marjamäki 88c61e8d1e ValueFlow: Avoid FP when lambda is used 2018-12-04 18:54:26 +01:00
Daniel Marjamäki f5a94ed300 ValueFlow: Fix FP when lambda is called 2018-12-04 18:46:00 +01:00
Igor 61878c5e11 Add null pointer check to fix SEGFAULT (#1499)
* Add null pointer check to fix segfault

* Add first test case to reproduce problem
2018-12-02 14:36:01 +01:00
Daniel Marjamäki 2887ee10c0 Fixed #8800 (Possible variable assignment ignored in boolean expression) 2018-11-26 14:00:03 +01:00
Paul Fultz II a3921ea861 Refactor valueFlowAfterCondition
So this unifies the `valueFlowAfterCondition` so it re-uses more code between checking for integers and container sizes. This should make valueFlowContainer more robust.

It also extends valueflow to support container comparisons such as `if (v.size() < 3)` or `if (v.size() > 3)` using the same mechanism that is used for integers.
2018-11-24 10:07:12 +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
Daniel Marjamäki 00340efc57 ValueFlow: Refactoring valueFlowCallFunction 2018-11-06 06:38:26 +01:00
Daniel Marjamäki ca19894a04 ValueFlow: In evaluate handle correlated values 2018-11-05 18:07:35 +01:00
Daniel Marjamäki 8a54420274 ValueFlow: Try to handle multiple argument values in <returnValue> evaluation 2018-11-04 20:52:12 +01:00
Daniel Marjamäki 6878de2c5e ValueFlow: getExpressionRange, valueFlowRightShift 2018-11-03 23:25:46 +01:00
Daniel Marjamäki 5de683ec49 Container size: Fix false positive 2018-11-02 20:10:40 +01:00
Daniel Marjamäki 16cc20a232 ValueFlow: Better handling of container size values 2018-11-02 18:28:32 +01:00
Daniel Marjamäki c03d32b429 ValueFlow: Improve containerSize handling of string like classes 2018-10-28 19:14:00 +01:00
Daniel Marjamäki 06ede9c239 ValueFlow: handle x+=y for containers 2018-10-27 18:38:04 +02:00
Daniel Marjamäki d7de46f50e ValueFlow: Fix false positive for container size 2018-09-08 10:43:08 +02:00
Daniel Marjamäki c6d43506b6 ValueFlow: Fix FP in switch 2018-09-04 20:28:48 +02:00
Daniel Marjamäki cc402869a6 Fixed #8689 (False positive: containerOutOfBounds after function call) 2018-09-02 14:08:34 +02:00
Daniel Marjamäki 6223204a06 Fixed #8708 (FP: Accessing an item in container that is empty) 2018-08-27 13:49:47 +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 866d198756 Fixed #8697 (noreturn output stream) 2018-08-21 06:32:33 +02:00
Daniel Marjamäki 2f834c3c3b Fixed #8688 (False positive: containerOutOfBounds) 2018-08-13 21:27:29 +02:00
Daniel Marjamäki 55ab842083 valueFlowSizeForward: Bailout when function calls modify the container size 2018-08-13 06:54:18 +02:00
Daniel Marjamäki 183345a939 valueFlowContainerSize: Fix FP when container size is changed 2018-08-12 22:23:19 +02:00
Daniel Marjamäki 7074eeb869 valueFlowContainerSize: assignment of string-like containers 2018-08-12 22:01:58 +02:00
Daniel Marjamäki 189ea5003a valueFlowContainerSize: empty container variables 2018-08-12 14:41:00 +02:00
Daniel Marjamäki 811a71aa06 valueFlowContainerSize: Handle guard logic 2018-08-11 15:08:54 +02:00
Daniel Marjamäki 6008bbdfb4 valueFlowContainerSize: Fix wrong value after condition if conditional code escapes 2018-08-11 13:49:42 +02:00
Daniel Marjamäki 10461e5429 valueFlowContainerSize: forward analysis inside conditional code 2018-08-10 22:18:38 +02:00
Daniel Marjamäki 4a502a7f6b valueFlowContainerSize: add simple forward analysis 2018-08-10 22:06:23 +02:00
Daniel Marjamäki 2b10e38eec valueFlowContainerSize: handling of 'v.size() == 10' 2018-08-10 21:43:47 +02:00
Paul Fultz II 3947c23290 Fix issue 8369: False negative: Condition 'condition' is always true (#1325)
* Fix issue 8369: False negative: Condition 'condition' is always true

* Use simpleMatch

* Add iterator header

* Cleanup

* Remove unused variable
2018-08-10 18:05:23 +02:00
Daniel Marjamäki 907f81d059 ValueFlow: Improved analysis of container size 2018-08-10 11:53:56 +02:00