From 403e2ff1e3b73d0c900f5a04957ac42e2265cd63 Mon Sep 17 00:00:00 2001 From: bosnichd Date: Tue, 2 Nov 2021 10:31:50 -0600 Subject: [PATCH] Fix bug in LocalFileIO::ConvertToAliasBuffer when a resolved alias ends in a path separator. (#5136) * Fix bug in LocalFileIO::ConvertToAliasBuffer when a resolved alias ends in a path separator, in which case we do not want to consume it when replacing it with the alias. eg. If the @products@ alias resolves to "C:\" and we call ConvertToAliasBuffer with "C:\some_folder\some_file.txt", the current behaviour results in "@products@some_folder\some_file.txt", but it needs to be "@products@\some_folder\some_file.txt" Signed-off-by: bosnichd * Update based on review feedback. Signed-off-by: bosnichd --- Code/Framework/AzFramework/AzFramework/IO/LocalFileIO.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzFramework/AzFramework/IO/LocalFileIO.cpp b/Code/Framework/AzFramework/AzFramework/IO/LocalFileIO.cpp index c1b9c941bc..19bffaccbd 100644 --- a/Code/Framework/AzFramework/AzFramework/IO/LocalFileIO.cpp +++ b/Code/Framework/AzFramework/AzFramework/IO/LocalFileIO.cpp @@ -635,6 +635,7 @@ namespace AZ size_t longestMatch = 0; size_t bufStringLength = inBuffer.size(); AZStd::string_view longestAlias; + AZStd::string_view longestResolvedAlias; for (const auto& [alias, resolvedAlias] : m_aliases) { @@ -653,6 +654,7 @@ namespace AZ { longestMatch = resolvedAlias.size(); longestAlias = alias; + longestResolvedAlias = resolvedAlias; } } } @@ -661,7 +663,10 @@ namespace AZ // rearrange the buffer to have // [alias][old path] size_t aliasSize = longestAlias.size(); - size_t charsToAbsorb = longestMatch; + // If the resolved alias ends in a path separator, do not consume it. + const bool resolvedAliasEndsInPathSeparator = (longestResolvedAlias.ends_with(AZ::IO::PosixPathSeparator) || + longestResolvedAlias.ends_with(AZ::IO::WindowsPathSeparator)); + const size_t charsToAbsorb = resolvedAliasEndsInPathSeparator ? longestMatch - 1 : longestMatch; size_t remainingData = bufStringLength - charsToAbsorb; size_t finalStringSize = aliasSize + remainingData; if (finalStringSize >= outBufferLength)