October 2017
Intermediate to advanced
586 pages
14h 8m
English
When it comes to returning an error from functions that are supposed to return a pointer, functions often return the NULL pointer. It is a working but quite meaningless approach, since you do not exactly know why this null pointer is returned. For that purpose, the kernel provides three functions, ERR_PTR, IS_ERR, and PTR_ERR:
void *ERR_PTR(long error); long IS_ERR(const void *ptr); long PTR_ERR(const void *ptr);
The first actually returns the error value as a pointer. Given a function that is likely to return -ENOMEM after a failed memory allocation, we have to do something such as return ERR_PTR(-ENOMEM);. The second is used to check whether the returned value is a pointer error or not, if (IS_ERR(foo)). The ...