slab2_buggy.c: check for kzalloc() failure case

(cred to @ncshy on github)

Signed-off-by: Kaiwan N Billimoria <kaiwan.billimoria@gmail.com>
This commit is contained in:
Kaiwan N Billimoria
2025-06-02 08:18:45 +05:30
parent a2d2531ff9
commit c39925e2d2

View File

@@ -16,7 +16,7 @@
* case, resulting in a kernel BUG.
*
* For details, please refer the book, Ch 8.
* (This code was initially missing and added later to the book GitHub repo)
* (This code was initially missing and added later to the book GitHub repo.)
*/
#define pr_fmt(fmt) "%s:%s(): " fmt, KBUILD_MODNAME, __func__
#include <linux/init.h>
@@ -31,11 +31,13 @@ MODULE_VERSION("0.2");
static int __init slab2_buggy_init(void)
{
int num = 1024, x = 3;
static char *kptr; // static; is initialized to NULL
static char *kptr; // static var, thus initialized to NULL
while (x > 0) {
if (!kptr) {
kptr = kzalloc(num, GFP_KERNEL);
if (!kptr)
return -ENOMEM;
// [... work on the slab memory ...]
memset(kptr, x, num);
}