pass: resmon: Fix potential NULL dereference value 02/191602/1
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 18 Oct 2018 05:54:46 +0000 (14:54 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 19 Oct 2018 02:58:39 +0000 (11:58 +0900)
Fix potential NULL dereference value by checking the returned value
from memory allocation functions.

Change-Id: I040f75a36f5ec904da45cd89633bba38e206f5ed
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-resmon-source.c

index e26651cc5280579b874b066aa6f25b0cbf13c233..ac469bf580d33a426f29713a4a45ca77b052c7b5 100644 (file)
@@ -151,19 +151,53 @@ static int resmon_cpuhp_init(struct resmon *monitor)
                return 0;
 
        result = calloc(RESMON_SRC_CPUHP_COUNT, sizeof(*result));
+       if (!result)
+               return -ENOMEM;
 
        for (i = 0; i < RESMON_SRC_CPUHP_COUNT; i++) {
                result[i].load = calloc(RESMON_SRC_CPUHP_COUNT,
                                        sizeof(unsigned int));
+               if (!result[i].load)
+                       goto err;
+
                result[i].nr_running = calloc(RESMON_SRC_CPUHP_COUNT,
                                        sizeof(unsigned int));
+               if (!result[i].nr_running)
+                       goto err;
+
                result[i].runnable_load = calloc(RESMON_SRC_CPUHP_COUNT,
                                        sizeof(unsigned int));
+               if (!result[i].runnable_load)
+                       goto err;
        }
 
        monitor->result = result;
 
        return 0;
+err:
+       for (i = 0; i < RESMON_SRC_CPUHP_COUNT; i++) {
+               if (result[i].load) {
+                       free(result[i].load);
+                       result[i].load = NULL;
+               }
+
+               if (result[i].nr_running) {
+                       free(result[i].nr_running);
+                       result[i].nr_running = NULL;
+               }
+
+               if (result[i].runnable_load) {
+                       free(result[i].runnable_load);
+                       result[i].runnable_load = NULL;
+               }
+       }
+
+       if (result) {
+               free(result);
+               result = NULL;
+       }
+
+       return -ENOMEM;
 }
 
 /**
@@ -185,13 +219,26 @@ static int resmon_cpuhp_exit(struct resmon *monitor)
                return -ENOMEM;
 
        for (i = 0; i < RESMON_SRC_CPUHP_COUNT; i++) {
-               free(result[i].load);
-               free(result[i].nr_running);
-               free(result[i].runnable_load);
+               if (result[i].load) {
+                       free(result[i].load);
+                       result[i].load = NULL;
+               }
+
+               if (result[i].nr_running) {
+                       free(result[i].nr_running);
+                       result[i].nr_running = NULL;
+               }
+
+               if (result[i].runnable_load) {
+                       free(result[i].runnable_load);
+                       result[i].runnable_load = NULL;
+               }
        }
 
-       free(monitor->result);
-       monitor->result = NULL;
+       if (monitor->result) {
+               free(monitor->result);
+               monitor->result = NULL;
+       }
 
        return 0;
 }