瀏覽代碼

Fixes an additional ASan detection in settings registry. (#18822)

The use-after free occurs because the AZ::IO::FixedMaxPath
function ".AsPosix()" returns a temporary object.

This temporary object was being cached in a char* pointer,
and then used after the original call site of AsPosix() resolved.

Signed-off-by: Nicholas Lawson <[email protected]>
Nicholas Lawson 5 月之前
父節點
當前提交
6ff7eae323
共有 1 個文件被更改,包括 4 次插入5 次删除
  1. 4 5
      Code/Framework/AzFramework/AzFramework/DocumentPropertyEditor/SettingsRegistrar.cpp

+ 4 - 5
Code/Framework/AzFramework/AzFramework/DocumentPropertyEditor/SettingsRegistrar.cpp

@@ -34,25 +34,24 @@ namespace AZ::DocumentPropertyEditor
 
         AZ::IO::FixedMaxPath fullSettingsPath = AZ::Utils::GetProjectPath();
         fullSettingsPath /= relativeFilepath;
-        const char* posixSettingsPath = fullSettingsPath.AsPosix().c_str();
+        AZ::IO::FixedMaxPath posixSettingsPath = fullSettingsPath.AsPosix().c_str();
 
         AZStd::string stringBuffer;
         AZ::IO::ByteContainerStream stringStream(&stringBuffer);
         if (!AZ::SettingsRegistryMergeUtils::DumpSettingsRegistryToStream(*registry, anchorKey, stringStream, dumperSettings))
         {
             return AZ::Failure(AZStd::string::format(
-                "Failed to save settings to file '%s': failed to retrieve settings from registry", posixSettingsPath));
+                "Failed to save settings to file '%s': failed to retrieve settings from registry", posixSettingsPath.c_str()));
         }
 
         constexpr auto openMode = AZ::IO::SystemFile::SF_OPEN_CREATE
             | AZ::IO::SystemFile::SF_OPEN_CREATE_PATH
             | AZ::IO::SystemFile::SF_OPEN_WRITE_ONLY;
-        if (AZ::IO::SystemFile outputFile; outputFile.Open(posixSettingsPath, openMode))
+        if (AZ::IO::SystemFile outputFile; outputFile.Open(posixSettingsPath.c_str(), openMode))
         {
             if(outputFile.Write(stringBuffer.data(), stringBuffer.size()) != stringBuffer.size())
             {
-                return AZ::Failure(AZStd::string::format(
-                    "Failed to save settings to file '%s': incomplete contents written", posixSettingsPath));
+                return AZ::Failure(AZStd::string::format("Failed to save settings to file '%s': incomplete contents written", posixSettingsPath.c_str()));
             }
         }