From 8a6a999f0914180bbd3641e89c2ea587384abe39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 8 Aug 2011 19:35:11 +0200 Subject: [PATCH] Fixed #2979 (Improve message: using char as array index) --- lib/checkother.cpp | 11 ++++------- test/testcharvar.cpp | 6 +++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index aaebf3d6c..02a77540f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3627,13 +3627,10 @@ void CheckOther::charArrayIndexError(const Token *tok) reportError(tok, Severity::warning, "charArrayIndex", - "When using a char variable as array index, sign extension will mean buffer overflow.\n" - "When using a char variable as array index, sign extension will cause buffer overflows. For example:\n" - " char c = 0x80;\n" - " char buf[512];\n" - " 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."); + "Using char type as array index\n" + "Using signed char type as array index. If the value " + "can be greater than 127 there will be a buffer overflow " + "(because of sign extension)."); } void CheckOther::charBitOpError(const Token *tok) diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index bfc974fdb..5f0886c43 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -77,20 +77,20 @@ private: " char ch = 0x80;\n" " buf[ch] = 0;\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" "{\n" " signed char ch = 0x80;\n" " buf[ch] = 0;\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" "{\n" " buf[ch] = 0;\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()); }