diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 601d33f72..a9d6648cf 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -363,6 +363,25 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str) return static_cast(doubleval); } + if (str[0] == '\'' && str.size() >= 3U && str[str.size()-1U] == '\'') { + char c; + if (str.size() == 3U && + str[0] == '\'' && + str[2] == '\'') + c = str[1]; + else if (str == "\'\\0\'") + c = '\0'; + else if (str == "\'\\n\'") + c = '\n'; + else if (str == "\'\\r\'") + c = '\r'; + else if (str == "\'\\t\'") + c = '\t'; + else + throw InternalError(0, "MathLib::toLongNumber: Unhandled char constant " + str); + return c & 0xff; + } + bigint ret = 0; std::istringstream istr(str); istr >> ret; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 185d1b14e..b6ca17460 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1023,7 +1023,8 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) } if (Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) { - tok->str(MathLib::toString(tok->str()[1] & 0xff)); + int c = MathLib::toLongNumber(tok->str()); + tok->str(MathLib::toString(c)); } if (tok->isNumber()) { diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 2e1918f44..7f71dfed3 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -594,29 +594,10 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value) static void valueFlowNumber(TokenList *tokenlist) { for (Token *tok = tokenlist->front(); tok; tok = tok->next()) { - if (tok->isNumber() && MathLib::isInt(tok->str())) { + if ((tok->isNumber() && MathLib::isInt(tok->str())) || (tok->tokType() == Token::eChar)) { ValueFlow::Value value(MathLib::toLongNumber(tok->str())); value.setKnown(); setTokenValue(tok, value); - } else if (tok->tokType() == Token::eChar) { - char c; - if (tok->str() == "\'\\n\'") - c = '\n'; - else if (tok->str() == "\'\\r\'") - c = '\r'; - else if (tok->str() == "\'\\t\'") - c = '\t'; - else if (tok->str().size() == 3U && - tok->str()[0] == '\'' && - (tok->str()[1] & 0x80) == 0 && - tok->str()[2] == '\'') - c = tok->str()[1]; - else - continue; - - ValueFlow::Value value(c); - value.setKnown(); - setTokenValue(tok, value); } } diff --git a/test/testobsolescentfunctions.cpp b/test/testobsolescentfunctions.cpp index 4911911c8..f14ab07ba 100644 --- a/test/testobsolescentfunctions.cpp +++ b/test/testobsolescentfunctions.cpp @@ -104,7 +104,7 @@ private: check("void f()\n" "{\n" " struct hostent *hp;\n" - " if(!hp = gethostbyname('127.0.0.1')) {\n" + " if(!hp = gethostbyname(\"127.0.0.1\")) {\n" " exit(1);\n" " }\n" "}"); @@ -115,7 +115,7 @@ private: check("void f()\n" "{\n" " long addr;\n" - " addr = inet_addr('127.0.0.1');\n" + " addr = inet_addr(\"127.0.0.1\");\n" " if(!hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) {\n" " exit(1);\n" " }\n" @@ -164,7 +164,7 @@ private: check("const char f()\n" "{\n" - " const char var[6] = 'index';\n" + " const char var[6] = \"index\";\n" " const char i = index(var, 0);\n" " return i;\n" "}"); @@ -188,7 +188,7 @@ private: check("void f()\n" "{\n" - " const char var[7] = 'rindex';\n" + " const char var[7] = \"rindex\";\n" " print(rindex(var, 0));\n" "}"); ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'rindex' called. It is recommended to use the function 'strrchr' instead.\n", errout.str()); diff --git a/test/teststring.cpp b/test/teststring.cpp index ff432d8f9..28404351e 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -307,10 +307,10 @@ private: // Ticket #5734 check("int foo(char c) {\n" - "return c == '42';}", "test.cpp"); + "return c == '4';}", "test.cpp"); ASSERT_EQUALS("", errout.str()); check("int foo(char c) {\n" - "return c == '42';}", "test.c"); + "return c == '4';}", "test.c"); ASSERT_EQUALS("", errout.str()); check("int foo(char c) {\n" "return c == \"42\"[0];}", "test.cpp");