Prechádzať zdrojové kódy

Add error handling for workqueue allocation

Previously, sched_init() did not check whether alloc_workqueue() succeeded.
This could lead to a NULL pointer dereference and kernel crash if memory
allocation failed. This commit adds a check for a NULL return and logs an
error message if allocation fails.

By returning -ENOMEM early, we ensure the module will not be loaded in an
invalid state and avoid undefined behavior or system instability.
Guan-Chun.Wu 5 dní pred
rodič
commit
2588783dcb
1 zmenil súbory, kde vykonal 4 pridanie a 0 odobranie
  1. 4 0
      examples/sched.c

+ 4 - 0
examples/sched.c

@@ -16,6 +16,10 @@ static void work_handler(struct work_struct *data)
 static int __init sched_init(void)
 {
     queue = alloc_workqueue("HELLOWORLD", WQ_UNBOUND, 1);
+    if (!queue) {
+        pr_err("Failed to allocate workqueue\n");
+        return -ENOMEM;
+    }
     INIT_WORK(&work, work_handler);
     queue_work(queue, &work);
     return 0;