Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ namespace ValueFlow
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok, std::move(value), settings);
} else if (tok->str() == "NULL" || (tok->isCpp() && tok->str() == "nullptr")) {
} else if (tok->str() == "NULL" || ((tok->isCpp() || settings.standards.c >= Standards::C23) && tok->str() == "nullptr")) {
Value value(0);
if (!tok->isTemplateArg())
value.setKnown();
Expand Down
8 changes: 6 additions & 2 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ class TestNullPointer : public TestFixture {
CheckOptions() = default;
bool inconclusive = false;
bool cpp = true;
Standards::cstd_t cstd = Standards::CLatest;
};

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build();
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).c(options.cstd).build();

// Tokenize..
SimpleTokenizer tokenizer(settings1, *this, options.cpp);
Expand Down Expand Up @@ -1331,8 +1332,11 @@ class TestNullPointer : public TestFixture {
check(code); // C++ file => nullptr means NULL
ASSERT_EQUALS("[test.cpp:4:11]: (error) Null pointer dereference: i [nullPointer]\n", errout_str());

check(code, dinit(CheckOptions, $.cpp = false)); // C file => nullptr does not mean NULL
check(code, dinit(CheckOptions, $.cpp = false, $.cstd = Standards::C17)); // C17 file => nullptr does not mean NULL
ASSERT_EQUALS("", errout_str());

check(code, dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("[test.c:4:11]: (error) Null pointer dereference: i [nullPointer]\n", errout_str());
}

void nullpointer15() { // #3560
Expand Down
17 changes: 17 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5635,6 +5635,23 @@ class TestValueFlow : public TestFixture {
value = valueOfTok(code, ", 1");
ASSERT_EQUALS(0, value.intvalue);
ASSERT_EQUALS(false, value.isKnown());

// #13959
const Settings settingsOld = settings;
settings = settingsBuilder(settingsOld).c(Standards::C23).build();
code = "void f(int* p) {\n"
" if (p == nullptr)\n"
" return;\n"
" if (p) {}\n"
"}\n";
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
ASSERT_EQUALS(1, value.intvalue);
ASSERT_EQUALS(true, value.isKnown());

settings = settingsBuilder(settingsOld).c(Standards::C17).build();
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
ASSERT(value == ValueFlow::Value());
settings = settingsOld;
}

void valueFlowSizeofForwardDeclaredEnum() {
Expand Down
Loading