]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
compat: add PTR_ERR_OR_ZERO
authorCasey Connolly <casey.connolly@linaro.org>
Wed, 1 Apr 2026 14:15:21 +0000 (16:15 +0200)
committerTom Rini <trini@konsulko.com>
Tue, 21 Apr 2026 17:19:49 +0000 (11:19 -0600)
Imported from Linux, this is nice to have along with the other ERR_PTR
helper macros.

Signed-off-by: Casey Connolly <casey.connolly@linaro.org>
include/linux/err.h

index 7a0b212c813cbe89a1aa381beee08933be6d143d..2463c4611b29f06cd7b94086b65ad76f7553d7df 100644 (file)
@@ -53,6 +53,31 @@ static inline void * __must_check ERR_CAST(__force const void *ptr)
        return (void *) ptr;
 }
 
+/**
+ * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
+ * @ptr: A potential error pointer.
+ *
+ * Convenience function that can be used inside a function that returns
+ * an error code to propagate errors received as error pointers.
+ * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
+ *
+ * .. code-block:: c
+ *
+ *     if (IS_ERR(ptr))
+ *             return PTR_ERR(ptr);
+ *     else
+ *             return 0;
+ *
+ * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
+ */
+static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
+{
+       if (IS_ERR(ptr))
+               return PTR_ERR(ptr);
+       else
+               return 0;
+}
+
 #endif
 
 #endif /* _LINUX_ERR_H */