diff --git a/lib/token.cpp b/lib/token.cpp index 486d7f0e3..309b9c4fa 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -621,8 +621,11 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) } // [.. => search for a one-character token.. - else if (p[0] == '[' && tok->_str.length() == 1 && chrInFirstWord(p, ']')) + else if (p[0] == '[' && chrInFirstWord(p, ']')) { + if(tok->_str.length() != 1) + return false; + const char *temp = p + 1; bool chrFound = false; int count = 0; diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 3a0d041da..f865fb941 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(deleteLast); TEST_CASE(matchAny); + TEST_CASE(matchSingleChar); TEST_CASE(matchNothingOrAnyNotElse); TEST_CASE(matchNumeric); TEST_CASE(matchBoolean); @@ -144,6 +145,17 @@ private: givenACodeSampleToTokenize varLogOrVar("abc||def"); ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%var% || %var%")); } + + void matchSingleChar() + { + givenACodeSampleToTokenize singleChar("a"); + ASSERT_EQUALS(true, Token::Match(singleChar.tokens(), "[a|bc]")); + ASSERT_EQUALS(false, Token::Match(singleChar.tokens(), "[d|ef]")); + + Token multiChar(0); + multiChar.str("[ab"); + ASSERT_EQUALS(false, Token::Match(&multiChar, "[ab|def]")); + } void matchNothingOrAnyNotElse() {