From 084b3c002f67273caa194ad02945e5a95bd190bc Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 14 Jun 2009 11:21:20 +0700 Subject: [PATCH] Fixed ticket #403 (false positive::Array index out of range) http://sourceforge.net/apps/trac/cppcheck/ticket/403 --- src/preprocessor.cpp | 5 +++-- test/testpreprocessor.cpp | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index c6de1ef6d..5b8ddfc06 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -703,7 +703,8 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, } else if (line.find("#file \"") == 0 || line.find("#endfile") == 0 || - line.find("#define") == 0) + line.find("#define") == 0 || + line.find("#undef") == 0) { // We must not remove #file tags or line numbers // are corrupted. File tags are removed by the tokenizer. @@ -712,7 +713,7 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, line[0] == '#') { // Remove #if, #else, #pragma etc, leaving only - // #define, #file and #endfile. and also lines + // #define, #undef, #file and #endfile. and also lines // which are not part of this configuration. line = ""; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index aa55ee7f5..184478722 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -726,13 +726,25 @@ private: void preprocessor_undef() { - const char filedata[] = "#define AAA int a;\n" - "#undef AAA\n" - "#define AAA char b=0;\n" - "AAA\n"; + { + const char filedata[] = "#define AAA int a;\n" + "#undef AAA\n" + "#define AAA char b=0;\n" + "AAA\n"; - // Compare results.. - ASSERT_EQUALS("\n\n\nchar b=0;\n", OurPreprocessor::expandMacros(filedata)); + // Compare results.. + ASSERT_EQUALS("\n\n\nchar b=0;\n", OurPreprocessor::expandMacros(filedata)); + } + + { + // ticket #403 + const char filedata[] = "#define z p[2]\n" + "#undef z\n" + "int z;\n" + "z = 0;\n"; + + ASSERT_EQUALS("\n\nint z;\nz = 0;\n", OurPreprocessor::getcode(filedata, "", "", NULL)); + } } void defdef()