channels/smartcard: avoid crash on SCardStatusA failure

This commit is contained in:
Marc-André Moreau 2014-05-06 11:25:19 -07:00
parent b3114ff4d8
commit 22e2490df7
2 changed files with 23 additions and 11 deletions

View File

@ -701,7 +701,7 @@ static DWORD smartcard_StatusA(SMARTCARD_DEVICE* smartcard, IRP* irp)
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
Status_Call call;
Status_Return ret;
Status_Return ret = { 0 };
DWORD cchReaderLen = 0;
LPSTR mszReaderNames = NULL;
@ -726,8 +726,11 @@ static DWORD smartcard_StatusA(SMARTCARD_DEVICE* smartcard, IRP* irp)
status = ret.ReturnCode = SCardStatusA(hCard, (LPSTR) &mszReaderNames, &cchReaderLen,
&ret.dwState, &ret.dwProtocol, (BYTE*) &ret.pbAtr, &ret.cbAtrLen);
ret.mszReaderNames = (BYTE*) mszReaderNames;
ret.cBytes = cchReaderLen;
if (status == SCARD_S_SUCCESS)
{
ret.mszReaderNames = (BYTE*) mszReaderNames;
ret.cBytes = cchReaderLen;
}
smartcard_trace_status_return(smartcard, &ret, FALSE);

View File

@ -1718,8 +1718,8 @@ UINT32 smartcard_pack_status_return(SMARTCARD_DEVICE* smartcard, wStream* s, Sta
void smartcard_trace_status_return(SMARTCARD_DEVICE* smartcard, Status_Return* ret, BOOL unicode)
{
UINT32 index;
UINT32 length;
int index;
int length;
char* pbAtr = NULL;
char* mszReaderNamesA = NULL;
@ -1728,20 +1728,26 @@ void smartcard_trace_status_return(SMARTCARD_DEVICE* smartcard, Status_Return* r
if (unicode)
{
length = ret->cBytes / 2;
length = (int) ret->cBytes / 2;
ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) ret->mszReaderNames, length, &mszReaderNamesA, 0, NULL, NULL);
}
else
{
length = ret->cBytes;
length = (int) ret->cBytes;
mszReaderNamesA = (char*) malloc(length);
CopyMemory(mszReaderNamesA, ret->mszReaderNames, ret->cBytes);
}
for (index = 0; index < length - 2; index++)
if (!mszReaderNamesA)
length = 0;
if (length > 2)
{
if (mszReaderNamesA[index] == '\0')
mszReaderNamesA[index] = ',';
for (index = 0; index < length - 2; index++)
{
if (mszReaderNamesA[index] == '\0')
mszReaderNamesA[index] = ',';
}
}
pbAtr = winpr_BinToHexString(ret->pbAtr, ret->cbAtrLen, FALSE);
@ -1755,8 +1761,11 @@ void smartcard_trace_status_return(SMARTCARD_DEVICE* smartcard, Status_Return* r
SCardGetCardStateString(ret->dwState), ret->dwState,
SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
WLog_Print(smartcard->log, WLOG_DEBUG, "cBytes: %d mszReaderNames: %s",
if (mszReaderNamesA)
{
WLog_Print(smartcard->log, WLOG_DEBUG, "cBytes: %d mszReaderNames: %s",
ret->cBytes, mszReaderNamesA);
}
WLog_Print(smartcard->log, WLOG_DEBUG,
"cbAtrLen: %d pbAtr: %s", ret->cbAtrLen, pbAtr);