From 87409ea6b3e7ac67ae39786bffa0e919757c2a85 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Thu, 25 Aug 2016 19:17:07 +0200 Subject: [PATCH] Running astyle; Improved testing of std::find; std.cfg: Added support for istream::read and ifstream::read. --- cfg/std.cfg | 13 ++++++++++ lib/checkio.cpp | 2 +- lib/checkmemoryleak.cpp | 8 +++---- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 6 ++--- lib/checkstl.cpp | 2 +- test/cfg/std.cpp | 51 +++++++++++++++++++++++++++++++++++----- 7 files changed, 68 insertions(+), 16 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index 3c3cf6acd..ee0bfeef9 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -3989,6 +3989,18 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + + + false + + + + + + + + @@ -4215,6 +4227,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 3fbc9e061..e450bf036 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -40,7 +40,7 @@ static const CWE CWE664(664U); // Improper Control of a Resource Through its Li static const CWE CWE685(685U); // Function Call With Incorrect Number of Arguments static const CWE CWE686(686U); // Function Call With Incorrect Argument Type static const CWE CWE687(687U); // Function Call With Incorrectly Specified Argument Value -static const CWE CWE704(704U); // Incorrect Type Conversion or Cast +static const CWE CWE704(704U); // Incorrect Type Conversion or Cast static const CWE CWE910(910U); // Use of Expired File Descriptor //--------------------------------------------------------------------------- diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 65e770b10..f855dbffc 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -39,10 +39,10 @@ namespace { } // CWE ID used: -static const CWE CWE398(398U); // Indicator of Poor Code Quality -static const CWE CWE401(401U); // Improper Release of Memory Before Removing Last Reference ('Memory Leak') -static const CWE CWE771(771U); // Missing Reference to Active Allocated Resource -static const CWE CWE772(772U); // Missing Release of Resource after Effective Lifetime +static const CWE CWE398(398U); // Indicator of Poor Code Quality +static const CWE CWE401(401U); // Improper Release of Memory Before Removing Last Reference ('Memory Leak') +static const CWE CWE771(771U); // Missing Reference to Active Allocated Resource +static const CWE CWE772(772U); // Missing Release of Resource after Effective Lifetime /** * Count function parameters diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 282a87462..6c56f2fca 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -30,7 +30,7 @@ namespace { CheckNullPointer instance; } -static const CWE CWE476(476U); // NULL Pointer Dereference +static const CWE CWE476(476U); // NULL Pointer Dereference //--------------------------------------------------------------------------- diff --git a/lib/checkother.cpp b/lib/checkother.cpp index edf366299..a40ca132a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -36,8 +36,8 @@ namespace { static const struct CWE CWE128(128U); // Wrap-around Error static const struct CWE CWE131(131U); // Incorrect Calculation of Buffer Size static const struct CWE CWE197(197U); // Numeric Truncation Error -static const struct CWE CWE362(362U); // Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') -static const struct CWE CWE369(369U); // Divide By Zero +static const struct CWE CWE362(362U); // Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') +static const struct CWE CWE369(369U); // Divide By Zero static const struct CWE CWE398(398U); // Indicator of Poor Code Quality static const struct CWE CWE475(475U); // Undefined Behavior for Input to API static const struct CWE CWE482(482U); // Comparing instead of Assigning @@ -50,7 +50,7 @@ static const struct CWE CWE687(687U); // Function Call With Incorrectly Specif static const struct CWE CWE688(688U); // Function Call With Incorrect Variable or Reference as Argument static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast static const struct CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior -static const struct CWE CWE768(768U); // Incorrect Short Circuit Evaluation +static const struct CWE CWE768(768U); // Incorrect Short Circuit Evaluation static const struct CWE CWE783(783U); // Operator Precedence Logic Error //---------------------------------------------------------------------------------- diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 8b2f7f69d..ca8a6fc04 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1254,7 +1254,7 @@ void CheckStl::autoPointerError(const Token *tok) reportError(tok, Severity::style, "useAutoPointerCopy", "Copying 'auto_ptr' pointer to another does not create two equal objects since one has lost its ownership of the pointer.\n" "'std::auto_ptr' has semantics of strict ownership, meaning that the 'auto_ptr' instance is the sole entity responsible for the object's lifetime. If an 'auto_ptr' is copied, the source looses the reference.", - CWE398, false); + CWE398, false); } void CheckStl::autoPointerContainerError(const Token *tok) diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index ecfe59e35..2e56b09f2 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include void bufferAccessOutOfBounds(void) { @@ -2984,17 +2986,42 @@ void uninitvar_find(std::string s) (void)s.find(pc,0); // cppcheck-suppress uninitvar (void)s.find(pc,pos); + // cppcheck-suppress uninitvar + (void)s.find("test",pos); // testing of size_t find (char c, size_t pos = 0) const; char c; // cppcheck-suppress uninitvar (void)s.find(c,pos); - /* - // testing of size_t find (const char* pc, size_t pos, size_t n) const; - size_t n; - // cppcheck-suppress uninitvar - (void)s.find(pc,pos,n); // #6991 - */ + + // testing of size_t find (const char* pc, size_t pos, size_t n) const; + size_t n; + // cppcheck-suppress uninitvar + (void)s.find(pc,pos,n); // #6991 + // cppcheck-suppress uninitvar + (void)s.find("test",pos,n); + // cppcheck-suppress uninitvar + (void)s.find("test",1,n); + // cppcheck-suppress uninitvar + (void)s.find("test",pos,1); + // cppcheck-suppress uninitvar + (void)s.find(pc,1,1); +} + +void uninivar_ifstream_read(std::ifstream &f) +{ + int size; + char buffer[10]; + // cppcheck-suppress uninitvar + f.read(buffer, size); +} + +void uninivar_istream_read(std::istream &f) +{ + int size; + char buffer[10]; + // cppcheck-suppress uninitvar + f.read(buffer, size); } void invalidFunctionArgBool_abs(bool b, double x, double y) @@ -3015,6 +3042,18 @@ void ignoredReturnValue_abs(int i) std::abs(-199); } +void nullPointer_ifstream_read(std::ifstream &f) +{ + // cppcheck-suppress nullPointer + f.read(NULL, 10); +} + +void nullPointer_istream_read(std::istream &f) +{ + // cppcheck-suppress nullPointer + f.read(NULL, 10); +} + void nullPointer_asctime(void) { struct tm *tm = 0;