Skip to content

Commit

Permalink
fixed stack overflow caused by T2A/USES_CONVERSION macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-V-K committed Jan 20, 2022
1 parent 33f16c4 commit 3e12691
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 28 additions & 6 deletions LogonHoursService/WorkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
#pragma comment(lib, "Netapi32.lib") // NetUserGetInfo(), etc.
#pragma comment(lib, "Wtsapi32.lib") // WTSEnumerateSessions(), etc.

// Converts wide string to multibyte string, allocating memory for new string
LPSTR W2A(LPCWSTR lpwszStrIn)
{
if (!lpwszStrIn)
return NULL;
const size_t nInputStrLen = wcslen(lpwszStrIn);

// Double NULL termination
const int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
const LPSTR pszOut = new char[nOutputStrLen];

if (pszOut)
{
memset(pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
return pszOut;
}

WorkerData g_WorkerData;

enum EDayOfWeek
Expand Down Expand Up @@ -123,8 +142,6 @@ WorkerData::WorkerData()

DWORD WINAPI WorkerThread(LPVOID lpData)
{
USES_CONVERSION;

LOG_DEBUG(__func__) << "Entry";

WorkerData* const pData = reinterpret_cast<WorkerData*>(lpData);
Expand Down Expand Up @@ -152,20 +169,24 @@ DWORD WINAPI WorkerThread(LPVOID lpData)
const auto session_id = pSessionInfo[i].SessionId;
LPWSTR pBuffer;
DWORD bytes;
LOG_DEBUG(__func__) << "Starting WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, " << session_id << ", WTSConnectState, ...)";
if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, session_id, WTSConnectState, &pBuffer, &bytes))
continue;
const WTS_CONNECTSTATE_CLASS session_state = *reinterpret_cast<WTS_CONNECTSTATE_CLASS*>(pBuffer);
WTSFreeMemory(pBuffer);
if (session_state != WTSActive)
continue;
LOG_DEBUG(__func__) << "Starting WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, " << session_id << ", WTSUserName, ...)";
if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, session_id, WTSUserName, &pBuffer, &bytes))
{
if (wcslen(pBuffer))
{
const std::wstring username(pBuffer);
const std::wstring wusername(pBuffer);
const std::unique_ptr<char[]> username(W2A(wusername.c_str()));
LOG_DEBUG(__func__) << "WTSQuerySessionInformation: active session " << session_id
<< ", user '" << T2A(username.c_str()) << '\'';
sessions.push_back({ session_id, username });
<< " '" << username.get() << '\''
;
sessions.push_back({ session_id, wusername });
}
WTSFreeMemory(pBuffer);
}
Expand All @@ -187,9 +208,10 @@ DWORD WINAPI WorkerThread(LPVOID lpData)
const LogonHours hours(userinfo2->usri2_logon_hours);
NetApiBufferFree(bufptr);

const char* const username = T2A(session.user.c_str());
if (!hours.All())
{
const std::unique_ptr<char[]> username(W2A(wusername.c_str()));

LOG_DEBUG(__func__) << "checking logon hours for session " << session.id << " '" << username << "'";

SYSTEMTIME systime; GetSystemTime(&systime); // UTC/GMT
Expand Down
2 changes: 0 additions & 2 deletions LogonHoursService/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
//#include <iostream>
#include <string>

#include <AtlConv.h> // T2A() and other string convesions macros

//#include <log4cpp/Appender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
Expand Down

0 comments on commit 3e12691

Please sign in to comment.