VirtualBox

Ignore:
Timestamp:
Mar 14, 2014 11:29:38 PM (11 years ago)
Author:
vboxsync
Message:

VBoxUSB/VBoxUsbLib-win.cpp: String descriptors are optional, don't assert in debug builds.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/VBoxUSB/win/lib/VBoxUsbLib-win.cpp

    r44529 r50794  
    55
    66/*
    7  * Copyright (C) 2011-2012 Oracle Corporation
     7 * Copyright (C) 2011-2014 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    582582static int usbLibDevStrDrEntryGet(HANDLE hHub, ULONG iPort, ULONG iDr, USHORT idLang, PVBOXUSB_STRING_DR_ENTRY *ppList)
    583583{
    584     char Buf[sizeof (USB_DESCRIPTOR_REQUEST) + MAXIMUM_USB_STRING_LENGTH];
    585     PUSB_DESCRIPTOR_REQUEST pRq = (PUSB_DESCRIPTOR_REQUEST)Buf;
    586     PUSB_STRING_DESCRIPTOR pDr = (PUSB_STRING_DESCRIPTOR)(Buf + sizeof (*pRq));
    587     memset (&Buf, 0, sizeof (Buf));
     584    char szBuf[sizeof (USB_DESCRIPTOR_REQUEST) + MAXIMUM_USB_STRING_LENGTH];
     585    RT_ZERO(szBuf);
     586
     587    PUSB_DESCRIPTOR_REQUEST pRq = (PUSB_DESCRIPTOR_REQUEST)szBuf;
     588    PUSB_STRING_DESCRIPTOR pDr = (PUSB_STRING_DESCRIPTOR)(szBuf + sizeof (*pRq));
     589    RT_BZERO(pDr, sizeof(USB_STRING_DESCRIPTOR));
     590
    588591    pRq->ConnectionIndex = iPort;
    589592    pRq->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | iDr;
    590593    pRq->SetupPacket.wIndex = idLang;
    591     pRq->SetupPacket.wLength = sizeof (Buf) - sizeof (*pRq);
     594    pRq->SetupPacket.wLength = sizeof (szBuf) - sizeof (*pRq);
     595
    592596    DWORD cbReturned = 0;
    593     if (!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, pRq, sizeof (Buf),
    594                                     pRq, sizeof (Buf),
    595                                     &cbReturned, NULL))
    596     {
    597         DWORD winEr = GetLastError();
    598         LogRel((__FUNCTION__": DeviceIoControl 1 fail winEr (%d)\n", winEr));
    599 #ifdef VBOX_WITH_ANNOYING_USB_ASSERTIONS
    600             AssertFailed();
    601 #endif
    602         return VERR_GENERAL_FAILURE;
    603     }
    604 
    605     if (cbReturned < sizeof (*pDr) + 2)
    606     {
    607         AssertFailed();
    608         return VERR_GENERAL_FAILURE;
    609     }
    610 
    611     if (!!(pDr->bLength % 2))
    612     {
    613         AssertFailed();
    614         return VERR_GENERAL_FAILURE;
    615     }
    616 
    617     if (pDr->bLength != cbReturned - sizeof (*pRq))
    618     {
    619         AssertFailed();
    620         return VERR_GENERAL_FAILURE;
    621     }
    622 
     597    if (!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, pRq, sizeof (szBuf),
     598                         pRq, sizeof(szBuf),
     599                         &cbReturned, NULL))
     600    {
     601        DWORD dwErr = GetLastError();
     602        AssertMsgFailed(("Getting USB descriptor failed with error %ld\n", dwErr));
     603        return RTErrConvertFromWin32(dwErr);
     604    }
     605
     606    /* Wrong descriptor type at the requested port index? Bail out. */
    623607    if (pDr->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE)
    624     {
    625         AssertFailed();
    626         return VERR_GENERAL_FAILURE;
    627     }
    628 
    629     PVBOXUSB_STRING_DR_ENTRY pEntry = (PVBOXUSB_STRING_DR_ENTRY)RTMemAllocZ(sizeof (*pEntry) + pDr->bLength + 2);
    630     Assert(pEntry);
     608        return VERR_NOT_FOUND;
     609
     610    /* Some more sanity checks. */
     611    if (   (cbReturned < sizeof (*pDr) + 2)
     612        || (!!(pDr->bLength % 2))
     613        || (pDr->bLength != cbReturned - sizeof(*pRq)))
     614    {
     615        AssertMsgFailed(("Sanity check failed for string descriptor: cbReturned=%RI32, cbDevReq=%zu, type=%RU8, len=%RU8, port=%RU32, index=%RU32, lang=%RU32\n",
     616                         cbReturned, sizeof(*pRq), pDr->bDescriptorType, pDr->bLength, iPort, iDr, idLang));
     617        return VERR_INVALID_PARAMETER;
     618    }
     619
     620    PVBOXUSB_STRING_DR_ENTRY pEntry =
     621        (PVBOXUSB_STRING_DR_ENTRY)RTMemAllocZ(sizeof(VBOXUSB_STRING_DR_ENTRY) + pDr->bLength + 2);
     622    AssertPtr(pEntry);
    631623    if (!pEntry)
    632     {
    633         return VERR_OUT_OF_RESOURCES;
    634     }
     624        return VERR_NO_MEMORY;
    635625
    636626    pEntry->pNext = *ppList;
     
    638628    pEntry->idLang = idLang;
    639629    memcpy(&pEntry->StrDr, pDr, pDr->bLength);
     630
    640631    *ppList = pEntry;
     632
    641633    return VINF_SUCCESS;
    642634}
     
    668660static int usbLibDevStrDrEntryGetAll(HANDLE hHub, ULONG iPort, PUSB_DEVICE_DESCRIPTOR pDevDr, PUSB_CONFIGURATION_DESCRIPTOR pCfgDr, PVBOXUSB_STRING_DR_ENTRY *ppList)
    669661{
     662    /* Read string descriptor zero to determine what languages are available. */
    670663    int rc = usbLibDevStrDrEntryGet(hHub, iPort, 0, 0, ppList);
    671 #ifdef VBOX_WITH_ANNOYING_USB_ASSERTIONS
    672     AssertRC(rc);
    673 #endif
    674664    if (RT_FAILURE(rc))
    675665        return rc;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette