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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ $(libcppdir)/vf_iteratorinfer.o: lib/vf_iteratorinfer.cpp lib/config.h lib/error
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_iterators.cpp

$(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
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_number.cpp

$(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
Expand Down
3 changes: 2 additions & 1 deletion lib/vf_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "vf_number.h"

#include "settings.h"
#include "token.h"
#include "tokenlist.h"
#include "vfvalue.h"
Expand All @@ -36,7 +37,7 @@ namespace ValueFlow
tok = valueFlowSetConstantValue(tok, settings);
}

if (tokenlist.isCPP()) {
if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) {
Comment thread
danmar marked this conversation as resolved.
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
if (tok->isName() && !tok->varId() && Token::Match(tok, "false|true")) {
ValueFlow::Value value(tok->str() == "true");
Expand Down
2 changes: 1 addition & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ $(libcppdir)/vf_iteratorinfer.o: ../lib/vf_iteratorinfer.cpp ../lib/config.h ../
$(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
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_iterators.cpp

$(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
$(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
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_number.cpp

$(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
Expand Down
10 changes: 10 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,16 @@ class TestLeakAutoVar : public TestFixture {
" return 'a';\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: ptr\n", errout_str());

check("char malloc_memleak(void) {\n"
" bool flag = false;\n"
" char *ptr = malloc(10);\n"
" if (flag) {\n"
" free(ptr);\n"
" }\n"
" return 'a';\n"
"}\n", false);
ASSERT_EQUALS("[test.c:7]: (error) Memory leak: ptr\n", errout_str());
}

void test1() {
Expand Down
15 changes: 15 additions & 0 deletions test/testtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,21 @@ class TestType : public TestFixture {
" return false || f((unsigned short)75000.0);\n"
"}\n", settingsDefault);
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout_str()));

checkP("#define TEST(b, f) b ? 5000 : (unsigned short)f\n" // #11685
"void f()\n"
"{\n"
" unsigned short u = TEST(true, 75000.0);\n"
"}\n", settingsDefault, "test.c");
ASSERT_EQUALS("", errout_str());

checkP("#define TEST(b, f) b ? 5000 : (unsigned short)f\n"
"void f()\n"
"{\n"
" unsigned short u = TEST(false, 75000.0);\n"
"}\n", settingsDefault, "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout_str()));

}

void integerOverflow() { // #11794
Expand Down
18 changes: 13 additions & 5 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ class TestValueFlow : public TestFixture {
}

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

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

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

const Settings settingsC23 = settingsBuilder().c(Standards::C23).build();
ASSERT_EQUALS(1, valueOfTok("x=true;", "true", &settingsC23, false).intvalue);
ASSERT_EQUALS(0, valueOfTok("x=false;", "false", &settingsC23, false).intvalue);

const Settings settingsC17 = settingsBuilder().c(Standards::C17).build();
ASSERT(!valueOfTok("x=true;", "true", &settingsC17, false).isKnown());
ASSERT(!valueOfTok("x=false;", "false", &settingsC17, false).isKnown());

// scope
{
const char code[] = "namespace N { enum E {e0,e1}; }\n"
Expand Down