diff --git a/connections/clients/windows/dart/core_adapter_dart.cc b/connections/clients/windows/dart/core_adapter_dart.cc index 11a4eff6..9697ed38 100644 --- a/connections/clients/windows/dart/core_adapter_dart.cc +++ b/connections/clients/windows/dart/core_adapter_dart.cc @@ -519,11 +519,15 @@ std::string GetPayloadPath(location::nearby::PayloadId payload_id) { // process is responsible for freeing this resource once it // is no longer needed by calling CoTaskMemFree, whether // SHGetKnownFolderPath succeeds or not. - - char *fullpathUTF8 = new char((wcslen(basePath) + 1) * sizeof(char)); - wcstombs(fullpathUTF8, basePath, (wcslen(basePath) + 1) * sizeof(char)); - - return absl::StrCat(fullpathUTF8, "\\", payload_id); + size_t bufferSize; + wcstombs_s(&bufferSize, NULL, 0, basePath, 0); + char *fullpathUTF8 = new char[bufferSize + 1]; + memset(fullpathUTF8, 0, bufferSize); + wcstombs_s(&bufferSize, fullpathUTF8, bufferSize, basePath, bufferSize - 1); + std::string fullPath = std::string(fullpathUTF8); + auto retval = absl::StrCat(fullPath, "\\", payload_id); + delete[] fullpathUTF8; + return retval; } void SendPayloadDart(Core *pCore, const char *endpoint_id,