Fixed #2979 (Improve message: using char as array index)
This commit is contained in:
parent
e2367b4149
commit
8a6a999f09
|
@ -3627,13 +3627,10 @@ void CheckOther::charArrayIndexError(const Token *tok)
|
||||||
reportError(tok,
|
reportError(tok,
|
||||||
Severity::warning,
|
Severity::warning,
|
||||||
"charArrayIndex",
|
"charArrayIndex",
|
||||||
"When using a char variable as array index, sign extension will mean buffer overflow.\n"
|
"Using char type as array index\n"
|
||||||
"When using a char variable as array index, sign extension will cause buffer overflows. For example:\n"
|
"Using signed char type as array index. If the value "
|
||||||
" char c = 0x80;\n"
|
"can be greater than 127 there will be a buffer overflow "
|
||||||
" char buf[512];\n"
|
"(because of sign extension).");
|
||||||
" buf[c] = 13; // buffer overflow, index must be a value between 0 and 511.\n"
|
|
||||||
" printf(\"%i\", buf[0x80]);\n"
|
|
||||||
"There is a big chance that the value that is printed won't be 13.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckOther::charBitOpError(const Token *tok)
|
void CheckOther::charBitOpError(const Token *tok)
|
||||||
|
|
|
@ -77,20 +77,20 @@ private:
|
||||||
" char ch = 0x80;\n"
|
" char ch = 0x80;\n"
|
||||||
" buf[ch] = 0;\n"
|
" buf[ch] = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) When using a char variable as array index, sign extension will mean buffer overflow.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (warning) Using char type as array index\n", errout.str());
|
||||||
|
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" signed char ch = 0x80;\n"
|
" signed char ch = 0x80;\n"
|
||||||
" buf[ch] = 0;\n"
|
" buf[ch] = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) When using a char variable as array index, sign extension will mean buffer overflow.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (warning) Using char type as array index\n", errout.str());
|
||||||
|
|
||||||
check("void foo(char ch)\n"
|
check("void foo(char ch)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" buf[ch] = 0;\n"
|
" buf[ch] = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) When using a char variable as array index, sign extension will mean buffer overflow.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Using char type as array index\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue