Commit Graph

8604 Commits

Author SHA1 Message Date
Daniel Marjamäki 9ef3c79bc6 Fixed #8851 (Crash in valueFlowContainerForward (daca@home: pbbam)) 2018-11-18 20:18:55 +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 d376e9f245 Track variable lifetime through function calls (#1481) 2018-11-16 06:12:28 +01:00
Carlo Marcelo Arenas Belon d880d64a9c test: avoid a mutable comparator for test (#1482) 2018-11-15 16:21:12 +01:00
Daniel Marjamäki 5d086d60ad Fixed #8844 (snd: Wrong varid and ast) 2018-11-14 21:05:09 +01:00
Daniel Marjamäki 69f2e0938a VarId: Improved varids for struct members (#8844) 2018-11-14 19:11:35 +01:00
Daniel Marjamäki 69f6100d83 Fixed #8339 (false positive: unread variable despite delete is used on it) 2018-11-14 17:04:21 +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 f0c86b9d14 Fix issue 8842: Crash in valueFlowTerminatingCondition on incomplete code (#1480) 2018-11-14 06:23:45 +01:00
Daniel Marjamäki e6a5e0f752 Warn when there is a unknown macro 2018-11-13 16:49: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 3d629944da Fixed #8100 (False positive when method/function defines lambda with pointer return value) 2018-11-12 11:28:38 +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
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
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 3f55707b30 Fixed #8064 (Taking the address of a moved variable is not accessing it) 2018-11-11 12:47:27 +01:00
Paul Fultz II e46691597e Fix issue 8841: FP knownConditionTrueFalse - Dont treat unknown function as noreturn (#1474)
* Fix issue 8841: Dont treat unknown function as noreturn

* Add a parameter to set default value when the function is unknown
2018-11-11 08:04:01 +01:00
Daniel Marjamäki ee2dfb6604 Fixed #8058 (False positive returnAddressOfAutoVariable within lambda) 2018-11-11 07:52:38 +01:00
Daniel Marjamäki 93cde415a7 Fixed #7988 (variable in condition gets wrong varid) 2018-11-11 07:50:25 +01:00
Daniel Marjamäki 148f257c6b Fixed #8838 (False positive on overridden virtual function that is marked both const and final) 2018-11-10 21:57:50 +01:00
Daniel Marjamäki 8327aab127 Fixed #8628 (Wrong AST in case) 2018-11-10 21:32:06 +01:00
Daniel Marjamäki d5a478d5c5 astyle formatting
[ci skip]
2018-11-10 21:30:01 +01:00
Daniel Marjamäki 4cef2e94e7 Fixed #8368 (FP redundantAssignment - assignment has unknown side-effects) 2018-11-10 18:42:13 +01:00
IOBYTE 69e6e11844 Fix a template simplifier namespace bug in #7145 (#1473)
* Fix a template simplifier namespace bug in #7145

* Refactor template simplifier to only call getTemplateDeclarations once per loop.
2018-11-10 16:41:14 +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
IOBYTE e302e6e7a1 Fix symbol database argsMatch to skip all redundant type information (class, struct, union, enum) (#1472) 2018-11-09 15:54:17 +01:00
IOBYTE 67fe99fcd9 Fix symbol database argsMatch function and its usage so qualified types are handled properly. (#1470)
* Fix symbol database argsMatch function and its usage so qualified types are handled properly.

* Remove assert.
2018-11-09 06:11:48 +01:00
Paul Fultz II 36e663e250 Fix issue 8839: FP knownConditionTrueFalse - condition inside a while-clause (#1469) 2018-11-09 06:09:51 +01:00
IOBYTE 17a8a4898d Fix another template simplifier namespace bug. (#1467)
* Fix another template simplifier namespace bug.

* Add missing forward declarations caused by token bug.
2018-11-07 21:25:42 +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
IOBYTE c966f31183 Fixed #8835 (friend class and non-empty constructor: Uninitialized members not reported) (#1466) 2018-11-06 06:44:08 +01:00
Daniel Marjamäki 00340efc57 ValueFlow: Refactoring valueFlowCallFunction 2018-11-06 06:38:26 +01:00
IOBYTE 9b67e680ae Fix template simplifier namespace support. (#1464) 2018-11-05 19:55:21 +01:00
Paul Fultz II d62ed420d8 Add a check target to cmake (#1465) 2018-11-05 18:13:04 +01:00
Daniel Marjamäki ca19894a04 ValueFlow: In evaluate handle correlated values 2018-11-05 18:07:35 +01:00
IOBYTE 2275f05f65 Fixed #8833 (false negative: No 'return' statement in non-void function causes undefined behavior.) (#1463) 2018-11-05 06:55:30 +01:00
Daniel Marjamäki 8a54420274 ValueFlow: Try to handle multiple argument values in <returnValue> evaluation 2018-11-04 20:52:12 +01:00
orbitcowboy 1d5e797e6d testother: Fixed typos in tests. Replaced 'voif' with 'void'. 2018-11-04 14:46:22 +01:00
Daniel Marjamäki 6878de2c5e ValueFlow: getExpressionRange, valueFlowRightShift 2018-11-03 23:25:46 +01:00
Daniel Marjamäki f8b0584f6a replace 'constexpr' with 'const' 2018-11-03 19:22:51 +01:00
Daniel Marjamäki 66ca03fa0c Fixed #8826 (false negative: Invalid memory address freed) 2018-11-03 18:55:20 +01:00
Daniel Marjamäki ef35b86b4a Multipass Valueflow 2018-11-03 15:53:24 +01:00
Daniel Marjamäki 6138294e3d Fixed #8818 (AST broken: restrict) 2018-11-03 11:05:38 +01:00
Daniel Marjamäki 9741239b2f Fixed #8824 (False positive: uninitialized variable (regression)) 2018-11-03 10:31:55 +01:00
Daniel Marjamäki a4afcb5995 Revert "Fixed #8818 (AST broken: restrict)"
This reverts commit e7d61f399a.
2018-11-02 21:07:37 +01:00
Daniel Marjamäki 0cc4555337 Revert "Tokenizer: Do not remove 'constexpr'"
This reverts commit 2f400b40ba.
2018-11-02 21:07:24 +01:00
Daniel Marjamäki 2f400b40ba Tokenizer: Do not remove 'constexpr' 2018-11-02 20:32:12 +01:00
Daniel Marjamäki e7d61f399a Fixed #8818 (AST broken: restrict) 2018-11-02 20:28:49 +01:00
Daniel Marjamäki 5de683ec49 Container size: Fix false positive 2018-11-02 20:10:40 +01:00
Daniel Marjamäki e53be26f48 Try to fix Travis 2018-11-02 19:04:21 +01:00
Daniel Marjamäki 16cc20a232 ValueFlow: Better handling of container size values 2018-11-02 18:28:32 +01:00
Daniel Marjamäki 3798feecad TemplateSimplifier : Remove 'class|struct' in template arguments 2018-11-02 14:49:40 +01:00
rikardfalkeborn 869e4ba6ab Add check for return value of boolean function (#1451)
* Add check for return value of boolean function

The rule for converting an integer to a boolean is that 0 is mapped to
false and everything else is mapped to true. There is nothing wrong with
the following code (according to the standards):

    bool f()
    {
        return -1;
    }

and neither gcc nor clang will warn about it. However, it's a bit
confusing. This commit adds a check that warns when a value other than 0
or 1 is returned from a boolean function (similar to the existing check
that functions with boolean arguments are only passed 0 or 1). Since the
code is perfectly legal, set the severity to "Style".

* Use early continue and remove some braces

* Add testcase with multiple returns

* Avoid null pointer dereference in case of return without operand

* Skip lambdas

Add TODO-test cases that shows FPs when the return type of lambdas are
specified explicitly (this is a problem with findLambdaEndToken).

* Enable testcases
2018-11-01 11:08:16 +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
Paul Fultz II fafd0742d4 Fix FPs with return conditions (#1455)
* Fix 8815: FP with identical inner conditions

* Fix issue 8801: FP when not returning a bool

* Fix FP

* Add missing semicolon

* Move returnVar
2018-10-31 09:47:48 +01:00
amai 3032ac4619 Fix compile error in test code 2018-10-28 21:36:28 +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 8beb42cc90 astyle formatting
[ci skip]
2018-10-28 17:37:19 +01:00
Paul Fultz II 2b6cc33dc6 Fix issue 8757: Throw syntax error on invalid code (#1378)
* Fix issue 8757: Throw syntax error on invalid code

* Fix FP with lambda returns

* Remove double percent

* Check more keywords

* Skip preprocessor directives

* Check for valid PP directive

* Fix preprocessor check

* Dont check for preprocessor directives
2018-10-28 17:17:53 +01:00
Paul Fultz II f5811c6818 Fix issue 8732: Syntax error when using enable_if (#1453)
* Fix issue 8732: Syntax error when using enable_if

* Fix FPs

* Use simpleMatch
2018-10-28 17:16:31 +01:00
Daniel Marjamäki 06ede9c239 ValueFlow: handle x+=y for containers 2018-10-27 18:38:04 +02:00
orbitcowboy b998dcb5aa gnu.cfg: Added support for '__builtin_memset'. 2018-10-27 18:25:05 +02:00
IOBYTE adbbadec7f Fixed #8798 (template simplifier: wrong simpifications for namespaces) (#1452) 2018-10-26 14:20:13 +02:00
Daniel Marjamäki ab08801dc4 Remove header tests from testconstructors, because they were invalid (using preprocessor directive)
The checker does not care if it's a header or source file. So I guess these tests were added to test
some parsing problem. Nowadays, #line is removed by simplecpp::preprocess, but there is no proper
testing for it in simplecpp.. I will add some testing for #line asap.
2018-10-26 07:03:15 +02: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 b8e8b12783 Fixed #8797 (exprDependsOnThis handle method call in non-inline method) 2018-10-24 20:17:00 +02:00
Daniel Marjamäki 2348dcde6c #8801: Quick fix for false positives 2018-10-24 19:57:50 +02:00
IOBYTE 0763fdbfad Copy template default argument values from forward declaration to declaration. (#1447)
It is possible to define default template parameter values in forward
declarations and not define any in the actual declaration.  Cppcheck
ignores forward declarations and only uses the default values in the
actual declaration so default values in forward declarations are copied
to the actual declaration when necessary.
2018-10-24 14:38:59 +02:00
Daniel Marjamäki 15160f1691 Fixed #8788 (AST Broken error from assigning lambda to variable) 2018-10-22 17:25:01 +02:00
Daniel Marjamäki dc38681a56 Remove FIXME in AST validation. Ticket #8749 2018-10-22 11:37:24 +02:00
Daniel Marjamäki c483bcb12b Fixed testrunner 2018-10-22 00:28:09 +02:00
Daniel Marjamäki f9b132e831 Tokenizer::simplifyTypedef: Fix bug with arrays 2018-10-21 21:15:34 +02:00
Paul Fultz II edde0eedaa Fix FP issue 8801: Condition 'a+b' is always true (#1444) 2018-10-21 08:04:00 +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 14afc3fd3a Fixed #8796 (Tokenizer::simplifyCompoundAssignment: Wrong simplification of return) 2018-10-20 21:33:43 +02:00
Daniel Marjamäki 29feaa5a51 Tokenizer: Avoid constant folding 2018-10-20 10:51:50 +02:00
Daniel Marjamäki d265a0d6ab Fixed #8798 (SymbolDatabase: Inner unnamed union) 2018-10-20 09:43:08 +02:00
orbitcowboy 0858488825
insecureCmdLineArgs: Fixed FN in case strdup() copies argv[]. (#1438)
* insecureCmdLineArgs: Fixed FN in case strdup() copies argv[].

* Formatted the code. There are no functional changes intended.

* Changes due to review comments from Daniel.
2018-10-19 11:04:15 +02:00
rikardfalkeborn 402d0c565f Fix false positive: Invalid string argument with array (#1439) 2018-10-19 07:48:47 +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 465db2dff7 Fixed #8786 (internalAstError on valid code with C style cast) 2018-10-18 20:17:23 +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
Igor 0a9be3e734 Improve STL iterators checking (#1380)
* Improve STL interators checking

* Improve error messages for container iterators from different scopes

* Mini refactoring

* Replace hardcoded pattern to ValueType::Type::ITERATOR

* Error messages improvements, more tests and refactoring

* Refactoring after code review

* Put getting operand data into separate function

* Update getErrorMessages and iterator errors ids

* Refactoring

* Fix error

* Refactoring, early return implementation

* Delete redundant code

* Tiny changes in comments
2018-10-17 06:36:51 +02:00
Daniel Marjamäki 1245a036f7 Add check for shadow variables 2018-10-16 20:17:27 +02:00
rikardfalkeborn f1074ea1ab Fix false positive: Invalid string argument with pointer to pointer (#1427)
If the address is taken inside an array, the address is not of a single
character, so do not warn about this.
2018-10-16 06:54:25 +02:00
IOBYTE 290563b964 Fix specialized template regression. (#1425)
* Fix specialized template regression.

Only check for instantiation of template being processed rather than
count of all instantiations.

* Add 2 more tests.
2018-10-15 19:35:26 +02:00
orbitcowboy a6e8270474
insecureCmdLineArgs: Fixed false negatives in case arguments are const. (#1419)
* insecureCmdLineArgs: Fixed false negatives in case arguments are const.

* Formatted the code, there are functional changes.

* Simplified matching as suggested by Daniel.
2018-10-15 10:05:43 +02:00
rikardfalkeborn 613dc19b68 #4241: Check for address of single character passed as string (#1381)
* #4241: Check for address of single character passed as string

Add a check that address of a single character is not passed as argument
to argument marked as strings (using strz). The check does not warn if
the address of a character with known value '\0'.

Since ValueFlow currently does not handle global constants (see #7597),
do not warn if the variable is global to avoid FPs when the address of
a global variable assigned to '\0' is passed to a function expecting a
string.

Remove comment in docs saying strz is unused.

* Change asdf to Hello world

* Add test of address to first element in string

* Add error reporting function to getErrorMessages

* Fix strings in test
2018-10-14 18:49:34 +02:00
IOBYTE 0a30768b59 Fixed #8693 (Template specialization: Constructor detected as normal … (#1418)
* Fixed #8693 (Template specialization: Constructor detected as normal function (functionStatic error))

Refactor template simplifier to remove the existing full specialization
function expandSpecialized and allow full specializations to use the
existing function expandTemplate.  The function expandTemplate was
modified to either expand the template like it originally did by copying
it or to modify the template in place.  Both instantiated and
uninstantiated full specializations are modified in place.  This also
fixes #8692 and probably other related tickets as well.

The function simplifyTemplates now tries twice to simplify templates so
more templates can be simplified.  We should try as many times as
necessary to find all possible templates.  We can't do that now because
uninstantiated templates are left unchanged.  It is relatively straight
forward to have the new code also expand in place uninstantiated
templates with their symbolic types but namespaces are not handled
properly (ticket #8671) and it would introduce regressions.

* Fix travis warnings.
2018-10-14 16:57:07 +02:00
Daniel Marjamäki 140b0b5ae9 astyle formatting
[ci skip]
2018-10-13 18:42:09 +02:00
Paul Fultz II 9cf092657c Skip some ast errors to avoid regressions (#1422)
* Skip some ast errors to avoid regressions

* Use simpleMatch

* Skip operator functions

* Add a test for issue 8788
2018-10-13 18:38:44 +02:00
Daniel Marjamäki 7833ade128 Updated copyright year 2018-10-13 18:20:31 +02:00
Daniel Marjamäki 7eb5ebe17e Fixed #7887 (User function 'search' is wrongly mixed with std function) 2018-10-10 17:35:53 +02:00
Daniel Marjamäki c048bd45f6 Refactor test code 2018-10-10 14:35:58 +02:00
Daniel Marjamäki 25cbfe27cf Partial fix for #7887, Improved tokenizer simplification of 'using namespace std;' - do not simplify user functions 2018-10-10 14:28:53 +02:00
Daniel Marjamäki 053b0d1654 STL: enable inconclusive warnings with --inconclusive 2018-10-09 20:10:43 +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 20121b34d8 Fixed #7718 (False positive: out of bounds of already resized std::string) 2018-10-09 06:53:26 +02:00