Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string size for error message generated by windows_category #966

Merged
merged 1 commit into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Release/src/utilities/asyncrt_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ std::string windows_category_impl::message(int errorCode) const CPPREST_NOEXCEPT
}
#endif

std::wstring buffer;
buffer.resize(buffer_size);
std::wstring buffer(buffer_size, 0);

const auto result = ::FormatMessageW(
dwFlags,
Expand All @@ -277,11 +276,15 @@ std::string windows_category_impl::message(int errorCode) const CPPREST_NOEXCEPT
buffer_size,
NULL);


if (result == 0)
{
return "Unable to get an error message for error code: " + std::to_string(errorCode) + ".";
}

// strip exceeding characters of the initial resize call
buffer.resize(result);

return utility::conversions::to_utf8string(buffer);
}

Expand Down
11 changes: 11 additions & 0 deletions Release/tests/functional/utils/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ TEST(scan_string_locale, "Ignore:Android", "Locale unsupported on Android")
}
}


#ifdef _WIN32
TEST(windows_category_message)
{
// Ensure the error message string returned by windows_category doesn't contain trailing zeros.
std::string error_message = utility::details::windows_category().message( 0 );
std::string zero_terminated_copy = error_message.c_str();
VERIFY_ARE_EQUAL( zero_terminated_copy, error_message );
}
#endif // _WIN32

}

}}} //namespaces