Annotating function parameters and return values (original) (raw)

This article describes typical uses of annotations for simple function parameters—scalars, and pointers to structures and classes—and most kinds of buffers. This article also shows common usage patterns for annotations. For additional annotations that are related to functions, see Annotating function behavior.

Pointer parameters

For the annotations in the following table, when a pointer parameter is annotated, the analyzer reports an error if the pointer is null. This annotation applies to pointers and to any data item that's pointed to.

Annotations and descriptions

typedef _Null_terminated_ wchar_t *PWSTR;  
void MyStringCopy(_Out_writes_(size) PWSTR p1, _In_ size_t size, _In_ PWSTR p2);  

In this example, the caller provides a buffer of size elements for p1. MyStringCopy makes some of those elements valid. More importantly, the _Null_terminated_ annotation on PWSTR means that p1 is null-terminated in post-state. In this way, the number of valid elements is still well-defined, but a specific element count isn't required.
The _bytes_ variant gives the size in bytes instead of elements. Use this variant only when the size can't be expressed as elements. For example, char strings would use the _bytes_ variant only if a similar function that uses wchar_t would.

void *memcpy(_Out_writes_bytes_all_(s) char *p1, _In_reads_bytes_(s) char *p2, _In_ int s);  
void *wordcpy(_Out_writes_all_(s) DWORD *p1, _In_reads_(s) DWORD *p2, _In_ int s);  
int ReadAllElements(_In_reads_to_ptr_(EndOfArray) const int *Array, const int *EndOfArray);  

Optional pointer parameters

When a pointer parameter annotation includes _opt_, it indicates that the parameter may be null. Otherwise, the annotation behaves the same as the version that doesn't include _opt_. Here is a list of the _opt_ variants of the pointer parameter annotations:

_In_opt_
_Out_opt_
_Inout_opt_
_In_opt_z_
_Inout_opt_z_
_In_reads_opt_
_In_reads_bytes_opt_
_In_reads_opt_z_

_Out_writes_opt_
_Out_writes_opt_z_
_Inout_updates_opt_
_Inout_updates_bytes_opt_
_Inout_updates_opt_z_
_Out_writes_to_opt_
_Out_writes_bytes_to_opt_
_Out_writes_all_opt_
_Out_writes_bytes_all_opt_

_Inout_updates_to_opt_
_Inout_updates_bytes_to_opt_
_Inout_updates_all_opt_
_Inout_updates_bytes_all_opt_
_In_reads_to_ptr_opt_
_In_reads_to_ptr_opt_z_
_Out_writes_to_ptr_opt_
_Out_writes_to_ptr_opt_z_

Output pointer parameters

Output pointer parameters require special notation to disambiguate nullness on the parameter and the pointed-to location.

Annotations and descriptions

Important

If the interface that you are annotating is COM, use the COM form of these annotations. Don't use the COM annotations with any other type interface.

Certain interface conventions presume that output parameters are nullified on failure. Except for explicitly COM code, the forms in the following table are preferred. For COM code, use the corresponding COM forms that are listed in the previous section.

Output reference parameters

A common use of the reference parameter is for output parameters. For simple output reference parameters such as int&, _Out_ provides the correct semantics. However, when the output value is a pointer such as int *&, the equivalent pointer annotations like _Outptr_ int ** don't provide the correct semantics. To concisely express the semantics of output reference parameters for pointer types, use these composite annotations:

Annotations and descriptions

Return values

The return value of a function resembles an _Out_ parameter but is at a different level of de-reference, and you don't have to consider the concept of the pointer to the result. For the following annotations, the return value is the annotated object—a scalar, a pointer to a struct, or a pointer to a buffer. These annotations have the same semantics as the corresponding _Out_ annotation.

_Ret_z_
_Ret_writes_(s)
_Ret_writes_bytes_(s)
_Ret_writes_z_(s)
_Ret_writes_to_(s,c)
_Ret_writes_maybenull_(s)
_Ret_writes_to_maybenull_(s)
_Ret_writes_maybenull_z_(s)

_Ret_maybenull_
_Ret_maybenull_z_
_Ret_null_
_Ret_notnull_
_Ret_writes_bytes_to_
_Ret_writes_bytes_maybenull_
_Ret_writes_bytes_to_maybenull_

Format string parameters

int MyPrintF(_Printf_format_string_ const wchar_t* format, ...)  
{  
       va_list args;  
       va_start(args, format);  
       int ret = vwprintf(format, args);  
       va_end(args);  
       return ret;  
}  
int MyScanF(_Scanf_format_string_ const wchar_t* format, ...)  
{  
       va_list args;  
       va_start(args, format);  
       int ret = vwscanf(format, args);  
       va_end(args);  
       return ret;  
}  
int MyScanF_s(_Scanf_s_format_string_ const wchar_t* format, ...)  
{  
       va_list args;  
       va_start(args, format);  
       int ret = vwscanf_s(format, args);  
       va_end(args);  
       return ret;  
}  

Other common annotations

Annotations and descriptions

See also