2009-09-29 16:37:22

by Phil Carmody

[permalink] [raw]
Subject: [PATCH 1/1] err.h: Additional helper function to simplify pointer error checking.

From: Phil Carmody <[email protected]>

There are quite a few instances in the kernel of checks of pointers
both against NULL and against the errno range, handling both cases
identically. This additional helper function would simplify such
code.

Signed-off-by: Phil Carmody <[email protected]>

---
Should apply cleanly to latest 2.6 tree.

The reason I noticed the lack of a helper like this was because I
was chasing down some issues where error returns were being handled
incorrectly. I concluded that a catch-all doesn't-point-to-an-object
test would be useful for pointers, so that such errors would be
harder to make.

There are possibly 50 instances in the code already of a verbatim
combined check, such as:
drivers/base/core.c: if (class == NULL || IS_ERR(class))
most from a few architecture-specific trees. I would be prepared
to create a broader-reaching patchset which migrates more verbose
checks to this simpler one if that would be desirable.

Aside - the test used isn't computationally optimal. Comparing
unlikely((unsigned long)-x <= MAX_ERRNO) may be quicker on some
archs. I have favoured the more self-explanatory and portable
approach in this patch.
---
include/linux/err.h | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/include/linux/err.h b/include/linux/err.h
index ec87f31..fcc836c 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -34,6 +34,11 @@ static inline long IS_ERR(const void *ptr)
return IS_ERR_VALUE((unsigned long)ptr);
}

+static inline long IS_ERR_OR_NULL(const void *ptr)
+{
+ return (ptr == NULL) || IS_ERR_VALUE((unsigned long)ptr);
+}
+
/**
* ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
* @ptr: The pointer to cast.
--
1.5.4.3