From 40ef08ddc5213478be6a697a608ba26956b78a4a Mon Sep 17 00:00:00 2001 From: r-barnett <43044166+r-barnett@users.noreply.github.com> Date: Wed, 1 May 2019 23:11:57 -0500 Subject: [PATCH] Fix smartcard segfault when no devices are present (#5364) * Don't assume smartcard reader list is not empty. * Check status of retrieval of smartcard device list before trying to filter it * style fixes --- channels/smartcard/client/smartcard_operations.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/channels/smartcard/client/smartcard_operations.c b/channels/smartcard/client/smartcard_operations.c index 759085d41..fcc326200 100644 --- a/channels/smartcard/client/smartcard_operations.c +++ b/channels/smartcard/client/smartcard_operations.c @@ -457,7 +457,7 @@ static DWORD filter_device_by_name_a(wLinkedList* list, LPSTR* mszReaders, DWORD { size_t rpos = 0, wpos = 0; - if (LinkedList_Count(list) < 1) + if (*mszReaders == NULL || LinkedList_Count(list) < 1) return cchReaders; do @@ -547,9 +547,6 @@ static LONG smartcard_ListReadersA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_O cchReaders = SCARD_AUTOALLOCATE; status = ret.ReturnCode = SCardListReadersA(operation->hContext, (LPCSTR) call->mszGroups, (LPSTR) &mszReaders, &cchReaders); - cchReaders = filter_device_by_name_a(smartcard->names, &mszReaders, cchReaders); - ret.msz = (BYTE*) mszReaders; - ret.cBytes = cchReaders; if (call->mszGroups) { @@ -563,6 +560,9 @@ static LONG smartcard_ListReadersA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_O return status; } + cchReaders = filter_device_by_name_a(smartcard->names, &mszReaders, cchReaders); + ret.msz = (BYTE*) mszReaders; + ret.cBytes = cchReaders; smartcard_trace_list_readers_return(smartcard, &ret, FALSE); if ((status = smartcard_pack_list_readers_return(smartcard, irp->output, &ret))) @@ -610,9 +610,6 @@ static LONG smartcard_ListReadersW_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_O cchReaders = SCARD_AUTOALLOCATE; status = ret.ReturnCode = SCardListReadersW(operation->hContext, (LPCWSTR) call->mszGroups, (LPWSTR) &mszReaders, &cchReaders); - cchReaders = filter_device_by_name_w(smartcard->names, &mszReaders, cchReaders); - ret.msz = (BYTE*) mszReaders; - ret.cBytes = cchReaders * 2; if (call->mszGroups) { @@ -626,6 +623,9 @@ static LONG smartcard_ListReadersW_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_O return status; } + cchReaders = filter_device_by_name_w(smartcard->names, &mszReaders, cchReaders); + ret.msz = (BYTE*) mszReaders; + ret.cBytes = cchReaders * 2; smartcard_trace_list_readers_return(smartcard, &ret, TRUE); if ((status = smartcard_pack_list_readers_return(smartcard, irp->output, &ret)))