Fix smartcard segfault when no devices are present (#5364)
authorr-barnett <43044166+r-barnett@users.noreply.github.com>
Thu, 2 May 2019 04:11:57 +0000 (23:11 -0500)
committerakallabeth <akallabeth@users.noreply.github.com>
Thu, 2 May 2019 04:11:57 +0000 (06:11 +0200)
* 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

index 759085d..fcc3262 100644 (file)
@@ -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)))