Edit

Share via


Warning C6387

'argument' may be 'value': this does not adhere to the specification for the function 'function name': Lines: x, y

Remarks

This warning is raised if an annotated function parameter is being passed an unexpected value. For example, passing a potentially null value to a parameter that is marked with _In_ annotation generates this warning.

This commonly happens when one function is annotated with _Post_ _Null_ on its return value (meaning it may return null), and the result is then passed to another function that expects a non-null _In_ parameter. To fix the issue, either change the first function's annotation to _Post_ _Notnull_ if it truly never returns null, or add a null check before passing the value to the second function.

Note

The SAL annotations (_Post_ _Null_, _Post_ _Notnull_, _In_, and so on) are used on function declarations to describe the contract of the function's parameters and return values. They do not apply to local variable declarations.

Code analysis name: INVALID_PARAM_VALUE_1

Example

The following code generates this warning because the function g is annotated as potentially returning null (_Post_ _Null_), and the result is passed to f, which requires a non-null input (_In_):

#include <sal.h>
#include <cstdio>

_Post_ _Null_ char* g()
{
    return nullptr;
}

void f(_In_ char* pch)
{
    printf("%s\n", pch);;
}

int main()
{
    char* pCh = g();
    f(pCh); // Warning C6387
}

To correct this warning, you can change the annotation on g if it never actually returns null:

#include <sal.h>
#include <cstdio>

_Post_ _Notnull_ char* g()
{
    static char buf[] = "hello";
    return buf;
}

void f(_In_ char* pch)
{
    printf("%s\n", pch);
}

int main()
{
    char* pCh = g();
    f(pCh); // No warning C6387
}

Alternatively, you can add a null check before passing the value:

#include <sal.h>
#include <cstdio>

_Post_ _Null_ char* g()
{
    return nullptr;
}
    
void f(_In_ char* pch)
{
    printf("%s\n", pch);
}

int main()
{
    char* pCh = g();
    if (pCh != nullptr)
    {
        f(pCh); // No warning C6387
    }
}

See also

strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l