From 50844aa7fc69f32f3d1e3d77cbddc2f867dc23c5 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sun, 18 Feb 2018 14:28:48 +0100 Subject: [PATCH] checkType: Take into account the size of char. --- cli/cppcheckexecutor.cpp | 2 +- lib/checktype.cpp | 6 ++++-- test/testtype.cpp | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 3653d940f..bf5e99607 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -804,7 +804,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha { Settings& settings = cppcheck.settings(); _settings = &settings; - bool std = tryLoadLibrary(settings.library, argv[0], "std.cfg"); + const bool std = tryLoadLibrary(settings.library, argv[0], "std.cfg"); bool posix = true; if (settings.standards.posix) posix = tryLoadLibrary(settings.library, argv[0], "posix.cfg"); diff --git a/lib/checktype.cpp b/lib/checktype.cpp index cb4eaa2b0..47c00966b 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -69,11 +69,13 @@ void CheckType::checkTooBigBitwiseShift() continue; // get number of bits of lhs - const ValueType *lhstype = tok->astOperand1()->valueType(); + const ValueType * const lhstype = tok->astOperand1()->valueType(); if (!lhstype || !lhstype->isIntegral() || lhstype->pointer >= 1U) continue; int lhsbits = 0; - if (lhstype->type <= ValueType::Type::INT) + if (lhstype->type == ValueType::Type::CHAR) + lhsbits = _settings->char_bit; + else if (lhstype->type <= ValueType::Type::INT) lhsbits = _settings->int_bit; else if (lhstype->type == ValueType::Type::LONG) lhsbits = _settings->long_bit; diff --git a/test/testtype.cpp b/test/testtype.cpp index 216a7bc9e..4209a60d7 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -65,6 +65,11 @@ private: Settings settings; settings.platform(Settings::Unix32); + check("char foo(char x) {\n" + " return x << 32;\n" + "}",&settings); + ASSERT_EQUALS("[test.cpp:2]: (error) Shifting 8-bit value by 32 bits is undefined behaviour\n", errout.str()); + check("int foo(unsigned int x) {\n" " return x << 32;\n" "}",&settings);