Commit Graph

600 Commits

Author SHA1 Message Date
Oliver Stöneberg 6397e29f84
cleaned up includes based on include-what-you-use (#3141) 2021-04-03 21:30:50 +02:00
Daniel Marjamäki 42437277dc Update Copyright year 2021-03-21 20:58:32 +01:00
PKEuS 141d2ac215 Refactorization: Improved internal implementation of severity and certainty levels
Backported from LCppC.
2021-02-24 22:00:06 +01:00
Oliver Stöneberg 7aa85aa408
Use std::unordered_* containers for faster lookups (#3052) 2021-01-16 13:52:09 +01:00
Ken-Patrick Lehrmann 56124f0c5d
Skip calculateWarningHash if we are not in bugHunting (#3047) 2021-01-13 18:38:00 +01:00
Rikard Falkeborn 791051ced0 Fix #9228 (FN common realloc mistake with assignment of NULL)
Do not match for assignments with NULL.
2020-09-13 21:49:49 +02:00
Rikard Falkeborn da8ad9ce19 memleakOnRealloc: Don't warn if pointer is NULL in condition 2020-09-12 18:48:23 +02:00
Rikard Falkeborn d5345052ab Fix #9793 (false positive, memleak with lambda)
Skip scopes with lambdas (similar to how checkleakautovar does). In
order to fix this when the lambda is inside a for loop, make
hasInlineOrLambdaFunction() recursive. This should be what all existing
users want.
2020-07-07 21:51:36 +02:00
Oliver Stöneberg 37bc0483a4
made check.h less heavy (#2633) 2020-05-23 07:16:49 +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
Oliver Stöneberg 2c1e36e63e
cleaned up includes based on include-what-you-use (#2600)
* cleaned up includes based on include-what-you-use

* check.h: trying to work around Visual Studio 2012 bug

* fixed Visual Studio compilation
2020-04-13 13:44:48 +02:00
Daniel Marjamäki b6833b525f Clang import 2020-01-25 17:01:17 +01:00
John Marshall 297efcd049 Avoid some additional memleakOnRealloc false positives (#2422)
* Avoid some additional memleakOnRealloc false positives

checkReallocUsage() already contains code to suppress the
`p = realloc(p, size)` error message when the pointer has been
previously copied from another variable (hence there is an additional
copy of the original pointer value) within the same function, as in
the added realloc21() test case.

Extend this so that `p = *pp` and `p = ptr->foo` are also recognized
as copies from another variable with the same original pointer value,
as in the added realloc22() and realloc23() test cases.

* Rewrite as a single findmatch() expression
2019-12-04 12:13:44 +01:00
Sebastian c990d10ffa
Check for JSON error when parsing addon .json files + fixes (#2374)
* cppcheck.cpp: Check for JSON error when parsing addon .json files

This fixes that errors in JSON files given via `--addon=*.json` are
silently ignored and maybe only a part of the JSON file is used.
Now the error message which picojson can return is checked and a
corresponding error message is returned again by getAddonInfo().

* naming.json: Fix missing comma

* CLI: Fix naming violations detected by addon naming.py via naming.json

* Addon naming: Add argument for validating names of constants

* LIB: Rename functions/variables so they are valid, loosen naming rules

* GUI: Fix naming violations
2019-11-20 15:37:09 +01:00
Rikard Falkeborn 239b660a52 Fix #9438 (Don't warn for return (void*) malloc(1)) (#2307) 2019-10-30 17:55:47 +01:00
Rikard Falkeborn fd3cb24973 leakNoReturnVar: Don't break early (#2095)
There seems to be no reason for stopping checking the scope if a call to
free() is seen (or fclose() or realloc()), so just continue checking.
Also, if there are multiple arguments, check all, perhaps there are more
memory leaks to warn about.
2019-08-23 06:33:00 +02:00
Rikard Falkeborn fc1d5b187f leakNoVarFunctionCall: Use AST more (fix #9252) (#2086)
Use the AST a little bit more to improve the check. In order to do so,
rewrite the check to work from the outer function first and then check
the arguments, instead of the other way around.

It also fixes Trac ticket #9252, no warning is now given for

	void* malloc1() {
		return(malloc1(1));
	}

This FP seems to be common in daca results.

It also makes it possible to improve handling of casts, for example
cppcheck now warns about

	void f() {
		strcpy(a, (void*) strdup(p));
	}

But not for

	char* f() {
		char* ret = (char*)strcpy(malloc(10), "abc");
		return ret;
	}

These FP/FN were introduced when the check was switched to use the
simplified token list.
2019-08-14 22:01:40 +02:00
Rikard Falkeborn ffcceb097e Memleak: Refactor check to separate function (#2079)
This makes all the checks in CheckMemoryLeakNoVar separate functions to
improve readability.
2019-08-13 13:00:59 +02:00
Rikard Falkeborn cd36f8ed0a Fix #9253: leakNoVarFunctionCall: do not warn if freopen opens standard stream (#2076)
This fixes false positives from daca@home where freopen is used to
reopen a standard stream. There is no longer a warning for

	void f() {
		assert(freopen("/dev/null", "r", stdin));
	}
2019-08-12 12:53:59 +02:00
Rikard Falkeborn a69a570505 Fix FP leakVarNotUsed with freopen() and stdin (#2034)
One usecase for freopen() is to redirect input and output streams to
files. For that, the return value is not needed.
2019-07-25 21:09:23 +02:00
Daniel Marjamäki 4fb6c27276 Modernize: Use enum class 2019-07-23 14:29:02 +02:00
Daniel Marjamäki 7ed3db7b32 astyle formatting
[ci skip]
2019-07-22 10:38:17 +02:00
Rikard Falkeborn 8cd1d5a47d Use library for memleak checks (#2002)
* Use library for memleak checks

Change memleakOnRealloc and leakReturnValNotUsed to use library
configuration instead of hardcoding "realloc".

In order to do so, some care needs to be taken when matching for a
reallocation function, since it can no longer be assumed that the input
to be allocated is the first argument of the function. This complicates
getReallocationType() and checkReallocUsage() but is necessary in order
to handle for example freopen() properly.

Also, refactor memleakOnRealloc check to reduce duplicated code when
checking "a" and "*a". When doing so, extending the check to look for
arbitrary number of "*" can be done for free (just change an if
statement to a while statement). Most likely, this is an unusual case in
real world code.

* Remove redundant whitespace in Token::Match()

* Run on simplified checks

* Fix cppcheck warning
2019-07-22 10:37:36 +02:00
Daniel Marjamäki e124b31334 Replace 'unsigned' with 'nonneg' 2019-07-17 09:11:42 +02:00
Daniel Marjamäki c04f31a2c2 Refactoring: Use common function 2019-07-17 09:04:42 +02:00
Daniel Marjamäki 5800692fa1 Move and refactor the CheckMemoryLeak::isclass 2019-07-17 08:59:09 +02:00
IOBYTE a05dfc9150 fix daca codeblocks crash (#1968)
* fix daca codeblocks crash

* fix the cause of the missing scope
2019-07-09 16:04:22 +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 80d7df01cd Fixed #8848 (False positive memory leak if locally defined type returns a new pointer) 2019-04-22 17:37:41 +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 bd7790fd8c Update copyright year 2019-02-09 07:24:06 +01:00
Daniel Marjamäki a4406aca32 Fixed #7845 (Leak reported when ignoring return value of 'new', even if pointer saved by constructor) 2019-02-03 12:15:05 +01:00
Daniel Marjamäki 35e56942d1 Fixed #8116 ([False positive] Invalid memory leak detection when using reference.) 2019-01-09 20:38:32 +01:00
rikardfalkeborn 036fcf7827 Fix warnings (#1541)
* Fix reorder warning

* Fix override warnings

Clang warns about missing overrides.

* Remove unused function

* Add missing argument to function calls
2018-12-28 12:59:05 +01:00
Daniel Marjamäki 643ddd4caa Code cleanup 2018-12-17 18:54:32 +01:00
Daniel Marjamäki 3b328f9187 CheckMemoryLeak: Cleanup the old memory leaks check 2018-12-17 18:12:50 +01:00
Daniel Marjamäki 9d8e5e36eb Added a few TODO comments 2018-11-20 22:57:01 +01:00
Paul Fultz II 54453c5802 Fix FP when copying pointer to string (#1479) 2018-11-14 06:59:25 +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
rikardfalkeborn b3fef7957a Fix FP with fclose after comma (#7525) (#1407) 2018-10-01 11:58:27 +02:00
Daniel Marjamäki ddbe4b89b5 Renamed --debug to --debug-simplified 2018-08-05 11:19:20 +02:00
Daniel Marjamäki c7b787888a Refactoring: Use range for loops 2018-07-14 10:09:12 +02:00
Daniel Marjamäki 86872f81ba Refactorings in CheckMemoryLeak 2018-06-18 09:40:27 +02:00
Daniel Marjamäki 4ba9437bd5 Rename private member variables 2018-06-17 23:09:41 +02:00
Daniel Marjamäki 79ffe1d4fc Rename _tokenizer, _settings, _errorLogger 2018-06-16 16:10:28 +02:00
Daniel Marjamäki 45379a3aa6 Updated copyright year for modified files
[ci skip]
2018-06-10 22:07:21 +02:00
rebnridgway 42a65c5160 Fix crash bug #8579 (#1238)
* Added declaration for deletePrevious function

* Added definition for deletePrevious function

* Fixed crash from deleteThis invalidating pointers

The crash was caused by deleteThis() invalidating the pointer to a constant variable usage.  This happened when a usage followed an assignment.  This fixes bug #8579.

* Added tokensFront to match tokensBack

This means deletePrevious can set the list's front if necessary.

* Initialised tokensFront in appropriate places

* Switched to using default Token constructor

* Switched to using Token default constructor

* Switched to using default constructor for Token

* Added missing argument to Token constructor

* Changed to use default constructor for Tokens

* Switched to using default constructor for Tokens

* Switched to using default constructor for Token

* Added new test for deleting front Token

Also made sure to use the correct constructor for Token in other tests.

* Syntax error

* Replaced tokensFront and tokensBack with a struct

This decreases the size of the Token class for performance purposes.

* Replaced tokensFront and tokensBack with a struct

* Added tokensFrontBack to destructor

* Reworked to use TokensBackFront struct

Also ran astyle.

* Reworked to use TokenList's TokensFrontBack member

* Reworked to use TokensFrontBack struct

* Reworked to use TokensFrontBack struct

* Reworked to work with TokensFrontBack struct

* Removed unnecessary scope operator

* Added missing parentheses

* Fixed syntax error

* Removed unnecessary constructor

* Default constructor now 0-initialises everything

This is safer for not using a temporary TokensFrontBack object, and doesn't use delegating constructors which aren't supported yet.

* Fixed unsafe null check

* Added missing explicit keyword

* Fixing stylistic nits

Removed default constructor as it has been superseded by the single-argument constructor with a default argument value.
Renamed listEnds to tokensFrontBack.
Fixed if statement that was supposed to be adding safety but would actually cause a crash if tokensFrontBack was null.

* Fixing stylistic nits

Removed default constructor and replaced it with a single-argument constructor with a default value.

* Fixing stylistic nits

Renamed _listEnds to _tokensFrontBack.

* Fixing stylistic nits

Renamed _listEnds to _tokensFrontBack.
2018-05-25 07:15:05 +02:00
PKEuS 4d549553b0 Small optimization in checkmemoryleak.cpp: Allow passing literals to addtoken()
Ran AStyle
2018-05-14 10:15:50 +02:00
Daniel Marjamäki ca8e19c96d SymbolDatabase: Refactor SymbolDatabase: variable list 2018-04-28 09:38:33 +02:00
Daniel Marjamäki f336c2efe7 Refactoring; Renamed Scope::classStart and Scope::classEnd 2018-04-27 22:36:30 +02:00
Daniel Marjamäki a0906140a6 Suppressions: New extensible Suppressions xml format that allow more attributes. To start with it also allows symbolName. 2018-04-09 06:43:48 +02:00
PKEuS e2002db78d Replaced make_container by C++11 initializer lists 2018-04-08 23:03:44 +02:00
jrp2014 c5e14ad590 Refactor lib/checkmemoryleak.cpp 2018-04-05 21:41:31 +02:00
Daniel Marjamäki 3ad6c7ebce Refactoring, use early continue 2018-04-05 08:21:43 +02:00
Paul Fultz II 54de7b48c9 Fix false positive when constructing with curly brace (#1148) 2018-04-05 06:47:59 +02:00
jrp2014 b6504c70ca Improve constness 2018-04-04 21:51:31 +02:00
Daniel Marjamäki c4caee6b18 Updated copyright year 2018-01-14 15:37:52 +01:00
Dmitry-Me 82825ab721 Reorder checks, remove duplicate check 2017-12-20 13:16:59 +03:00
Ayaz Salikhov be2c65eb58 Simplify int vs bool 2017-10-08 07:54:39 +02:00
Ayaz Salikhov b8cd7dbb5c Use nullptr instead of 0 or NULL (#936) 2017-08-09 20:00:26 +02:00
Daniel Marjamäki f7514fcd2c Fix issue detected by Coverity, dereference tok3 and then check if its null. 2017-08-02 08:38:36 +02:00
Ayaz Salikhov 28aa939d69 iwyu - include what you use 2017-05-27 04:33:47 +02:00
PKEuS b1f4bd7504 Refactorization: Reimplemented Settings::_enabled as a bitfeld instead of std::set (#7995) 2017-04-11 11:49:26 +02:00
PKEuS 3c8f5b85ae Refactorization: Allocate Token::_values (ValueFlow information) dynamically, reducing size of each token by around 10% 2017-03-27 18:48:34 +02:00
PKEuS 2f0db369f0 Refactorization: Avoid construction of empty strings by using emptyString 2017-03-01 10:50:50 +01:00
Daniel Marjamäki 55d7ab65b9 Fixed #7918 (::exit() not recognized as noreturn) 2017-02-25 12:12:39 +01:00
amai2012 722fac391d Merge pull request #858 from simartin/ticket_7820
Ticket #7820: Properly report leaks with conditional deallocations followed by a return.
2017-01-18 09:31:49 +01:00
Simon Martin 9e76b21d7f Ticket #7820: Properly report leaks with conditional deallocations followed by a return. 2017-01-17 23:02:39 +01:00
Simon Martin ae939b1385 Teach CheckMemoryLeak about "alloc ; loop alloc ;" kinds of patterns. (#861)
Add an optional extended description…
2017-01-15 22:16:23 +01:00
Simon Martin c82d8a0d06 Ticket #7745: Simplify "alloc ; dealloc ;" blocks that we don't have any value for CheckMemoryLeak's analysis. (#860)
Add an optional extended description…
2017-01-15 22:14:37 +01:00
Matthias Krüger 064a4aa4cc improve constness of functions as per cppchecks suggestions. 2017-01-07 14:31:03 +01:00
Daniel Marjamäki 85ae3adeb2 Fixed #5144 (FP old memleak - tree structure) 2016-12-27 08:12:37 +01:00
PKEuS 2f6350a0d0 Refactorized Library 2016-12-06 14:09:28 +01:00
Simon Martin f816fb811e Ticket #7680: Properly handle ::delete during memory leak checks. 2016-09-10 14:55:50 +02:00
orbitcowboy 87409ea6b3 Running astyle; Improved testing of std::find; std.cfg: Added support for istream::read and ifstream::read. 2016-08-25 19:17:07 +02:00
Roberto Martelloni 01ee9ee5e6 CWE mapping of invalidLengthModifierError, leakUnsafeArgAlloc, nullPointerDefaultArg, nullPointerRedundantCheck, raceAfterInterlockedDecrement. 2016-08-24 15:37:14 +01:00
Daniel Marjamäki 54be403f64 Fixed #7026 (Cppcheck does not recognizes the use of a variabile inside lambda function) 2016-05-26 18:07:56 +02:00
Daniel Marjamäki 38741868b5 Fixed #3989 (false positive: memory leak (inline function)) 2016-05-26 17:42:27 +02:00
PKEuS 64d2fd2f57 Added new attribute "arg" to <alloc> and <dealloc> to specify the argument that is allocated/deallocated.
This fixes several issues with allocation functions in windows.cfg, such as HeapAlloc() and VirtualAllocEx() (#7503)
2016-05-22 17:19:14 +02:00
Daniel Marjamäki 5a1bea2a09 Fixed #7244 (False memory leak when POD is allocated with value initialization and pushed onto container) 2016-05-14 14:56:51 +02:00
Roberto Martelloni e89cd1b8a4 CWE mapping of unsafeClassCanLeak, zerodivcond, invalidPointerCast, redundantCopy, redundantAssignment, comparisonFunctionIsAlwaysTrueOrFalse, checkCastIntToCharAndBack, cstyleCast, passedByValue, clarifyCondition, exceptThrowInDestructor, exceptDeallocThrow, exceptRethrowCopy, catchExceptionByValue, fflushOnInputStream, seekOnAppendedFile, publicAllocationError
CWE mapping of unsafeClassCanLeak, zerodivcond, invalidPointerCast, redundantCopy, redundantAssignment, comparisonFunctionIsAlwaysTrueOrFalse, checkCastIntToCharAndBack, cstyleCast, passedByValue, clarifyCondition, exceptThrowInDestructor, exceptDeallocThrow, exceptRethrowCopy, catchExceptionByValue, fflushOnInputStream, seekOnAppendedFile, publicAllocationError
2016-04-12 19:29:40 +02:00
Alexander Mai 0533d7bf9c Run astyle + minor refactoring 2016-02-27 16:03:50 +01:00
PKEuS 4d01af3fe1 Fixed false positive in CheckMemoryLeakStructMember when returning a member that has been allocated (#7302). 2016-01-31 10:10:48 +01:00
Daniel Marjamäki 5e10e680da CWE: refactoring. use constants instead of magic numbers. 2016-01-25 20:01:48 +01:00
Daniel Marjamäki 0fb9ab7b4a Refactoring CheckMemoryLeakNoVar::checkForUnusedReturnValue(). use continue 2016-01-25 10:33:11 +01:00
Roberto Martelloni 5ce69da02d Mapped 26 errors to their CWEs ID. 2016-01-24 20:53:05 +00:00
Daniel Marjamäki 642cc57428 CheckMemoryLeak: Fix FP when overloaded new is used 2016-01-20 10:34:03 +01:00
Daniel Marjamäki d39097f52a Removed redundant null pointer check 2016-01-16 14:15:51 +01:00
Roberto Martelloni 5c1965a6e0 Added mapped CWE IDs to reportErr function calls 2016-01-05 20:37:39 +01:00
Alexander Mai d6fd4705cf #7255 segmentation fault (invalid code) in CheckMemoryLeakInFunction::getcode 2016-01-03 20:53:03 +01:00
Daniel Marjamäki 79e663dd6f Settings: Remove '_' prefix for public member variables 2016-01-03 16:18:17 +01:00
Lauri Nurmi 996c9244d8 Update copyright year to 2007-2016. 2016-01-01 15:34:45 +02:00
PKEuS 940d569980 Refactorization: Removed redundant %any% patterns. 2015-12-24 14:40:48 +01:00
Alexander Mai 86c9387987 #7182 crash: CheckMemoryLeak::functionReturnType() 2015-12-05 18:43:29 +01:00
Alexander Mai e69377d5a8 #7183 CheckClass::checkMemset() uint overflow. Plus some minor refactoring 2015-12-05 18:22:01 +01:00
PKEuS e8522c7883 Small refactorizations:
- #include cleanup
- Use std::array instead of std::vector
- Do not create a stringstream to concatenate 4 strings
- Use std::cout instead of printf
2015-11-29 10:56:44 +01:00
PKEuS 3b4160600d Fixed crash in CheckMemoryLeak::functionReturnType() for unary operator:: (#7172) 2015-11-27 11:18:40 +01:00