diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 1060fb2fa..0cc9903e0 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -113,7 +113,7 @@ void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const Val void CheckType::checkIntegerOverflow() { // unknown sizeof(int) => can't run this checker - if (_settings->platformType == Settings::Unspecified) + if (_settings->platformType == Settings::Unspecified || _settings->int_bit >= 64) return; // max int value according to platform settings. @@ -136,8 +136,14 @@ void CheckType::checkIntegerOverflow() const ValueFlow::Value *value = tok->getValueGE(maxint + 1, _settings); if (!value) value = tok->getValueLE(-maxint - 2, _settings); - if (value) - integerOverflowError(tok, *value); + if (!value) + continue; + + // For left shift, it's common practice to shift into the sign bit + if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (1LL << _settings->int_bit)) + continue; + + integerOverflowError(tok, *value); } } } diff --git a/test/testtype.cpp b/test/testtype.cpp index 8f59ac036..3788e0df4 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -42,7 +42,7 @@ private: TEST_CASE(checkFloatToIntegerOverflow); } - void check(const char code[], Settings* settings = 0) { + void check(const char code[], Settings* settings = 0, const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); @@ -55,7 +55,7 @@ private: // Tokenize.. Tokenizer tokenizer(settings, this); std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); + tokenizer.tokenize(istr, filename); // Check.. CheckType checkType(&tokenizer, settings, this); @@ -137,6 +137,11 @@ private: " return 123456U * x;\n" "}",&settings); ASSERT_EQUALS("", errout.str()); + + check("int foo() {\n" + " x = 1 << 31;\n" // this is technically integer overflow but it's common code + "}", &settings, "test.c"); + ASSERT_EQUALS("", errout.str()); } void signConversion() {