Skip to content

Commit 5f4423f

Browse files
Fix #13959 Support nullptr in C23 (danmar#7621)
1 parent 8e62a24 commit 5f4423f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/vf_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ namespace ValueFlow
150150
if (!tok->isTemplateArg())
151151
value.setKnown();
152152
setTokenValue(tok, std::move(value), settings);
153-
} else if (tok->str() == "NULL" || (tok->isCpp() && tok->str() == "nullptr")) {
153+
} else if (tok->str() == "NULL" || ((tok->isCpp() || settings.standards.c >= Standards::C23) && tok->str() == "nullptr")) {
154154
Value value(0);
155155
if (!tok->isTemplateArg())
156156
value.setKnown();

test/testnullpointer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,13 @@ class TestNullPointer : public TestFixture {
183183
CheckOptions() = default;
184184
bool inconclusive = false;
185185
bool cpp = true;
186+
Standards::cstd_t cstd = Standards::CLatest;
186187
};
187188

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

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

1334-
check(code, dinit(CheckOptions, $.cpp = false)); // C file => nullptr does not mean NULL
1335+
check(code, dinit(CheckOptions, $.cpp = false, $.cstd = Standards::C17)); // C17 file => nullptr does not mean NULL
13351336
ASSERT_EQUALS("", errout_str());
1337+
1338+
check(code, dinit(CheckOptions, $.cpp = false));
1339+
ASSERT_EQUALS("[test.c:4:11]: (error) Null pointer dereference: i [nullPointer]\n", errout_str());
13361340
}
13371341

13381342
void nullpointer15() { // #3560

test/testvalueflow.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,6 +5635,23 @@ class TestValueFlow : public TestFixture {
56355635
value = valueOfTok(code, ", 1");
56365636
ASSERT_EQUALS(0, value.intvalue);
56375637
ASSERT_EQUALS(false, value.isKnown());
5638+
5639+
// #13959
5640+
const Settings settingsOld = settings;
5641+
settings = settingsBuilder(settingsOld).c(Standards::C23).build();
5642+
code = "void f(int* p) {\n"
5643+
" if (p == nullptr)\n"
5644+
" return;\n"
5645+
" if (p) {}\n"
5646+
"}\n";
5647+
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
5648+
ASSERT_EQUALS(1, value.intvalue);
5649+
ASSERT_EQUALS(true, value.isKnown());
5650+
5651+
settings = settingsBuilder(settingsOld).c(Standards::C17).build();
5652+
value = valueOfTok(code, "p ) { }", &settings, /*cpp*/ false);
5653+
ASSERT(value == ValueFlow::Value());
5654+
settings = settingsOld;
56385655
}
56395656

56405657
void valueFlowSizeofForwardDeclaredEnum() {

0 commit comments

Comments
 (0)