Przeglądaj źródła

DxilRuntimeReflection: avoid possibility of wstring::data() invalidation

Tex Riddell 7 lat temu
rodzic
commit
00302c4dfe

+ 2 - 1
include/dxc/HLSL/DxilRuntimeReflection.h

@@ -12,6 +12,7 @@
 #include <windows.h>
 #include <unordered_map>
 #include <vector>
+#include <memory>
 #include "DxilConstants.h"
 
 namespace hlsl {
@@ -388,7 +389,7 @@ typedef struct DXIL_LIBRARY_DESC {
 
 class DxilRuntimeReflection {
 private:
-  typedef std::unordered_map<const char *, std::wstring> StringMap;
+  typedef std::unordered_map<const char *, std::unique_ptr<wchar_t[]>> StringMap;
   typedef std::vector<DXIL_RESOURCE> ResourceList;
   typedef std::vector<DXIL_RESOURCE *> ResourceRefList;
   typedef std::vector<DXIL_FUNCTION> FunctionList;

+ 4 - 3
include/dxc/HLSL/DxilRuntimeReflection.inl

@@ -81,9 +81,10 @@ void DxilRuntimeReflection::AddString(const char *ptr) {
     int size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, ptr, -1,
                                      nullptr, 0);
     if (size != 0) {
-      m_StringMap[ptr] = std::wstring(size, '\0');
+      auto pNew = std::make_unique<wchar_t[]>(size);
       ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, ptr, -1,
-                            &(m_StringMap[ptr][0]), size);
+                            pNew.get(), size);
+      m_StringMap[ptr] = std::move(pNew);
     }
   }
 }
@@ -92,7 +93,7 @@ const wchar_t *DxilRuntimeReflection::GetWideString(const char *ptr) {
   if (m_StringMap.find(ptr) == m_StringMap.end()) {
     AddString(ptr);
   }
-  return m_StringMap.at(ptr).data();
+  return m_StringMap.at(ptr).get();
 }
 
 bool DxilRuntimeReflection::InitFromRDAT(const void *pRDAT) {