none
Code analyzer warning: C6102 (Visual Studio 2013) RRS feed

  • Question

  • Dear all,

    I have developed application, that would get the device name and opens handle to the device. I have run the code analysis in Visual Studio 2012. There were no issues. I have migrated to Visual Studio 2013 and warning started prompting.

    Issue:

    C6102 Using 'dwRequiredSize' from failed function call at line '73'.

    dwRequiredSize' is not initialized 73

    Enter this branch, (assume '<branch condition>') 75
    'dwRequiredSize' is used, but may not have been initialized 77

    Code snippet:

        if (NULL == *devicePath)
        {
            SP_DEVICE_INTERFACE_DATA            sDeviceInterface = { 0 };
            PSP_DEVICE_INTERFACE_DETAIL_DATA    spDeviceInterfaceDetail = NULL;

            hDeviceInfo = SetupDiGetClassDevsEx(&DEVICE_GUID, NULL, NULL, (DIGCF_DEVICEINTERFACE | DIGCF_PRESENT), NULL, NULL, NULL);
            if (INVALID_HANDLE_VALUE == hDeviceInfo)
            {
                hDeviceInfo = NULL;
                return;
            }

            sDeviceInterface.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);

            fResult = SetupDiEnumDeviceInterfaces(hDeviceInfo, NULL, &DEVICE_GUID, 0, &sDeviceInterface);

            if (TRUE == fResult)
            {
    <Line 73>            fResult = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &sDeviceInterface, NULL, 0, &dwRequiredSize, NULL);

    <Line 75>             if ((FALSE == fResult) && (ERROR_INSUFFICIENT_BUFFER == GetLastError()))
                {
                    spDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(dwRequiredSize);
                    if (NULL != spDeviceInterfaceDetail)
                    {
                        spDeviceInterfaceDetail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

    <Line 77>                     fResult = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &sDeviceInterface, spDeviceInterfaceDetail, dwRequiredSize, &dwRequiredSize, NULL);

                        if (TRUE == fResult)
                        {
                            *devicePath = _strdup(spDeviceInterfaceDetail->DevicePath);
                            LOG("%s : %s", __FUNCTION__, *devicePath);
                        }
                        FREE_ALLOCATION(spDeviceInterfaceDetail);
                    }
                }

                SetupDiDestroyDeviceInfoList(hDeviceInfo);
            }
            else
            {
                SetupDiDestroyDeviceInfoList(hDeviceInfo);
            }

    Thanks In advance.

    Regards,

    Prakash.

    Thursday, August 21, 2014 12:06 PM

All replies

  • why not just initialize dwRequiredSize to zero?

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, August 21, 2014 5:10 PM
  • I have done the initialization of the variable to ZERO(0). But still getting the same issue.

    I have initialized dwRequiredSize before  if (NULL == *devicePath) 

    dwRequiredSize = 0;

    Friday, August 22, 2014 6:02 AM
  • have you tried initializing it at the point of declaration?

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, August 22, 2014 6:28 AM
  • Dear Doron Holan,

    Yes. I have initialized the variable @ the start of the function and before calling the SetupDiGetDeviceInterfaceDetail function also.

    Generally SetupDiGetDeviceInterfaceDetail is used with buffers NULL to get the required size and then allocate those many bytes and input the allocated buffer.

    I guess there is deadlock between code analyzer and the function behavior. Please suggest.

    Thanks,

    Prakash Manannavar.

    Tuesday, August 26, 2014 6:41 AM