The issue of checking if pointer is NULL or "zero size" in Linux kernel

Viewed 631

In Linux kernel sources there is some indicator called ZERO_SIZE_PTR to indicate zero sized kmalloc requests. But also there is a macro ZERO_OR_NULL_PTR() which checks some pointer if it's "zero size" or "NULL". The code:

/*
 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
 *
 * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
 *
 * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
 * Both make kfree a no-op.
 */
#define ZERO_SIZE_PTR ((void *)16)

#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
                (unsigned long)ZERO_SIZE_PTR)

The check looks like ptr <= (void *)16.
In simple terms, it means that NULL-pointer is the pointer from 0 to 15, and zero size pointer is 16.
So pointers from 17 and higher are supposed to be valid. Is that proper that 15 is invalid (NULL) but 17 is valid? It seems to me that valid pointers do not start from 17, but from higher numbers.

I mean perhaps it will be more logical to check such way x == NULL || x == ZERO_SIZE_PTR (without involving 1-15 range).

0 Answers
Related