[PATCH RFC] err.h: document that PTR_ERR should only be used if IS_ERR returns true

From: Uwe Kleine-KÃnig
Date: Sun Oct 14 2018 - 16:28:14 EST


I have no idea about the rationale, but that's what LDD3 recommends.

Signed-off-by: Uwe Kleine-KÃnig <uwe@xxxxxxxxxxxxxxxxx>
---
Hello,

during a review I claimed that PTR_ERR should only be used if IS_ERR was
already checked. The rationale isn't obvious though and Thierry
suggested to keep the code as is and not introduce an IS_ERR check.

I found in Linux Device Drivers 3[1]:

You should use PTR_ERR only on a value for which IS_ERR returns
a true value; any other value is a valid pointer

I wonder if there is a relevant reason that LDD3 suggests to check
IS_ERR first, maybe something like "On an Alpha it is important because
not doing it results in a bus error there." There are no details
mentioned there however. If there is a reason, this patch should be
adapted such that the comment includes it.

Any ideas?

Best regards
Uwe

[1] https://static.lwn.net/images/pdf/LDD3/ch11.pdf, on page 295

include/linux/err.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/linux/err.h b/include/linux/err.h
index 87be24350e91..8f052983108e 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -26,6 +26,9 @@ static inline void * __must_check ERR_PTR(long error)
return (void *) error;
}

+/*
+ * You should use PTR_ERR only on a value for which IS_ERR returns a true value.
+ */
static inline long __must_check PTR_ERR(__force const void *ptr)
{
return (long) ptr;
--
2.19.1