Fixed #2470 (False negative: Possible null pointer dereference (C++0x keyword 'nullptr'))

This commit is contained in:
PKEuS 2011-10-16 12:54:58 +02:00 committed by Daniel Marjamäki
parent 32496fb009
commit f8074b71d2
6 changed files with 43 additions and 12 deletions

View File

@ -385,6 +385,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->c99 = true;
}
else if (strcmp(argv[i], "--std=c++11") == 0) {
_settings->cpp11 = true;
}
// Output formatter
else if (strcmp(argv[i], "--template") == 0) {
// "--template path/"

View File

@ -19,12 +19,7 @@
#include "settings.h"
#include "path.h"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <cctype> // std::isdigit, std::isalnum, etc
#include <set>
#include <stack>

View File

@ -21,7 +21,6 @@
#include <list>
#include <string>
#include <istream>
#include <set>
#include "suppressions.h"
@ -189,6 +188,9 @@ public:
/** Code is C99 standard - it is not compatible with previous versions */
bool c99;
/** Code follows C++11 standard - it is not compatible with previous versions */
bool cpp11;
/** size of standard types */
unsigned int sizeof_bool;
unsigned int sizeof_short;

View File

@ -31,18 +31,13 @@
#include "path.h"
#include "symboldatabase.h"
#include <locale>
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include <list>
#include <cassert>
#include <algorithm>
#include <cctype>
#include <stack>
#include <stdexcept> // for std::runtime_error
//---------------------------------------------------------------------------
@ -2405,6 +2400,13 @@ bool Tokenizer::tokenize(std::istream &code,
removeRedundantSemicolons();
if (_settings->cpp11) {
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (tok->str() == "nullptr")
tok->str("0");
}
}
return validate();
}
//---------------------------------------------------------------------------

View File

@ -77,6 +77,7 @@ private:
TEST_CASE(reportProgressTest); // "Test" suffix to avoid hiding the parent's reportProgress
TEST_CASE(stdposix);
TEST_CASE(stdc99);
TEST_CASE(stdcpp11);
TEST_CASE(suppressionsOld); // TODO: Create and test real suppression file
TEST_CASE(suppressions);
TEST_CASE(suppressionsNoFile);
@ -563,6 +564,15 @@ private:
ASSERT(settings.c99);
}
void stdcpp11() {
REDIRECT;
const char *argv[] = {"cppcheck", "--std=c++11", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT(settings.cpp11);
}
void suppressionsOld() {
// TODO: Fails because there is no suppr.txt file!
REDIRECT;

View File

@ -45,6 +45,7 @@ private:
TEST_CASE(nullpointer9);
TEST_CASE(nullpointer10);
TEST_CASE(nullpointer11); // ticket #2812
TEST_CASE(nullpointer12); // ticket #2470
TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it
TEST_CASE(nullConstantDereference); // Dereference NULL constant
TEST_CASE(gcc_statement_expression); // Don't crash
@ -54,13 +55,14 @@ private:
TEST_CASE(scanf_with_invalid_va_argument);
}
void check(const char code[], bool inconclusive = false) {
void check(const char code[], bool inconclusive = false, bool cpp11 = false) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.inconclusive = inconclusive;
settings.cpp11 = cpp11;
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -1030,6 +1032,22 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str());
}
void nullpointer12() { // ticket #2470
check("int foo()\n"
"{\n"
" int* i = nullptr;\n"
" return *i;\n"
"}\n", false, true); // Check as C++11 code
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
check("int foo(int nullptr)\n"
"{\n"
" int* i = nullptr;\n"
" return *i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Check if pointer is null and the dereference it
void pointerCheckAndDeRef() {
check("void foo(char *p) {\n"