---
title: Nonnull return value violation
framework: xcode
role: article
role_heading: Article
path: xcode/nonnull-return-value-violation
---

# Nonnull return value violation

Detects when a function incorrectly returns null.

## Overview

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. note: The nonnull violation check for return types with the _Nonnull annotation is off by default. You can turn it on by enabling the -fsanitize=nullability-return compiler flag. 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

- [Misaligned pointer](xcode/misaligned-pointer.md)
- [Invalid Boolean value](xcode/invalid-boolean.md)
- [Out-of-bounds array access](xcode/out-of-bounds-array-access.md)
- [Invalid enumeration value](xcode/invalid-enumeration-value.md)
- [Reaching of unreachable point](xcode/reaching-of-unreachable-point.md)
- [Dynamic type violation](xcode/dynamic-type-violation.md)
- [Invalid float cast](xcode/invalid-float-cast.md)
- [Division by zero](xcode/division-by-zero.md)
- [Nonnull argument violation](xcode/nonnull-argument-violation.md)
- [Nonnull variable assignment violation](xcode/nonnull-variable-assignment-violation.md)
- [Null reference creation and null pointer dereference](xcode/null-reference-creation-and-null-pointer-dereference.md)
- [Invalid object size](xcode/invalid-object-size.md)
- [Invalid shift](xcode/invalid-shift.md)
- [Integer overflow](xcode/integer-overflow.md)
- [Invalid variable-length array](xcode/invalid-variable-length-array.md)
