OSDN Git Service

PATCH: #3056974 FileFilterMgr accepts long filepath
authorKimmo Varis <kimmov@gmail.com>
Sun, 12 Sep 2010 10:24:09 +0000 (10:24 +0000)
committerKimmo Varis <kimmov@gmail.com>
Sun, 12 Sep 2010 10:24:09 +0000 (10:24 +0000)
 Fixes bug #3050199
 Submitted by Matthias Mayer

Docs/Users/ChangeLog.txt
Src/FileFilterMgr.cpp
Src/Plugins.cpp
Src/editlib/ccrystaltextview.cpp

index a75e0aa..849d535 100644 (file)
@@ -21,6 +21,7 @@ WinMerge 2.13.14
   Bugfix: Location pane focus enabled "Save" (#3022292)
   Bugfix: WinMerge was vulnerable to DLL hijacking as described in
     Microsoft Security Advisory (2269637)  (#33056008)
+  Bugfix: Fix crash with long paths (#3056974)
   Translation updates:
   - Chinese (#3033324)
   - French (#3025202)
index 39c3aeb..413a93d 100644 (file)
@@ -142,7 +142,7 @@ static void AddFilterPattern(vector<FileFilterElement*> *filterList, String & st
 
        // Ignore lines beginning with '##'
        size_t pos = str.find(commentLeader);
-       if (pos == 0)
+       if (pos == str.npos)
                return;
 
        // Find possible comment-separator '<whitespace>##'
@@ -298,35 +298,37 @@ FileFilter * FileFilterMgr::GetFilterByPath(LPCTSTR szFilterPath)
  */
 BOOL TestAgainstRegList(const vector<FileFilterElement*> *filterList, LPCTSTR szTest)
 {
-       vector<FileFilterElement*>::const_iterator iter = filterList->begin();
-       while (iter != filterList->end())
-       {
                int ovector[30];
-               char compString[200] = {0};
-               int stringLen = 0;
-               TCHAR * tempName = _tcsdup(szTest); // Create temp copy for conversions
-               TCHAR * cmpStr = _tcsupr(tempName);
+       const String sTest = szTest;
+       const int ilen = sTest.length() * sizeof(TCHAR) + 1;
+       char *compString = new char[ilen];
+       size_t stringlen;
 
 #ifdef UNICODE
-               stringLen = TransformUcs2ToUtf8(cmpStr, _tcslen(cmpStr),
-                       compString, sizeof(compString));
+       stringlen = TransformUcs2ToUtf8(sTest.c_str(), sTest.length(),
+               compString, ilen);
 #else
-               strcpy(compString, cmpStr);
-               stringLen = strlen(compString);
+       strncpy(compString, sTest.c_str(), sTest.length());
+       stringlen = ilen - 1;//linelen(compString);
 #endif
 
+       compString[stringlen] = 0;
+
+       int result = 0;
+       vector<FileFilterElement*>::const_iterator iter = filterList->begin();
+       while (iter != filterList->end())
+       {
                pcre * regexp = (*iter)->pRegExp;
                pcre_extra * extra = (*iter)->pRegExpExtra;
-               int result = pcre_exec(regexp, extra, compString, stringLen,
+               result = pcre_exec(regexp, extra, compString,(int) stringlen,
                        0, 0, ovector, 30);
-
-               free(tempName);
-
                if (result >= 0)
-                       return TRUE;
-
+                       break;
                ++iter;
        }
+       delete [] compString;
+       if (result >= 0)
+               return TRUE;
        return FALSE;
 }
 
index fc8b042..3099ff9 100644 (file)
@@ -286,16 +286,17 @@ void PluginInfo::LoadFilterString()
                FileFilterElement element;
                const char * errormsg = NULL;
                int erroroffset = 0;
-               char regexString[200] = {0};
-               int regexLen = 0;
+               const int ilen = _tcslen(sPiece) * sizeof(TCHAR) + 1;
+               char *regexString = new char[ilen];
+
 #ifdef UNICODE
-               regexLen = TransformUcs2ToUtf8((LPCTSTR)sPiece, _tcslen(sPiece),
-                       regexString, sizeof(regexString));
+               size_t regexLen = TransformUcs2ToUtf8((LPCTSTR)sPiece, _tcslen(sPiece),
+                       regexString, ilen);
 #else
                strcpy(regexString, (LPCTSTR)sPiece);
-               regexLen = strlen(regexString);
+               size_t regexLen = ilen;
 #endif
-
+               regexString[regexLen] = 0;
                pcre *regexp = pcre_compile(regexString, 0, &errormsg, &erroroffset, NULL);
                if (regexp)
                {
@@ -303,7 +304,7 @@ void PluginInfo::LoadFilterString()
                        elem->pRegExp = regexp;
                        m_filters->push_back(elem);
                }
-
+               delete [] regexString;
        };
 }
 
index 0506c50..cf4fae9 100644 (file)
@@ -4510,101 +4510,101 @@ FindStringHelper (LPCTSTR pszFindWhere, LPCTSTR pszFindWhat, DWORD dwFlags,
      int &nLen)
 {
   if (dwFlags & FIND_REGEXP)
-    {
-      int pos;
-      const char * errormsg = NULL;
-      int erroroffset = 0;
-      char regexString[200] = {0};
-      int regexLen = 0;
-      int pcre_opts = 0;
+  {
+    int pos;
+    const char * errormsg = NULL;
+    int erroroffset = 0;
+    const int ilen = _tcslen(pszFindWhat) * sizeof(TCHAR) + 1;
+    char *regexString = new char[ilen];
+    int pcre_opts = 0;
 
 #ifdef UNICODE
-      // For unicode builds, use UTF-8.
-      // Convert pattern to UTF-8 and set option for PCRE to specify UTF-8.
-      regexLen = TransformUcs2ToUtf8(pszFindWhat, _tcslen(pszFindWhat),
-          regexString, sizeof(regexString));
-      pcre_opts |= PCRE_UTF8;
+    // For unicode builds, use UTF-8.
+    // Convert pattern to UTF-8 and set option for PCRE to specify UTF-8.
+    size_t regexLen = TransformUcs2ToUtf8(pszFindWhat, _tcslen(pszFindWhat),
+      regexString, ilen);
+    pcre_opts |= PCRE_UTF8;
 #else
-      strcpy(regexString, pszFindWhat);
-      regexLen = strlen(regexString);
+    strcpy(regexString, pszFindWhat);
+    size_t regexLen = ilen;
 #endif
-      pcre_opts |= PCRE_BSR_ANYCRLF;
-      if ((dwFlags & FIND_MATCH_CASE) == 0)
-        pcre_opts |= PCRE_CASELESS;
-
-      pcre *regexp = pcre_compile(regexString, pcre_opts, &errormsg,
-          &erroroffset, NULL);
-      pcre_extra *pe = NULL;
-      if (regexp)
-        {
-          errormsg = NULL;
-          pe = pcre_study(regexp, 0, &errormsg);
-        }
+    regexString[regexLen] = 0;
+    pcre_opts |= PCRE_BSR_ANYCRLF;
+    if ((dwFlags & FIND_MATCH_CASE) == 0)
+      pcre_opts |= PCRE_CASELESS;
 
-      int ovector[30];
-      int compStringBufLen = _tcslen(pszFindWhere) * sizeof(TCHAR) + 1;
-      char *compString = new char[compStringBufLen];
-      int stringLen = 0;
+    pcre *regexp = pcre_compile(regexString, pcre_opts, &errormsg,
+      &erroroffset, NULL);
+    pcre_extra *pe = NULL;
+    if (regexp)
+    {
+      errormsg = NULL;
+      pe = pcre_study(regexp, 0, &errormsg);
+    }
+    delete [] regexString;
+    int ovector[30];
+    int compStringBufLen = _tcslen(pszFindWhere) * sizeof(TCHAR) + 1;
+    char *compString = new char[compStringBufLen];
+    int stringLen = 0;
 
 #ifdef UNICODE
-      stringLen = TransformUcs2ToUtf8(pszFindWhere, _tcslen(pszFindWhere),
-          compString, compStringBufLen);
-      compString[stringLen] = '\0';
+    stringLen = TransformUcs2ToUtf8(pszFindWhere, _tcslen(pszFindWhere),
+      compString, compStringBufLen);
 #else
-      strncpy(compString, pszFindWhere, compStringBufLen);
-      stringLen = strlen(compString);
+    strncpy(compString, pszFindWhere, compStringBufLen);
+    stringLen = compStringBufLen;
 #endif
+    compString[stringLen] = 0;
+    int result = pcre_exec(regexp, pe, compString, stringLen,
+      0, 0, ovector, 30);
 
-      int result = pcre_exec(regexp, pe, compString, stringLen,
-          0, 0, ovector, 30);
-
-      if (result >= 0)
-        {
+    if (result >= 0)
+    {
 #ifdef UNICODE
-          pos = ucr::stringlen_of_utf8(compString, ovector[0]);
-          nLen = ucr::stringlen_of_utf8(compString, ovector[1]) - pos;
+      pos = ucr::stringlen_of_utf8(compString, ovector[0]);
+      nLen = ucr::stringlen_of_utf8(compString, ovector[1]) - pos;
 #else
-          pos = ovector[0];
-          nLen = ovector[1] - ovector[0];
+      pos = ovector[0];
+      nLen = ovector[1] - ovector[0];
 #endif
-        }
-      else
-        pos = -1;
-
-      delete [] compString;
-      pcre_free(regexp);
-      pcre_free(pe);
-      return pos;
     }
+    else
+      pos = -1;
+
+    delete [] compString;
+    pcre_free(regexp);
+    pcre_free(pe);
+    return pos;
+  }
   else
-    {
-      ASSERT (pszFindWhere != NULL);
-      ASSERT (pszFindWhat != NULL);
-      int nCur = 0;
-      int nLength = (int) _tcslen (pszFindWhat);
-      nLen = nLength;
-      for (;;)
-        {
-          LPCTSTR pszPos = _tcsstr (pszFindWhere, pszFindWhat);
-          if (pszPos == NULL)
-            return -1;
-          if ((dwFlags & FIND_WHOLE_WORD) == 0)
-            return nCur + (int) (pszPos - pszFindWhere);
-          if (pszPos > pszFindWhere && xisalnum (pszPos[-1]))
-            {
-              nCur += (int) (pszPos - pszFindWhere + 1);
-              pszFindWhere = pszPos + 1;
-              continue;
-            }
-          if (xisalnum (pszPos[nLength]))
-            {
-              nCur += (int) (pszPos - pszFindWhere + 1);
-              pszFindWhere = pszPos + 1;
-              continue;
-            }
-          return nCur + (int) (pszPos - pszFindWhere);
-        }
+  {
+    ASSERT (pszFindWhere != NULL);
+    ASSERT (pszFindWhat != NULL);
+    int nCur = 0;
+    int nLength = (int) _tcslen (pszFindWhat);
+    nLen = nLength;
+    for (;;)
+    {
+      LPCTSTR pszPos = _tcsstr (pszFindWhere, pszFindWhat);
+      if (pszPos == NULL)
+        return -1;
+      if ((dwFlags & FIND_WHOLE_WORD) == 0)
+        return nCur + (int) (pszPos - pszFindWhere);
+      if (pszPos > pszFindWhere && xisalnum (pszPos[-1]))
+      {
+        nCur += (int) (pszPos - pszFindWhere + 1);
+        pszFindWhere = pszPos + 1;
+        continue;
+      }
+      if (xisalnum (pszPos[nLength]))
+      {
+        nCur += (int) (pszPos - pszFindWhere + 1);
+        pszFindWhere = pszPos + 1;
+        continue;
+      }
+      return nCur + (int) (pszPos - pszFindWhere);
     }
+  }
   ASSERT (FALSE);               // Unreachable
 
   return -1;