Search code examples
cwinapisplint

Can't get rid of splint warning "Field used before definition", "rvalue is used that may not be initialized to a value on some execution"


I'm struggling to get rid of a splint warning for the following code:

void GetPrinterInfo(HANDLE hPrinter){
    PRINTER_INFO_4* pPrinterInfo = NULL;
    DWORD bytesNeeded;

    GetPrinter(hPrinter, 4, NULL, 0, &bytesNeeded);

    pPrinterInfo = malloc(bytesNeeded);

    if (GetPrinter(hPrinter, 4, (LPVOID)(pPrinterInfo), bytesNeeded, &bytesNeeded)){
        printf("Printer name: %S", pPrinterInfo->pPrinterName);
    }

    free(pPrinterInfo);
}

The warning is (on the "printf" line):

Field pPrinterInfo->pPrinterName used before definition

1> An rvalue is used that may not be initialized to a value on some execution

Presumably it assumes that the pPrinter hasn't been populated yet. I have tried marking the header definition of SetPrinter with /*@out@*/s etc. but it doesn't help. I've tried a bunch of annotations inside the header file such as /*@temp@*/, /*@dependent@*/ to no avail.

How do I sensibly let splint know that the pPrinter field is expected to be valid after a call to GetPrinter?


Solution

  • It is still not clear why SetPrinter with the out annotation didn't work. However an acceptable workaround (from user3386109) is to just blank the memory so that splint treats it as initialized:

    pPrinterInfo = malloc(bytesNeeded);
    
    ZeroMemory(pPrinterInfo, bytesNeeded);
    
    if (GetPrinter(hPrinter, 4, (LPVOID)(pPrinterInfo), bytesNeeded, &bytesNeeded)){
    

    Where ZeroMemory is just an alias for memset.

    This is not ideal, would be better if no extra actions were undertaken to fix this warning, however it will do for my case.