Search code examples
cvisual-studiocompiler-warningstry-catch-finally

return status from C finally and C2220


I i am using code as

NTSTATUS
Register (_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType)
{
    NTSTATUS status = STATUS_FLT_DO_NOT_ATTACH;

    try {

        if (VolumeFilesystemType != FLT_FSTYPE_NTFS) {

            status = STATUS_NOT_SUPPORTED;
            leave;
        }

        ...

    }
    finally {
        if (!NT_SUCCESS(status)) {
            KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "AAFileFilter!Failed to register with status: %x\n", status));
            return STATUS_FLT_DO_NOT_ATTACH;
        }
        else
        {
            return  STATUS_SUCCESS;
        }
    }
}

I got C2220 for warning C4532 . If i changing code as

...
    finally {
        if (!NT_SUCCESS(status)) {
            KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "AAFileFilter!Failed to register with status: %x\n", status));
        }
    }

    if (NT_SUCCESS(status)) 
    {
        return STATUS_SUCCESS;
    }
    else
    {
        return STATUS_FLT_DO_NOT_ATTACH;
    }
}
  • Warning gone.. don't undestand what is the reason? The code should exactly works the same in both cases as for me. (I am using VS2013 with WindowsKernelModeDriver8.1 project in C)

Solution

  • The most likely reason why you are getting compiler errors is because this is not valid C.

    • try does not exist in C (but in C++, Java, C#).
    • finally does not exist in C (but in Java, C#).
    • leave; is not valid C unless this is some macro you haven't posted.
    • Functions returning a value, without containing a return statement are invalid in C, since 1999. And they are stupid, since the invention of C.

    You might have to enable non-standard language extensions, or otherwise switch to a strictly conforming C compiler. Visual Studio is infamous for its poor standard compliance.