Skip to content

added -fsanitizer=integer to UBSAN #2922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 .github/workflows/ubsan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- name: Build
run: |
CC=clang CXX=clang++ CXXFLAGS="-fsanitize=undefined -fsanitize=nullability -O1 -g3 -DCPPCHK_GLIBCXX_DEBUG" make cppcheck testrunner -j$(nproc) USE_Z3=yes HAVE_RULES=yes MATCHCOMPILER=yes
CC=clang CXX=clang++ CXXFLAGS="-fsanitize=undefined -fsanitize=nullability -fsanitize=integer -O1 -g3 -DCPPCHK_GLIBCXX_DEBUG" make cppcheck testrunner -j$(nproc) USE_Z3=yes HAVE_RULES=yes MATCHCOMPILER=yes

- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->checkAllConfigurations = true;

if (mSettings->force)
mSettings->maxConfigs = ~0U;
mSettings->maxConfigs = static_cast<int>(~0U);

else if ((def || mSettings->preprocessOnly) && !maxconfigs)
mSettings->maxConfigs = 1U;
Expand Down
2 changes: 1 addition & 1 deletion cmake/dynamic_analyzer_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if(ANALYZE_UNDEFINED)
add_compile_options(-fno-sanitize-recover=all)
add_compile_options(-fno-omit-frame-pointer)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fsanitize=nullability -fsanitize=integer")
endif()

if(ANALYZE_DATAFLOW)
Expand Down
2 changes: 1 addition & 1 deletion lib/mathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
biguint ret = 0;
std::istringstream istr(str);
istr >> ret;
return ret;
return static_cast<bigint>(ret);
}

// in-place conversion of (sub)string to double. Requires no heap.
Expand Down
8 changes: 4 additions & 4 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static ValueFlow::Value castValue(ValueFlow::Value value, const ValueType::Sign
const MathLib::biguint one = 1;
value.intvalue &= (one << bit) - 1;
if (sign == ValueType::Sign::SIGNED && value.intvalue & (one << (bit - 1))) {
value.intvalue |= ~((one << bit) - 1ULL);
value.intvalue |= static_cast<long long>(~((one << bit) - 1ULL));
}
}
return value;
Expand Down Expand Up @@ -792,7 +792,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
if (value1.tokvalue->tokType() == Token::eString) {
const std::string s = value1.tokvalue->strValue();
const MathLib::bigint index = value2.intvalue;
if (index == s.size()) {
if (static_cast<std::string::size_type>(index) == s.size()) {
result.intvalue = 0;
setTokenValue(parent, result, settings);
} else if (index >= 0 && index < s.size()) {
Expand Down Expand Up @@ -3997,9 +3997,9 @@ static std::list<ValueFlow::Value> truncateValues(std::list<ValueFlow::Value> va
if (value.isIntValue() && sz > 0 && sz < 8) {
const MathLib::biguint unsignedMaxValue = (1ULL << (sz * 8)) - 1ULL;
const MathLib::biguint signBit = 1ULL << (sz * 8 - 1);
value.intvalue &= unsignedMaxValue;
value.intvalue &= static_cast<long long>(unsignedMaxValue);
if (valueType->sign == ValueType::Sign::SIGNED && (value.intvalue & signBit))
value.intvalue |= ~unsignedMaxValue;
value.intvalue |= static_cast<long long>(~unsignedMaxValue);
}
}
return values;
Expand Down
20 changes: 10 additions & 10 deletions test/testclangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ class TestClangImport: public TestFixture {
GET_SYMBOL_DB(clang);

// Enum scope and type
ASSERT_EQUALS(3, db->scopeList.size());
ASSERT_EQUALS(3UL, db->scopeList.size());
const Scope &enumScope = db->scopeList.back();
ASSERT_EQUALS(Scope::ScopeType::eEnum, enumScope.type);
ASSERT_EQUALS("abc", enumScope.className);
Expand All @@ -1039,7 +1039,7 @@ class TestClangImport: public TestFixture {
GET_SYMBOL_DB(clang);

// There is a function foo that has 2 arguments
ASSERT_EQUALS(1, db->functionScopes.size());
ASSERT_EQUALS(1UL, db->functionScopes.size());
const Scope *scope = db->functionScopes[0];
const Function *func = scope->function;
ASSERT_EQUALS(2, func->argCount());
Expand All @@ -1058,12 +1058,12 @@ class TestClangImport: public TestFixture {
GET_SYMBOL_DB(clang);

// There is a function foo that has 2 arguments
ASSERT_EQUALS(1, db->functionScopes.size());
ASSERT_EQUALS(1UL, db->functionScopes.size());
const Scope *scope = db->functionScopes[0];
const Function *func = scope->function;
ASSERT_EQUALS(2, func->argCount());
ASSERT_EQUALS(0, (long long)func->getArgumentVar(0)->nameToken());
ASSERT_EQUALS(0, (long long)func->getArgumentVar(1)->nameToken());
ASSERT(func->getArgumentVar(0)->nameToken() == nullptr);
ASSERT(func->getArgumentVar(1)->nameToken() == nullptr);
}

void symbolDatabaseFunction3() { // #9640
Expand All @@ -1075,7 +1075,7 @@ class TestClangImport: public TestFixture {
GET_SYMBOL_DB(clang);

// There is a function foo that has 2 arguments
ASSERT_EQUALS(1, db->functionScopes.size());
ASSERT_EQUALS(1UL, db->functionScopes.size());
const Scope *scope = db->functionScopes[0];
const Function *func = scope->function;
ASSERT_EQUALS(2, func->argCount());
Expand All @@ -1090,8 +1090,8 @@ class TestClangImport: public TestFixture {
GET_SYMBOL_DB(clang);

// There is a function f that is const
ASSERT_EQUALS(2, db->scopeList.size());
ASSERT_EQUALS(1, db->scopeList.back().functionList.size());
ASSERT_EQUALS(2UL, db->scopeList.size());
ASSERT_EQUALS(1UL, db->scopeList.back().functionList.size());
const Function &func = db->scopeList.back().functionList.back();
ASSERT(func.isConst());
}
Expand Down Expand Up @@ -1175,7 +1175,7 @@ class TestClangImport: public TestFixture {
ASSERT(!!tok);
tok = tok->next();
ASSERT(tok->hasKnownIntValue());
ASSERT_EQUALS(44, tok->getKnownIntValue());
ASSERT_EQUALS(44LL, tok->getKnownIntValue());
}

void valueFlow2() {
Expand All @@ -1191,7 +1191,7 @@ class TestClangImport: public TestFixture {
ASSERT(!!tok);
tok = tok->next();
ASSERT(tok->hasKnownIntValue());
ASSERT_EQUALS(10, tok->getKnownIntValue());
ASSERT_EQUALS(10LL, tok->getKnownIntValue());
}

void valueType1() {
Expand Down
18 changes: 9 additions & 9 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argvsp[] = {"cppcheck", "-rp=C:/foo;C:\\bar", "file.cpp"};
ASSERT(defParser.parseFromArgs(3, argvsp));
ASSERT_EQUALS(true, settings.relativePaths);
ASSERT_EQUALS(2, settings.basePaths.size());
ASSERT_EQUALS(2UL, settings.basePaths.size());
ASSERT_EQUALS("C:/foo", settings.basePaths[0]);
ASSERT_EQUALS("C:/bar", settings.basePaths[1]);

Expand All @@ -288,7 +288,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argvlp[] = {"cppcheck", "--relative-paths=C:/foo;C:\\bar", "file.cpp"};
ASSERT(defParser.parseFromArgs(3, argvlp));
ASSERT_EQUALS(true, settings.relativePaths);
ASSERT_EQUALS(2, settings.basePaths.size());
ASSERT_EQUALS(2UL, settings.basePaths.size());
ASSERT_EQUALS("C:/foo", settings.basePaths[0]);
ASSERT_EQUALS("C:/bar", settings.basePaths[1]);
}
Expand Down Expand Up @@ -637,7 +637,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-j", "3", "file.cpp"};
settings.jobs = 0;
ASSERT(defParser.parseFromArgs(4, argv));
ASSERT_EQUALS(3, settings.jobs);
ASSERT_EQUALS(3U, settings.jobs);
}

void jobsMissingCount() {
Expand Down Expand Up @@ -945,7 +945,7 @@ class TestCmdlineParser : public TestFixture {
CmdLineParser parser(&settings);
// Fails since no ignored path given
ASSERT_EQUALS(false, parser.parseFromArgs(2, argv));
ASSERT_EQUALS(0, parser.getIgnoredPaths().size());
ASSERT_EQUALS(0UL, parser.getIgnoredPaths().size());
}

/*
Expand Down Expand Up @@ -982,7 +982,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-i", "src", "-i", "module", "file.cpp"};
CmdLineParser parser(&settings);
ASSERT(parser.parseFromArgs(6, argv));
ASSERT_EQUALS(2, parser.getIgnoredPaths().size());
ASSERT_EQUALS(2UL, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src/", parser.getIgnoredPaths()[0]);
ASSERT_EQUALS("module/", parser.getIgnoredPaths()[1]);
}
Expand All @@ -992,7 +992,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-ifoo.cpp", "file.cpp"};
CmdLineParser parser(&settings);
ASSERT(parser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS(1UL, parser.getIgnoredPaths().size());
ASSERT_EQUALS("foo.cpp", parser.getIgnoredPaths()[0]);
}
*/
Expand All @@ -1001,7 +1001,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-isrc/foo.cpp", "file.cpp"};
CmdLineParser parser(&settings);
ASSERT(parser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, parser.getIgnoredPaths().size());
ASSERT_EQUALS(1UL, parser.getIgnoredPaths().size());
ASSERT_EQUALS("src/foo.cpp", parser.getIgnoredPaths()[0]);
}

Expand All @@ -1024,7 +1024,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-U_WIN32", "file.cpp"};
settings = Settings();
ASSERT(defParser.parseFromArgs(3, argv));
ASSERT_EQUALS(1, settings.userUndefs.size());
ASSERT_EQUALS(1UL, settings.userUndefs.size());
ASSERT(settings.userUndefs.find("_WIN32") != settings.userUndefs.end());
}

Expand All @@ -1033,7 +1033,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "-U_WIN32", "-UNODEBUG", "file.cpp"};
settings = Settings();
ASSERT(defParser.parseFromArgs(4, argv));
ASSERT_EQUALS(2, settings.userUndefs.size());
ASSERT_EQUALS(2UL, settings.userUndefs.size());
ASSERT(settings.userUndefs.find("_WIN32") != settings.userUndefs.end());
ASSERT(settings.userUndefs.find("NODEBUG") != settings.userUndefs.end());
}
Expand Down
16 changes: 8 additions & 8 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class TestErrorLogger : public TestFixture {
void ErrorMessageConstruct() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false);
ASSERT_EQUALS(1, msg.callStack.size());
ASSERT_EQUALS(1UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
Expand All @@ -123,7 +123,7 @@ class TestErrorLogger : public TestFixture {
void ErrorMessageConstructLocations() const {
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false);
ASSERT_EQUALS(2, msg.callStack.size());
ASSERT_EQUALS(2UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
Expand All @@ -133,7 +133,7 @@ class TestErrorLogger : public TestFixture {
void ErrorMessageVerbose() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(1, msg.callStack.size());
ASSERT_EQUALS(1UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
Expand All @@ -143,7 +143,7 @@ class TestErrorLogger : public TestFixture {
void ErrorMessageVerboseLocations() const {
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, msg.callStack.size());
ASSERT_EQUALS(2UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
Expand All @@ -153,7 +153,7 @@ class TestErrorLogger : public TestFixture {
void CustomFormat() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(1, msg.callStack.size());
ASSERT_EQUALS(1UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("foo.cpp:5,error,errorId,Programming error.", msg.toString(false, "{file}:{line},{severity},{id},{message}"));
Expand All @@ -163,7 +163,7 @@ class TestErrorLogger : public TestFixture {
void CustomFormat2() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(1, msg.callStack.size());
ASSERT_EQUALS(1UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("Programming error. - foo.cpp(5):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
Expand All @@ -174,7 +174,7 @@ class TestErrorLogger : public TestFixture {
// Check that first location from location stack is used in template
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, msg.callStack.size());
ASSERT_EQUALS(2UL, msg.callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("Programming error. - bar.cpp(8):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
Expand Down Expand Up @@ -306,7 +306,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("[]:;,()", msg2.callStack.front().getfile(false));
ASSERT_EQUALS(":/,;", msg2.callStack.front().getOrigFile(false));
ASSERT_EQUALS(654, msg2.callStack.front().line);
ASSERT_EQUALS(33, msg2.callStack.front().column);
ASSERT_EQUALS(33U, msg2.callStack.front().column);
ASSERT_EQUALS("abcd:/,", msg2.callStack.front().getinfo());
}

Expand Down
Loading