瀏覽代碼

Bug fix: Possibility that files in root folder were incorrectly used

Source commit: 88e091dd7ad1cbdac404d6e883ff8a44aa89b316
Martin Prikryl 1 年之前
父節點
當前提交
d2deb06e87

+ 14 - 2
source/core/Common.cpp

@@ -963,6 +963,17 @@ bool __fastcall SamePaths(const UnicodeString & Path1, const UnicodeString & Pat
   return AnsiSameText(IncludeTrailingBackslash(Path1), IncludeTrailingBackslash(Path2));
 }
 //---------------------------------------------------------------------------
+UnicodeString CombinePaths(const UnicodeString & Path1, const UnicodeString & Path2)
+{
+  // Make sure that C: + foo => C:\foo and not C:foo
+  UnicodeString Path1Terminated = Path1;
+  if (EndsStr(L":", Path1Terminated))
+  {
+    Path1Terminated = IncludeTrailingBackslash(Path1Terminated);
+  }
+  return TPath::Combine(Path1Terminated, Path2);
+}
+//---------------------------------------------------------------------------
 int __fastcall CompareLogicalText(
   const UnicodeString & S1, const UnicodeString & S2, bool NaturalOrderNumericalSorting)
 {
@@ -1525,7 +1536,7 @@ bool TSearchRecSmart::IsHidden() const
 //---------------------------------------------------------------------------
 UnicodeString TSearchRecChecked::GetFilePath() const
 {
-  return TPath::Combine(Dir, Name);
+  return CombinePaths(Dir, Name);
 }
 //---------------------------------------------------------------------------
 TSearchRecOwned::~TSearchRecOwned()
@@ -1628,7 +1639,7 @@ void __fastcall ProcessLocalDirectory(UnicodeString DirName,
   }
 
   TSearchRecOwned SearchRec;
-  if (FindFirstChecked(TPath::Combine(DirName, AnyMask), FindAttrs, SearchRec) == 0)
+  if (FindFirstChecked(CombinePaths(DirName, AnyMask), FindAttrs, SearchRec) == 0)
   {
     do
     {
@@ -4543,3 +4554,4 @@ UnicodeString GetDividerLine()
 {
   return UnicodeString::StringOfChar(L'-', 27);
 }
+//---------------------------------------------------------------------

+ 1 - 0
source/core/Common.h

@@ -100,6 +100,7 @@ UnicodeString __fastcall EscapePuttyCommandParam(UnicodeString Param);
 UnicodeString __fastcall StringsToParams(TStrings * Strings);
 UnicodeString __fastcall ExpandEnvironmentVariables(const UnicodeString & Str);
 bool __fastcall SamePaths(const UnicodeString & Path1, const UnicodeString & Path2);
+UnicodeString CombinePaths(const UnicodeString & Path1, const UnicodeString & Path2);
 UnicodeString GetNormalizedPath(const UnicodeString & Path);
 UnicodeString GetCanonicalPath(const UnicodeString & Path);
 bool __fastcall IsPathToSameFile(const UnicodeString & Path1, const UnicodeString & Path2);

+ 11 - 11
source/core/Configuration.cpp

@@ -951,7 +951,7 @@ void __fastcall TConfiguration::CleanupRegistry(const UnicodeString & RegistryPa
     Registry->UnmungedRoot = RegistryStorageSubKey;
 
     AppLogFmt(L"Cleaning up registry key %s", (RegistryPath));
-    UnicodeString ARegistryPath = TPath::Combine(RegistryStorageSubKey, RegistryPath);
+    UnicodeString ARegistryPath = CombinePaths(RegistryStorageSubKey, RegistryPath);
     UnicodeString Buf = ARegistryPath;
     while (!Buf.IsEmpty())
     {
@@ -991,9 +991,9 @@ TStrings * TConfiguration::GetCaches()
   Result->Add(FtpsCertificateStorageKey);
   Result->Add(HttpsCertificateStorageKey);
   Result->Add(DirectoryStatisticsCacheKey);
-  Result->Add(TPath::Combine(ConfigurationSubKey, CDCacheKey));
-  Result->Add(TPath::Combine(ConfigurationSubKey, BannersKey));
-  Result->Add(TPath::Combine(ConfigurationSubKey, LastFingerprintsStorageKey));
+  Result->Add(CombinePaths(ConfigurationSubKey, CDCacheKey));
+  Result->Add(CombinePaths(ConfigurationSubKey, BannersKey));
+  Result->Add(CombinePaths(ConfigurationSubKey, LastFingerprintsStorageKey));
   return Result.release();
 }
 //---------------------------------------------------------------------------
@@ -1683,8 +1683,8 @@ TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport
   std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
 
   UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
-  UnicodeString FilezillaSiteManagerFile = TPath::Combine(AppDataPath, L"FileZilla\\sitemanager.xml");
-  UnicodeString FilezillaConfigurationFile = TPath::Combine(AppDataPath, L"FileZilla\\filezilla.xml");
+  UnicodeString FilezillaSiteManagerFile = CombinePaths(AppDataPath, L"FileZilla\\sitemanager.xml");
+  UnicodeString FilezillaConfigurationFile = CombinePaths(AppDataPath, L"FileZilla\\filezilla.xml");
 
   if (FileExists(ApiPath(FilezillaSiteManagerFile)))
   {
@@ -1724,7 +1724,7 @@ bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList
 UnicodeString GetOpensshFolder()
 {
   UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
-  UnicodeString Result = TPath::Combine(ProfilePath, OpensshFolderName);
+  UnicodeString Result = CombinePaths(ProfilePath, OpensshFolderName);
   return Result;
 }
 //---------------------------------------------------------------------
@@ -1732,7 +1732,7 @@ TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImpor
   TStoredSessionList * Sessions, UnicodeString & Error)
 {
   std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
-  UnicodeString KnownHostsFile = TPath::Combine(GetOpensshFolder(), L"known_hosts");
+  UnicodeString KnownHostsFile = CombinePaths(GetOpensshFolder(), L"known_hosts");
 
   try
   {
@@ -1776,7 +1776,7 @@ TStoredSessionList * TConfiguration::SelectOpensshSessionsForImport(
   TStoredSessionList * Sessions, UnicodeString & Error)
 {
   std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
-  UnicodeString ConfigFile = TPath::Combine(GetOpensshFolder(), L"config");
+  UnicodeString ConfigFile = CombinePaths(GetOpensshFolder(), L"config");
 
   try
   {
@@ -1801,7 +1801,7 @@ TStoredSessionList * TConfiguration::SelectOpensshSessionsForImport(
               // If path does not exist, try if it works relatively to .ssh/
               if (!FileExists(ApiPath(IncludePath)))
               {
-                IncludePath = TPath::Combine(GetOpensshFolder(), IncludePath);
+                IncludePath = CombinePaths(GetOpensshFolder(), IncludePath);
               }
 
               if (FileExists(ApiPath(IncludePath)))
@@ -1971,7 +1971,7 @@ UnicodeString TConfiguration::GetCertificateStorageExpanded()
   UnicodeString Result = FCertificateStorage;
   if (Result.IsEmpty())
   {
-    UnicodeString DefaultCertificateStorage = TPath::Combine(ExtractFilePath(ModuleFileName()), L"cacert.pem");
+    UnicodeString DefaultCertificateStorage = CombinePaths(ExtractFilePath(ModuleFileName()), L"cacert.pem");
     if (FileExists(DefaultCertificateStorage))
     {
       Result = DefaultCertificateStorage;

+ 1 - 1
source/core/SessionInfo.cpp

@@ -357,7 +357,7 @@ public:
 
     if (RecordLocal)
     {
-      UnicodeString FileName = TPath::Combine(Item->Local.Directory, Item->Local.FileName);
+      UnicodeString FileName = CombinePaths(Item->Local.Directory, Item->Local.FileName);
       SynchronizeChecklistItemFileInfo(FileName, Item->IsDirectory, Item->Local);
     }
     if (RecordRemote)

+ 4 - 4
source/core/Terminal.cpp

@@ -879,7 +879,7 @@ void TParallelOperation::Done(
 
               try
               {
-                UnicodeString TargetName = TPath::Combine(TargetDir, FParallelFileTargetName);
+                UnicodeString TargetName = CombinePaths(TargetDir, FParallelFileTargetName);
                 UnicodeString TargetNamePartial = TargetName + PartialExt;
                 UnicodeString TargetNamePartialOnly = ExtractFileName(TargetNamePartial);
 
@@ -896,7 +896,7 @@ void TParallelOperation::Done(
                     UnicodeString FileNameOnly = UnixExtractFileName(FileName);
                     // Safe as write access to FParallelFileMerged is guarded by FParallelFileMerging
                     int Index = FParallelFileMerged;
-                    UnicodeString TargetPartName = GetPartPrefix(TPath::Combine(TargetDir, FileNameOnly)) + IntToStr(Index);
+                    UnicodeString TargetPartName = GetPartPrefix(CombinePaths(TargetDir, FileNameOnly)) + IntToStr(Index);
 
                     if ((CopyParam->PartSize >= 0) && (Terminal->OperationProgress->TransferredSize != CopyParam->PartSize))
                     {
@@ -1101,7 +1101,7 @@ int TParallelOperation::GetNext(
         }
         else
         {
-          DirectoryData.OppositePath = TPath::Combine(TargetDir, OnlyFileName);
+          DirectoryData.OppositePath = CombinePaths(TargetDir, OnlyFileName);
         }
 
         DirectoryData.Exists = false;
@@ -7838,7 +7838,7 @@ void TTerminal::CheckParallelFileTransfer(
         {
           ParallelFileSize = UltimateFile->Size;
           UnicodeString TargetFileName = CopyParam->ChangeFileName(UnixExtractFileName(ParallelFileName), osRemote, true);
-          UnicodeString DestFullName = TPath::Combine(TargetDir, TargetFileName);
+          UnicodeString DestFullName = CombinePaths(TargetDir, TargetFileName);
 
           if (::FileExists(ApiPath(DestFullName)))
           {

+ 1 - 1
source/forms/CustomScpExplorer.cpp

@@ -6438,7 +6438,7 @@ void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side,
     }
     else
     {
-      OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
+      OpenFileInExplorer(CombinePaths(LocalPath, Item->GetFileName()));
     }
   }
   else if (DebugAlwaysTrue(Side == osRemote))

+ 1 - 1
source/forms/ScpCommander.cpp

@@ -2823,7 +2823,7 @@ void TScpCommanderForm::LocalLocalCopy(
       UnicodeString SourcePath = FileOperator->OperandFrom->Strings[Index];
       UnicodeString FileName = TPath::GetFileName(SourcePath);
       FileName = MaskFileName(FileName, FileMask);
-      UnicodeString DestinationPath = TPath::Combine(DestinationDir, FileName);
+      UnicodeString DestinationPath = CombinePaths(DestinationDir, FileName);
       FileOperator->OperandTo->Add(DestinationPath);
     }
 

+ 2 - 2
source/forms/SynchronizeChecklist.cpp

@@ -1311,8 +1311,8 @@ void __fastcall TSynchronizeChecklistDialog::MoveActionExecute(TObject *)
   else if (Action2 == TSynchronizeChecklist::saDeleteLocal)
   {
     Side = osLocal;
-    FileName = TPath::Combine(MoveItems.second->Local.Directory, MoveItems.second->Local.FileName);
-    NewFileName = TPath::Combine(MoveItems.first->Local.Directory, MoveItems.first->Remote.FileName);
+    FileName = CombinePaths(MoveItems.second->Local.Directory, MoveItems.second->Local.FileName);
+    NewFileName = CombinePaths(MoveItems.first->Local.Directory, MoveItems.first->Remote.FileName);
     RemoteFile = NULL;
   }
   else

+ 1 - 1
source/windows/GUITools.cpp

@@ -80,7 +80,7 @@ bool __fastcall FindFile(UnicodeString & Path)
         while (!Result && !Paths.IsEmpty())
         {
           UnicodeString P = CutToChar(Paths, L';', false);
-          // Not using TPath::Combine as it throws on an invalid path and PATH is not under our control
+          // Not using CombinePaths as it throws on an invalid path and PATH is not under our control
           NewPath = IncludeTrailingBackslash(P) + Path;
           Result = FileExistsFix(NewPath);
           if (Result)

+ 3 - 3
source/windows/Setup.cpp

@@ -2371,19 +2371,19 @@ UnicodeString GetNetCoreVersionStr()
       ::SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
     }
     UnicodeString RuntimeFolder = L"shared\\Microsoft.NETCore.App";
-    UnicodeString DotNetPath = TPath::Combine(TPath::Combine(ProgramsFolder, L"dotnet"), RuntimeFolder);
+    UnicodeString DotNetPath = CombinePaths(CombinePaths(ProgramsFolder, L"dotnet"), RuntimeFolder);
     if (!DirectoryExistsFix(DotNetPath))
     {
       UnicodeString DotNetExe = L"dotnet.exe";
       if (FindFile(DotNetExe))
       {
-        DotNetPath = TPath::Combine(ExtractFilePath(DotNetExe), RuntimeFolder);
+        DotNetPath = CombinePaths(ExtractFilePath(DotNetExe), RuntimeFolder);
       }
     }
     if (DirectoryExistsFix(DotNetPath))
     {
       TSearchRecChecked SearchRec;
-      DotNetPath = TPath::Combine(DotNetPath, L"*.*");
+      DotNetPath = CombinePaths(DotNetPath, L"*.*");
       if (FindFirstUnchecked(ApiPath(DotNetPath), faDirectory, SearchRec) == 0)
       {
         do

+ 1 - 1
source/windows/WinInterface.cpp

@@ -1407,7 +1407,7 @@ UnicodeString DumpCallstackEventName(int ProcessId)
 UnicodeString DumpCallstackFileName(int ProcessId)
 {
   UnicodeString FileName = FORMAT(L"%s.txt", (DumpCallstackEventName(ProcessId)));
-  UnicodeString Result = TPath::Combine(SystemTemporaryDirectory(), FileName);
+  UnicodeString Result = CombinePaths(SystemTemporaryDirectory(), FileName);
   return Result;
 }
 //---------------------------------------------------------------------------