Fix #964 (Integer division by zero exception)

http://sourceforge.net/apps/trac/cppcheck/ticket/964
This commit is contained in:
Reijo Tomperi 2009-11-15 17:44:30 +02:00
parent f569edc614
commit 9275b49688
3 changed files with 18 additions and 6 deletions

View File

@ -678,7 +678,9 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
// malloc() gets count of bytes and not count of // malloc() gets count of bytes and not count of
// elements, so we should calculate count of elements // elements, so we should calculate count of elements
// manually // manually
size /= _tokenizer->sizeOfType(declTok); unsigned int sizeOfType = _tokenizer->sizeOfType(declTok);
if (sizeOfType > 0)
size /= _tokenizer->sizeOfType(declTok);
} }
} }
else else

View File

@ -134,6 +134,7 @@ private:
TEST_CASE(memset1); TEST_CASE(memset1);
TEST_CASE(counter_test); TEST_CASE(counter_test);
TEST_CASE(strncpy1); TEST_CASE(strncpy1);
TEST_CASE(unknownType);
} }
@ -1335,6 +1336,15 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str());
} }
void unknownType()
{
check("void f()\n"
"{\n"
" UnknownType *a = malloc(4);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestBufferOverrun) REGISTER_TEST(TestBufferOverrun)

View File

@ -1143,11 +1143,11 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int f(int (*assign)(char *p))\n" checkUninitVar("int f(int (*assign)(char *p))\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" (*assign)(&i);\n" " (*assign)(&i);\n"
" return i;\n" " return i;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// arrays.. // arrays..