user14189198
user14189198

Reputation: 173

Passing a NULL pointer to kfree

What happens if we pass a NULL pointer to kernel free? Which used to free memory allocated by kmalloc. Is it the same as passing a NULL pointer to the user-space free which described here?

Upvotes: 1

Views: 2412

Answers (2)

mt42
mt42

Reputation: 119

This is however suboptimal and semantically wrong.

There is no logic or sense in passing a NULL pointer to kfree().

By silently ignoring attempts to pass NULL pointer to kfree(), this inherently masks some memory allocation logical errors and bugs when they occur.

Calling kfree() for the NULL pointer twice may mean that the control logic tries to deallocate an object from the heap twice, having it set to NULL the first time. This usually means a hard-to-find bug in the kernel.

Upvotes: 0

user14189198
user14189198

Reputation: 173

From the documentation of void kfree (const void *objp);

If objp is NULL, no operation is performed.

Upvotes: 2

Related Questions