none
SetupDiGetDeviceRegistryProperty() Analyzer problem RRS feed

  • Question

  • The following code does not pass the VS2015 Analyzer:

        LPTSTR buffer = NULL;
        DWORD reqSize = 0;
        BOOL bRegProp;
    
    
    	// Getting the size of required buffer
    	bRegProp = SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Prop, NULL, NULL, 0, &reqSize);
    	DWORD err = GetLastError();
    
    	if (err != ERROR_INSUFFICIENT_BUFFER)
             return NULL;
    
    	// Allocate buffer according to required size
        buffer = new TCHAR[(reqSize /sizeof(TCHAR))+2];
        if(!buffer)
            return NULL;
    
    	// Get the string into the buffer 
    	if (!SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Prop, NULL, (LPBYTE)buffer, reqSize, NULL))
    		return NULL;

    The first call to SetupDiGetDeviceRegistryProperty() always returns FALSE.

    However, when the last error is ERROR_INSUFFICIENT_BUFFER  (0x7A) then the value in reqSize is correct. The code hence is perfectly functional. The Analyzer however does not approve of it:

    Should I just ignore it?


    Sunday, January 3, 2016 7:52 AM

Answers

  • However, when the last error is ERROR_INSUFFICIENT_BUFFER  (0x7A) then the value in reqSize is correct.

    According to the current documentation on SetupDiGetDeviceRegistryProperty, this is not the documented behavior.

    This error value is not mentioned there as the condition to get the required size. Try to massage your code to fit the documentation.

    -- pa

    • Marked as answer by shaul_ei Sunday, January 3, 2016 3:48 PM
    Sunday, January 3, 2016 2:12 PM

All replies

  • However, when the last error is ERROR_INSUFFICIENT_BUFFER  (0x7A) then the value in reqSize is correct.

    According to the current documentation on SetupDiGetDeviceRegistryProperty, this is not the documented behavior.

    This error value is not mentioned there as the condition to get the required size. Try to massage your code to fit the documentation.

    -- pa

    • Marked as answer by shaul_ei Sunday, January 3, 2016 3:48 PM
    Sunday, January 3, 2016 2:12 PM
  • Hey Pavel, True. It used to in the pre-SAL days.

    However, I follow the instructions of how to allocate the output buffer:

    PropertyBuffer [out, optional]
    A pointer to a buffer that receives the property that is being retrieved. If this parameter is set to NULL, 
    and PropertyBufferSize is also set to zero, the function returns the required size for the buffer in RequiredSize. PropertyBufferSize [in] The size, in bytes, of the PropertyBuffer buffer. RequiredSize [out, optional] A pointer to a variable of type DWORD that receives the required size, in bytes,
    of the PropertyBuffer buffer that is required to hold the data for the requested property.
    This parameter is optional and can be NULL.

    I wonder whether to ignore the warning or to provide a way-too-large a buffer (say 8K) to hold the output string. 


    • Edited by shaul_ei Sunday, January 3, 2016 2:57 PM typo
    Sunday, January 3, 2016 2:56 PM
  • 8K is a trivial size for temporary storage in usermode. Give it even 16K.

    -- pa

    Sunday, January 3, 2016 3:02 PM