Contents

Nonnull return value violation

Detects when a function incorrectly returns null.

Overview

Use this check to detect when a function with the returns_nonnull attribute, or a function with a return type that has the _Nonnull annotation, returns null. Available in Xcode 9 and later.

Violation of the nonnull attribute for a function in C

In the following code, there is a violation of the returns_nonnull attribute of the nonnull_returning_function function:

__attribute__((returns_nonnull)) int *nonnull_returning_function(int *p) {
    return p; // Warning: NULL can be returned here
}
nonnull_returning_function(NULL); // Error: nonnull return value attribute violation

Solution

Correct logic errors, add any necessary null guards to the function, or remove the returns_nonnull attribute and rework the function caller logic accordingly.

Violation of the nonnull annotation for a return type in C

The following code violates the _Nonnull annotation of the return type for the nonnull_returning_function function:

int *_Nonnull nonnull_returning_function(int *p) {
    return p; // Warning: NULL can be returned here
}
nonnull_returning_function(NULL); // Error: nonnull return value attribute violation

Solution

Correct logic errors, add any necessary null guards to the function, or remove the _Nonnull annotation and rework the function caller logic accordingly.

See Also

Undefined Behavior Sanitizer