[PATCH v2 1/3] err.h: add INIT_ERR_PTR macro

Christian Marangi posted 3 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH v2 1/3] err.h: add INIT_ERR_PTR macro
Posted by Christian Marangi 3 months, 1 week ago
Add INIT_ERR_PTR macro to initialize static variables with error
pointers. This might be useful for specific case where there is a static
variable initialized to an error condition and then later set to the
real handle once probe finish/completes.

This is to handle compilation problems like:

error: initializer element is not constant

where ERR_PTR can't be used.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 include/linux/err.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/include/linux/err.h b/include/linux/err.h
index 1d60aa86db53..8c37be0620ab 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -41,6 +41,14 @@ static inline void * __must_check ERR_PTR(long error)
 	return (void *) error;
 }
 
+/**
+ * INIT_ERR_PTR - Init a const error pointer.
+ * @error: A negative error code.
+ *
+ * Like ERR_PTR(), but usable to initialize static variables.
+ */
+#define INIT_ERR_PTR(error) ((void *)(error))
+
 /* Return the pointer in the percpu address space. */
 #define ERR_PTR_PCPU(error) ((void __percpu *)(unsigned long)ERR_PTR(error))
 
-- 
2.51.0
Re: [PATCH v2 1/3] err.h: add INIT_ERR_PTR macro
Posted by Andy Shevchenko 3 months, 1 week ago
On Fri, Oct 31, 2025 at 02:08:32PM +0100, Christian Marangi wrote:
> Add INIT_ERR_PTR macro to initialize static variables with error

INIT_ERR_PTR()

> pointers. This might be useful for specific case where there is a static
> variable initialized to an error condition and then later set to the
> real handle once probe finish/completes.
> 
> This is to handle compilation problems like:
> 
> error: initializer element is not constant
> 
> where ERR_PTR can't be used.

ERR_PTR()

In case you need a new version, please address the above.
Anyways, LGTM now,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko