Skip to content

Commit 60e80b6

Browse files
committed
Fixed cppcheck-opensource#5885 - fsanitize=undefined: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' in lib/tokenize.cpp.
1 parent e9144d1 commit 60e80b6

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

lib/mathlib.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,18 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
539539
return toString(d1 - d2);
540540
}
541541

542+
std::string MathLib::incdec(const std::string & var, const std::string & op)
543+
{
544+
if (op == "++")
545+
return MathLib::add(var, "1");
546+
else if (op == "--")
547+
return MathLib::subtract(var, "1");
548+
549+
throw InternalError(0, std::string("Unexpected operation '") + op + "' in MathLib::incdec(). Please report this to Cppcheck developers.");
550+
return "";
551+
552+
}
553+
542554
std::string MathLib::divide(const std::string &first, const std::string &second)
543555
{
544556
if (MathLib::isInt(first) && MathLib::isInt(second)) {

lib/mathlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class CPPCHECKLIB MathLib {
6262
static std::string multiply(const std::string & first, const std::string & second);
6363
static std::string divide(const std::string & first, const std::string & second);
6464
static std::string mod(const std::string & first, const std::string & second);
65+
static std::string incdec(const std::string & var, const std::string & op);
6566
static std::string calculate(const std::string & first, const std::string & second, char action);
6667

6768
static std::string sin(const std::string & tok);

lib/tokenize.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5198,21 +5198,6 @@ bool Tokenizer::simplifyFunctionReturn()
51985198
return ret;
51995199
}
52005200

5201-
5202-
static void incdec(std::string &value, const std::string &op)
5203-
{
5204-
int ivalue = 0;
5205-
std::istringstream istr(value);
5206-
istr >> ivalue;
5207-
if (op == "++")
5208-
++ivalue;
5209-
else if (op == "--")
5210-
--ivalue;
5211-
std::ostringstream ostr;
5212-
ostr << ivalue;
5213-
value = ostr.str();
5214-
}
5215-
52165201
void Tokenizer::simplifyVarDecl(bool only_k_r_fpar)
52175202
{
52185203
simplifyVarDecl(list.front(), nullptr, only_k_r_fpar);
@@ -6918,7 +6903,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
69186903
tok3->varId(valueVarId);
69196904
tok3->deleteNext();
69206905
}
6921-
incdec(value, op);
6906+
value = MathLib::incdec(value, op);
69226907
if (!Token::simpleMatch((*tok2)->tokAt(-2), "for (")) {
69236908
(*tok2)->tokAt(2)->str(value);
69246909
(*tok2)->tokAt(2)->varId(valueVarId);
@@ -6928,7 +6913,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
69286913

69296914
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) &&
69306915
!Token::Match(tok3->tokAt(3), "[.[]")) {
6931-
incdec(value, tok3->next()->str());
6916+
value = MathLib::incdec(value, tok3->next()->str());
69326917
(*tok2)->tokAt(2)->str(value);
69336918
(*tok2)->tokAt(2)->varId(valueVarId);
69346919
if (Token::Match(tok3, "[;{}] %any% %any% ;")) {

test/testmathlib.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestMathLib : public TestFixture {
4747
TEST_CASE(convert);
4848
TEST_CASE(naninf);
4949
TEST_CASE(isNullValue);
50+
TEST_CASE(incdec);
5051
}
5152

5253
void isGreater() const {
@@ -781,6 +782,25 @@ class TestMathLib : public TestFixture {
781782
ASSERT_EQUALS(false, MathLib::isNullValue("garbage"));
782783
ASSERT_EQUALS(false, MathLib::isNullValue("UL"));
783784
}
785+
786+
void incdec() {
787+
// increment
788+
{
789+
MathLib::biguint num = ~10U;
790+
const std::string op = "++";
791+
const std::string strNum = MathLib::incdec(MathLib::toString(num), op);
792+
const MathLib::biguint incrementedNum = MathLib::toULongNumber(strNum);
793+
ASSERT_EQUALS(num + 1U, incrementedNum);
794+
}
795+
// decrement
796+
{
797+
MathLib::biguint num = ~10U;
798+
const std::string op = "--";
799+
const std::string strNum = MathLib::incdec(MathLib::toString(num), op);
800+
const MathLib::biguint decrementedNum = MathLib::toULongNumber(strNum);
801+
ASSERT_EQUALS(num - 1U, decrementedNum);
802+
}
803+
}
784804
};
785805

786806
REGISTER_TEST(TestMathLib)

0 commit comments

Comments
 (0)