summaryrefslogtreecommitdiffstats
path: root/base/string_util.cc
diff options
context:
space:
mode:
authortony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-03 23:32:57 +0000
committertony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-03 23:32:57 +0000
commitc5a7248b45656b1ca63f9ce35b60ca337f83d3d1 (patch)
tree0f6f50e96e99046d820d9b49ccea1294244b7c2a /base/string_util.cc
parentdf2c7403503b45467d3d76ddc2f42296fa53ac50 (diff)
downloadchromium_src-c5a7248b45656b1ca63f9ce35b60ca337f83d3d1.zip
chromium_src-c5a7248b45656b1ca63f9ce35b60ca337f83d3d1.tar.gz
chromium_src-c5a7248b45656b1ca63f9ce35b60ca337f83d3d1.tar.bz2
Fixes to the string MatchPattern functions:
1) Make it explicit that it only supports ASCII (since it iterates character by character). 2) Limit the recursion to 16 levels. We could allow more, but in the case of a ?, it has exponential complexity, so I figured 16 was a good stopping point. It seems rare that someone would have more than 16 '?' and '*'s. BUG=28645 Review URL: http://codereview.chromium.org/460047 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33748 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r--base/string_util.cc21
1 files changed, 13 insertions, 8 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 01a6907..9db0f03 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -1561,7 +1561,11 @@ static void EatWildcard(const CHAR** pattern) {
}
template <class CHAR>
-static bool MatchPatternT(const CHAR* eval, const CHAR* pattern) {
+static bool MatchPatternT(const CHAR* eval, const CHAR* pattern, int depth) {
+ const int kMaxDepth = 16;
+ if (depth > kMaxDepth)
+ return false;
+
// Eat all the matching chars.
EatSameChars(&pattern, &eval);
@@ -1581,8 +1585,8 @@ static bool MatchPatternT(const CHAR* eval, const CHAR* pattern) {
// If this is a question mark, then we need to compare the rest with
// the current string or the string with one character eaten.
if (pattern[0] == '?') {
- if (MatchPatternT(eval, pattern + 1) ||
- MatchPatternT(eval + 1, pattern + 1))
+ if (MatchPatternT(eval, pattern + 1, depth + 1) ||
+ MatchPatternT(eval + 1, pattern + 1, depth + 1))
return true;
}
@@ -1590,7 +1594,7 @@ static bool MatchPatternT(const CHAR* eval, const CHAR* pattern) {
// of the pattern.
if (pattern[0] == '*') {
while (*eval) {
- if (MatchPatternT(eval, pattern + 1))
+ if (MatchPatternT(eval, pattern + 1, depth + 1))
return true;
eval++;
}
@@ -1608,12 +1612,13 @@ static bool MatchPatternT(const CHAR* eval, const CHAR* pattern) {
return false;
}
-bool MatchPattern(const std::wstring& eval, const std::wstring& pattern) {
- return MatchPatternT(eval.c_str(), pattern.c_str());
+bool MatchPatternWide(const std::wstring& eval, const std::wstring& pattern) {
+ return MatchPatternT(eval.c_str(), pattern.c_str(), 0);
}
-bool MatchPattern(const std::string& eval, const std::string& pattern) {
- return MatchPatternT(eval.c_str(), pattern.c_str());
+bool MatchPatternASCII(const std::string& eval, const std::string& pattern) {
+ DCHECK(IsStringASCII(eval) && IsStringASCII(pattern));
+ return MatchPatternT(eval.c_str(), pattern.c_str(), 0);
}
bool StringToInt(const std::string& input, int* output) {