From: Sup Yut Sum Date: Fri, 19 Apr 2013 13:06:17 +0000 (+0800) Subject: If longer than MAX_PATH, try to shorten it to reduce crash X-Git-Tag: REL_1.8.3.0_EXTERNAL~47 X-Git-Url: https://repo.or.cz/w/TortoiseGit.git/commitdiff_plain/0dbc4f9286d020b3a3fc30fe18ad000c1012e118 If longer than MAX_PATH, try to shorten it to reduce crash Signed-off-by: Sup Yut Sum --- diff --git a/src/Utils/CommonAppUtils.cpp b/src/Utils/CommonAppUtils.cpp index d585179b8..369706911 100644 --- a/src/Utils/CommonAppUtils.cpp +++ b/src/Utils/CommonAppUtils.cpp @@ -239,7 +239,24 @@ bool CCommonAppUtils::FileOpenSave(CString& path, int * filterindex, UINT title, TCHAR szFile[MAX_PATH] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH. ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwndOwner; - _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path); + if (path.GetLength() >= MAX_PATH) + { + CString dir = path; + while (true) + { + int index = dir.ReverseFind(_T('\\')); + if (index < 0) + break; + dir = dir.Left(index); + if (PathFileExists(dir)) + break; + } + GetShortPathName(dir, szFile, MAX_PATH); + CString remain = path.Right(path.GetLength() - dir.GetLength()); + _tcscat_s(szFile, MAX_PATH, remain); + } + else + _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path); ofn.lpstrFile = szFile; ofn.nMaxFile = _countof(szFile);