Skip to content

Commit

Permalink
windows/serialInterface: Properly deal with the nul characters Window…
Browse files Browse the repository at this point in the history
…s puts on the end of string registry keys
  • Loading branch information
dragonmux authored and esden committed Nov 13, 2023
1 parent 9cf1372 commit 6143a6b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/windows/serialInterface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct hklmRegistryKey_t
{
DWORD valueLength{0U};
// Figure out how long the value is, stored into `valueLength`
if (const auto result{RegGetValue(handle, nullptr, keyName.data(), RRF_RT_REG_SZ, nullptr, nullptr, &valueLength)};
if (const auto result{RegGetValueA(handle, nullptr, keyName.data(), RRF_RT_REG_SZ, nullptr, nullptr, &valueLength)};
result != ERROR_SUCCESS && result != ERROR_MORE_DATA)
{
displayError(result, "retrieve value for key"sv, keyName);
Expand All @@ -88,12 +88,16 @@ struct hklmRegistryKey_t
// Allocate a string long enough that has been prefilled with nul characters
std::string value(valueLength, '\0');
// Retrieve the actual value of the key now we have all the inforamtion needed to do so
if (const auto result{RegGetValue(handle, nullptr, keyName.data(), RRF_RT_REG_SZ, nullptr, value.data(),
if (const auto result{RegGetValueA(handle, nullptr, keyName.data(), RRF_RT_REG_SZ, nullptr, value.data(),
&valueLength)}; result != ERROR_SUCCESS)
{
displayError(result, "retrieve value for key"sv, keyName);
return {};
}

// After, trim trailing nul characters as there will be 1 or 2
while (!value.empty() && value.back() == '\0')
value.erase(value.end() - 1U);
return value;
}
};
Expand Down

0 comments on commit 6143a6b

Please sign in to comment.