Commit Graph

1363 Commits

Author SHA1 Message Date
Daniel Marjamäki 00fae7fb42 Fixed #9261 (Inconsistent violation report between using global enum and namespaced enum.) 2019-10-20 07:07:38 +02:00
Daniel Marjamäki 574141a492 shadowVariable: Add comment 2019-10-19 18:51:36 +02:00
Daniel Marjamäki 24db81adb5 Fixed #8903 (False positive 'shadows outer variable' when using GCC statement expressions) 2019-10-19 18:50:27 +02:00
Steve Mokris 506a952ad2 Parse `extern "C"`, and use it to avoid FP reference warnings (#2234)
Previously, cppcheck discarded the `extern "C"` specifier.  This patch modifies cppcheck to parse each as a Scope in the symbol database, then uses that scope to avoid false positives when making recommendations about changing a function argument to be a reference (since variable references is a C++ feature, unavailable in C, and thus unavailable in `extern "C"`).
2019-10-08 17:48:09 +02:00
Daniel Marjamäki 07b337c580 Removed extra zero division heuristics, they seem redundant as ValueType is used 2019-10-01 17:33:58 +02:00
warmsocks a56bc006b7 Fixed a typo in lib/checkother.cpp. Corrected spelling errors found by codespell. (#2170) 2019-09-11 19:21:38 +02:00
Daniel Marjamäki a9fda3f488 Clarify redundantVarAssignment warnings 2019-09-04 10:55:41 +02:00
IOBYTE e5220bdf0c make ellipsis ... a single token (#2143)
* make ellipsis ... a single token

Using cppcheck -E to preprocess code with ellipsis produces output that
can't be compiled because ... is split into 3 tokens.

* try to fix addon
2019-09-04 08:07:30 +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
Daniel Marjamäki 4bd9d76a4c Try to make Travis happy 2019-08-25 10:24:13 +02:00
Daniel Marjamäki 82eec11898 Created redundantInitialization id 2019-08-25 09:45:39 +02:00
Daniel Marjamäki 9cbdc262f6 Fix Cppcheck internal warning 2019-08-23 16:56:28 +02:00
Daniel Marjamäki d3f7fc92f2 Quick fix to make Travis happy 2019-08-23 15:23:49 +02:00
Daniel Marjamäki 7061cc334b RedundantAssignment: Don't warn for initialization with {0} 2019-08-23 08:51:16 +02:00
Paul Fultz II 744a94ad5a Fix issue 9231: FP constParameter - argument passed parent ctor (#2032) 2019-07-24 20:59:58 +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
Daniel Marjamäki d11d6f112e Detect shadowed arguments 2019-07-17 17:08:42 +02:00
Daniel Marjamäki ef73a10e30 Replace 'unsigned' with 'nonneg' in checkother 2019-07-16 09:10:10 +02:00
Daniel Marjamäki cf06acae08 Fixed #5995 (False negative selfAssignment regression from 1.65) 2019-07-13 21:45:54 +02:00
Daniel Marjamäki 9f548efbd3 Refactoring: enum class 2019-07-10 15:27:07 +02:00
shaneasd 7e54f989f9 Update symbol database such that the override keyword implies that the function is also virtual (#1907)
* Update symbol database such that the override keyword implies that the function is also virtual

* Add test case for implicit override

* change isVirtual to hasVirtualSpecifier

* fix method documentation for getVirtualFunctionCalls and getFirstVirtualFunctionCallStack

* Fix isImplicitlyVirtual to consider the override keyword and document logic

* Fix getFirstVirtualFunctionCallStack and getVirtualFunctionCalls to use isImplicitlyVirtual instead of isVirtual so new test case passes
2019-07-04 12:32:32 +02:00
Daniel Marjamäki 7ceb51a952 Try to improve 'clarifyStatement' warning message 2019-06-22 08:44:41 +02:00
Paul Fultz II f75c15af56 Fix issue 6821: New check: access heap/stack data using address of variable
This fixes errors with:

```cpp
int f() {
    int i;
    return (&i)[1];
}
```

It uses the lifetime analysis to detect the issues.
2019-05-31 12:24:31 +02:00
Daniel Marjamäki de4a33167d astyle formatting
[ci skip]
2019-05-21 10:43:33 +02:00
Paul Fultz II 9838bfa79f Fix false positive in constArgument when passing struct member (#1845) 2019-05-21 10:41:16 +02:00
Daniel Marjamäki cb06aebdab Removed --std=posix. From now on, if you use --library=posix then the posix checks will be enabled. 2019-04-12 06:47:28 +02:00
Rikard Falkeborn d23e987941 Fix CheckInternal warnings (#1790) 2019-04-06 06:55:46 +02:00
Daniel Marjamäki 3f9dd4c567 Variable scope: Fix FP for reference variable in range for loop 2019-04-02 12:59:24 +02:00
Daniel Marjamäki 0efddc4010 astyle formatting
[ci skip]
2019-03-31 16:20:06 +02:00
Daniel Marjamäki 29a5404d1e Incomplete statement: Fix FP for 'ar & x' 2019-03-31 11:50:57 +02:00
Daniel Marjamäki 6eaf2c03d9 CheckOther::checkInvalidFree: Move check to normal checking. And clarify the message. 2019-03-07 06:35:44 +01:00
Paul Fultz II 032020c40d Fix issue 9001: FP: Found suspicious operator ',' [constStatement] 2019-02-27 07:09:22 +01:00
Paul Fultz II e846312fed Check subtraction of pointers to different objects 2019-02-27 06:55:48 +01:00
Daniel Marjamäki 68062e3702 Fixed #8954 (false positive: Local variable x shadows outer variable) 2019-02-23 16:22:22 +01:00
Paul Fultz II fd3c1fd040 Fix issue 1777: Undefined Behavior: Comparing pointers to different objects
This uses the lifetime analysis to check when comparing pointer that point to different objects:

```cpp
int main(void)
{
    int foo[10];
    int bar[10];
    int diff;

    if(foo > bar)   // Undefined Behavior
    {
       diff = 1;
    }

    return 0;
}
```
2019-02-23 08:32:08 +01:00
Paul Fultz II dfaf75db54 Fix issue 8984: FP constStatement regression with qualified name (#1676) 2019-02-19 07:08:36 +01:00
Paul Fultz II cf1ad5087a Extend constStatement checker
This reworks constStatement to find more issues. It catches issue [8827](https://trac.cppcheck.net/ticket/8827):

```cpp
extern void foo(int,const char*,int);
void f(int value)
{
        foo(42,"test",42),(value&42);
}
```

It also catches from issue [8451](https://trac.cppcheck.net/ticket/8451):

```cpp
void f1(int x) {
    1;
    (1);
    (char)1;
    ((char)1);
    !x;
    (!x);
    ~x;
}
```

And also:

```cpp
void f(int x) {
    x;
}
```

The other examples are not caught due to incomplete AST.
2019-02-15 13:31:40 +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
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