Commit Graph

1324 Commits

Author SHA1 Message Date
rikardfalkeborn 7779a9186e Use valueflow in unsigned less than zero checker (#1630)
The unsigned less than zero checker looked for patterns like "<= 0".
Switching to use valueflow improves the checker in a few aspects.

First, it removes false positives where instead of 0, the code is using
0L, 0U, etc. Instead of having to hard code the different variants of 0,
valueflow handles this automatically. This fixes FPs on the form

	uint32_t value = 0xFUL;
	void f() {
  		if (value < 0u)
		{
			value = 0u;
		}
	}

where 0u was previously not recognized by the checker. This fixes #8836.

Morover, it makes it possible to handle templates properly. In commit
fa076598ad, all warnings inside templates
were made inconclusive, since the checker had no idea if "0" came from
a template parameter or not.

This makes it possible to not warn for the following case which was
reported as a FP in #3233

	template<int n> void foo(unsigned int x) {
	if (x <= n);
	}
	foo<0>();

but give a warning for the following case

	template<int n> void foo(unsigned int x) {
	if (x <= 0);
	}

Previously, both these cases gave inconclusive warnings.

Finally, it makes it possible to give warnings for the following code:

	void f(unsigned x) {
		int y = 0;
		if (x <= y) {}
	}

Also, previously, the checker for unsigned variables larger than 0, the
checker used the string of the astoperand. This meant that for code like
the following:

	void f(unsigned x, unsigned y) {
		if (x -y >= 0) {}
	}

cppcheck would output

	[unsigned-expression-positive.c] (style) Unsigned variable '-' can't be negative so it is unnecessary to test it.

using expressionString() instead gives a better error message

        [unsigned-expression-positive.c] (style) Unsigned expression 'x-z' can't be negative so it is unnecessary to test it.
2019-01-31 09:30:29 +01:00
Daniel Marjamäki ac357a96f7 Fixed #7384 (confuses syntax with variable name / "variable '(' is less than zero") 2018-12-31 18:36:06 +01:00
Daniel Marjamäki ed514644b8 Renamed FwdAnalysisAllPaths to FwdAnalysis 2018-12-31 18:00:47 +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 fe38e256cc Fixed #7907 (FN: redundant assignment inside switchcase, overwritten by assignment outside of switch) 2018-12-17 16:10: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 1f27cd56c0 FwdAnalysis; Code cleanup, isNullOperand 2018-12-13 21:01:33 +01:00
Daniel Marjamäki a16f694254 Revert "Fix issue 8884: Regression: False positive: Variable 'f' is reassigned a value before the old one has been used (#1513)"
This reverts commit 6953dddfa6.
2018-12-08 08:25:20 +01:00
Paul Fultz II 6953dddfa6 Fix issue 8884: Regression: False positive: Variable 'f' is reassigned a value before the old one has been used (#1513) 2018-12-08 07:33:51 +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 ba564076db Fixed #7907 (FN: redundant assignment inside switchcase, overwritten by assignment outside of switch) 2018-12-02 14:04:59 +01:00
Daniel Marjamäki 88785dda02 Refactoring the redundant assignments check 2018-12-02 11:41:27 +01:00
Daniel Marjamäki 13c6489571 Redundant variable assignment: Fix FN for struct members 2018-11-27 18:22:22 +01:00
Daniel Marjamäki 32c05fef92 Redundant assignment: check that --enable=style has been used. 2018-11-27 13:04:50 +01:00
Daniel Marjamäki affd03f1d6 Minor tweak for redundantVariableAssignment 2018-11-26 11:16:50 +01:00
Daniel Marjamäki 0160f80ffe redundantVarAssignment: avoid FPs when loops are used 2018-11-25 10:32:30 +01:00
Daniel Marjamäki 22f736eae2 redundant variable assignments: avoid FPs for initializations 2018-11-25 08:49:09 +01:00
Daniel Marjamäki 2f66b31d43 redundantAssignmentInSwitch: fix false positive when there is goto 2018-11-24 21:39:01 +01:00
Daniel Marjamäki 866688c70a Rewriting redundantAssignment checker 2018-11-24 10:03:54 +01:00
Daniel Marjamäki 8f164d9602 Refactoring; use visitAstNodes 2018-11-23 19:22:53 +01:00
Daniel Marjamäki ecb3c04fba astutils: Add function that visits nodes in AST tree 2018-11-23 06:53:43 +01:00
Daniel Marjamäki 9d8e5e36eb Added a few TODO comments 2018-11-20 22:57:01 +01:00
Daniel Marjamäki a5af5b2d08 Revert "Fixed #7619 (False positive: Redundant assignment)"
There is a crash I need to look into.
2018-11-20 06:23:43 +01:00
Daniel Marjamäki c899792232 Fixed #7619 (False positive: Redundant assignment) 2018-11-19 21:23:36 +01:00
Daniel Marjamäki a8cbbe0e16 Fixed #8816 (FP shadowLocal - variable shadows a template function?) 2018-11-19 07:00:15 +01:00
Daniel Marjamäki 69e7392ce2 Fixed #8449 (False positive 'constStatement' when initializing std::unordered_map) 2018-11-12 18:10:34 +01:00
Daniel Marjamäki 58882b1f14 Fixed #8261 (False positive "Statements following return, break, continue, goto or throw will never be executed.") 2018-11-12 06:31:17 +01:00
Daniel Marjamäki 3f55707b30 Fixed #8064 (Taking the address of a moved variable is not accessing it) 2018-11-11 12:47:27 +01:00
Daniel Marjamäki 4cef2e94e7 Fixed #8368 (FP redundantAssignment - assignment has unknown side-effects) 2018-11-10 18:42:13 +01: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 29feaa5a51 Tokenizer: Avoid constant folding 2018-10-20 10:51:50 +02:00
Daniel Marjamäki 4983a6a5dc astyle formatting 2018-10-18 20:08:32 +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
Paul Fultz II 58d1de5814 Expand the duplicate variable assignment warnings when the inconclusive flag is used (#1433)
* Warn for more duplicate var expressions when inconclusive is set

* Fix issue with missing function name
2018-10-17 06:57:33 +02:00
Daniel Marjamäki 1245a036f7 Add check for shadow variables 2018-10-16 20:17:27 +02:00
rikardfalkeborn 75caf8e4de Fix #8230: FP unknown evaluation order on comma expression in while clause (#1415)
The while part of a do-while loop looks almost like a function call, so
extend the check for function calls to ignore while-statements.

Note that there was only an FP when checking c-code, since the check is
disabled for c++-code. Therefore, make sure the test cases are run on a
c-file.
2018-10-09 14:44:01 +02:00
Daniel Marjamäki aa58f41e6d Fixed #7152 (False positive for redundantAssignment after calling a lambda function) 2018-10-08 21:07:21 +02:00
Carlo Marcelo Arenas Belon d66c92edc3 Remove unused parameters for CheckOther::oppositeExpressionError (#1412) 2018-10-05 08:36:49 +02:00
Paul Fultz II 26a8435d76 Use knownConditionTrueFalse when is duplicate expression is true or false (#1410) 2018-10-05 06:00:24 +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 4598995564 Enable followVar for duplicate ternary expressions (#1406) 2018-10-01 14:31:06 +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
Daniel Marjamäki 7591616f43 Fixed #8710 (Update message duplicateExpression when operand is integer literal) 2018-08-30 18:28:34 +02:00
Daniel Marjamäki ad0ebd8f1a astyle formatting
[ci skip]
2018-08-25 21:32:52 +02:00
Paul Fultz II a5f4c5d0eb Improve message for same expressions (#1349)
* Improve message for same expressions

* Update message
2018-08-25 14:25:31 +02:00
Daniel Marjamäki e442bc47b1 astyle formatting
[ci skip]
2018-08-17 19:55:21 +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