summaryrefslogtreecommitdiff
path: root/include/linux/err.h
diff options
context:
space:
mode:
authorCasey Connolly <[email protected]>2026-04-01 16:15:21 +0200
committerTom Rini <[email protected]>2026-04-21 11:19:49 -0600
commit3f6f674b4a5653eb7223645be00ddb82368ddb33 (patch)
tree30439ecf02331cfb54697372202fed135ecb1dd9 /include/linux/err.h
parent45c610d718bd605a1b3bdf99ec0386620b54b559 (diff)
compat: add PTR_ERR_OR_ZERO
Imported from Linux, this is nice to have along with the other ERR_PTR helper macros. Signed-off-by: Casey Connolly <[email protected]>
Diffstat (limited to 'include/linux/err.h')
-rw-r--r--include/linux/err.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/include/linux/err.h b/include/linux/err.h
index 7a0b212c813..2463c4611b2 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -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 */