Refactor setting of lpnSize when buffer size is insufficient

This commit is contained in:
Andrew Schwartzmeyer 2015-07-22 16:36:19 -07:00
parent 865fd0a56b
commit d589ac1cbf

View file

@ -85,9 +85,15 @@ BOOL GetUserName(WCHAR_T *lpBuffer, LPDWORD lpnSize)
std::vector<unsigned char> output;
SCXCoreLib::Utf8ToUtf16le(username, output);
if (output.size()/2 + 1 > *lpnSize) {
// The length is the number of characters in the string, which
// is half the string size because UTF-16 encodes two bytes
// per character, plus one for the trailing null.
const DWORD length = output.size()/2 + 1;
if (length > *lpnSize) {
errno = ERROR_INSUFFICIENT_BUFFER;
*lpnSize = output.size()/2 + 1;
// Set lpnSize if buffer is too small to inform user
// of necessary size
*lpnSize = length;
return 0;
}