Skip to content

Commit 71c0679

Browse files
authored
fix #13098 & #11685 for C23 (#6996)
1 parent c36c8a6 commit 71c0679

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ $(libcppdir)/vf_iteratorinfer.o: lib/vf_iteratorinfer.cpp lib/config.h lib/error
726726
$(libcppdir)/vf_iterators.o: lib/vf_iterators.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_iterators.h lib/vf_settokenvalue.h lib/vfvalue.h
727727
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_iterators.cpp
728728

729-
$(libcppdir)/vf_number.o: lib/vf_number.cpp lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vf_number.h lib/vf_settokenvalue.h lib/vfvalue.h
729+
$(libcppdir)/vf_number.o: lib/vf_number.cpp lib/addoninfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vf_number.h lib/vf_settokenvalue.h lib/vfvalue.h
730730
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_number.cpp
731731

732732
$(libcppdir)/vf_pointeralias.o: lib/vf_pointeralias.cpp lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vf_pointeralias.h lib/vf_settokenvalue.h lib/vfvalue.h

lib/vf_number.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "vf_number.h"
2020

21+
#include "settings.h"
2122
#include "token.h"
2223
#include "tokenlist.h"
2324
#include "vfvalue.h"
@@ -36,7 +37,7 @@ namespace ValueFlow
3637
tok = valueFlowSetConstantValue(tok, settings);
3738
}
3839

39-
if (tokenlist.isCPP()) {
40+
if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) {
4041
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
4142
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
4243
ValueFlow::Value value(tok->str() == "true");

oss-fuzz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ $(libcppdir)/vf_iteratorinfer.o: ../lib/vf_iteratorinfer.cpp ../lib/config.h ../
413413
$(libcppdir)/vf_iterators.o: ../lib/vf_iterators.cpp ../lib/astutils.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vf_iterators.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
414414
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_iterators.cpp
415415

416-
$(libcppdir)/vf_number.o: ../lib/vf_number.cpp ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vf_common.h ../lib/vf_number.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
416+
$(libcppdir)/vf_number.o: ../lib/vf_number.cpp ../lib/addoninfo.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/settings.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vf_common.h ../lib/vf_number.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
417417
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_number.cpp
418418

419419
$(libcppdir)/vf_pointeralias.o: ../lib/vf_pointeralias.cpp ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vf_pointeralias.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h

test/testleakautovar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,16 @@ class TestLeakAutoVar : public TestFixture {
27692769
" return 'a';\n"
27702770
"}\n", true);
27712771
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: ptr\n", errout_str());
2772+
2773+
check("char malloc_memleak(void) {\n"
2774+
" bool flag = false;\n"
2775+
" char *ptr = malloc(10);\n"
2776+
" if (flag) {\n"
2777+
" free(ptr);\n"
2778+
" }\n"
2779+
" return 'a';\n"
2780+
"}\n", false);
2781+
ASSERT_EQUALS("[test.c:7]: (error) Memory leak: ptr\n", errout_str());
27722782
}
27732783

27742784
void test1() {

test/testtype.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,21 @@ class TestType : public TestFixture {
533533
" return false || f((unsigned short)75000.0);\n"
534534
"}\n", settingsDefault);
535535
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout_str()));
536+
537+
checkP("#define TEST(b, f) b ? 5000 : (unsigned short)f\n" // #11685
538+
"void f()\n"
539+
"{\n"
540+
" unsigned short u = TEST(true, 75000.0);\n"
541+
"}\n", settingsDefault, "test.c");
542+
ASSERT_EQUALS("", errout_str());
543+
544+
checkP("#define TEST(b, f) b ? 5000 : (unsigned short)f\n"
545+
"void f()\n"
546+
"{\n"
547+
" unsigned short u = TEST(false, 75000.0);\n"
548+
"}\n", settingsDefault, "test.c");
549+
ASSERT_EQUALS("[test.c:4]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout_str()));
550+
536551
}
537552

538553
void integerOverflow() { // #11794

test/testvalueflow.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ class TestValueFlow : public TestFixture {
489489
}
490490

491491
#define tokenValues(...) tokenValues_(__FILE__, __LINE__, __VA_ARGS__)
492-
std::list<ValueFlow::Value> tokenValues_(const char* file, int line, const char code[], const char tokstr[], const Settings *s = nullptr) {
492+
std::list<ValueFlow::Value> tokenValues_(const char* file, int line, const char code[], const char tokstr[], const Settings *s = nullptr, bool cpp = true) {
493493
SimpleTokenizer tokenizer(s ? *s : settings, *this);
494-
ASSERT_LOC(tokenizer.tokenize(code), file, line);
494+
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
495495
const Token *tok = Token::findmatch(tokenizer.tokens(), tokstr);
496496
return tok ? tok->values() : std::list<ValueFlow::Value>();
497497
}
@@ -523,9 +523,9 @@ class TestValueFlow : public TestFixture {
523523
return result;
524524
}
525525

526-
#define valueOfTok(code, tokstr) valueOfTok_(code, tokstr, __FILE__, __LINE__)
527-
ValueFlow::Value valueOfTok_(const char code[], const char tokstr[], const char* file, int line) {
528-
std::list<ValueFlow::Value> values = removeImpossible(tokenValues_(file, line, code, tokstr));
526+
#define valueOfTok(...) valueOfTok_(__FILE__, __LINE__, __VA_ARGS__)
527+
ValueFlow::Value valueOfTok_(const char* file, int line, const char code[], const char tokstr[], const Settings *s = nullptr, bool cpp = true) {
528+
std::list<ValueFlow::Value> values = removeImpossible(tokenValues_(file, line, code, tokstr, s, cpp));
529529
return values.size() == 1U && !values.front().isTokValue() ? values.front() : ValueFlow::Value();
530530
}
531531

@@ -557,6 +557,14 @@ class TestValueFlow : public TestFixture {
557557
ASSERT_EQUALS_DOUBLE(16, valueOfTok("x=(double)16;", "(").floatValue, 1e-5);
558558
ASSERT_EQUALS_DOUBLE(0.0625, valueOfTok("x=1/(double)16;", "/").floatValue, 1e-5);
559559

560+
const Settings settingsC23 = settingsBuilder().c(Standards::C23).build();
561+
ASSERT_EQUALS(1, valueOfTok("x=true;", "true", &settingsC23, false).intvalue);
562+
ASSERT_EQUALS(0, valueOfTok("x=false;", "false", &settingsC23, false).intvalue);
563+
564+
const Settings settingsC17 = settingsBuilder().c(Standards::C17).build();
565+
ASSERT(!valueOfTok("x=true;", "true", &settingsC17, false).isKnown());
566+
ASSERT(!valueOfTok("x=false;", "false", &settingsC17, false).isKnown());
567+
560568
// scope
561569
{
562570
const char code[] = "namespace N { enum E {e0,e1}; }\n"

0 commit comments

Comments
 (0)