power: Add return value check and file descriptor handling code 76/317576/2 accepted/tizen_9.0_unified accepted/tizen_unified accepted/tizen_unified_dev accepted/tizen_unified_toolchain accepted/tizen_unified_x accepted/tizen_unified_x_asan tizen tizen_9.0 accepted/tizen/9.0/unified/20241030.234707 accepted/tizen/unified/20240913.101427 accepted/tizen/unified/dev/20240919.040413 accepted/tizen/unified/toolchain/20241004.101827 accepted/tizen/unified/x/20240919.040607 accepted/tizen/unified/x/asan/20241014.000203 tizen_9.0_m2_release
authorYunhee Seo <yuni.seo@samsung.com>
Thu, 12 Sep 2024 02:14:17 +0000 (11:14 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Thu, 12 Sep 2024 08:33:48 +0000 (17:33 +0900)
To avoid abnormal behavior and segmentation fault issue,
handling code is added.
 - Memory allocation fail case
 - File descriptor close handling
 - Uninitialized pointer variable handling

Change-Id: Id2ed9e369d8aefe3254b98d4e8953f7c6d741f0e
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
src/power/power.c

index d5f62dd273e0793afc64a69ea282b759dcb82333..05fbd62e8ac305407af544b8284c457ed8506cf5 100644 (file)
@@ -44,7 +44,7 @@ static struct wakeup_source source[MAX_NUM_WAKEUP_SOURCES];
 
 static int read_wakeup_sources(char ***wakeup_source_name, int *wakeup_source_number)
 {
-       char **new_wakeup_source_name;
+       char **new_wakeup_source_name = NULL;
        char buf[MAX_NAME_LENGTH + 1] = {0, };
        struct wakeup_source input = {0, };
        int source_index = 0; /* for iteration of all wakeup sources */
@@ -63,23 +63,24 @@ static int read_wakeup_sources(char ***wakeup_source_name, int *wakeup_source_nu
 
        for (int i = 0; i < NUM_INFORMATION; ++i) {
                ret = fscanf(fp, "%50s", buf);
-               if (ret < 1)
+               if (ret < 1) {
+                       ret = -EINVAL;
                        goto parse_err;
+               }
        }
 
        new_wakeup_source_name = calloc(MAX_NUM_WAKEUP_SOURCES, sizeof(char*));
        if (!new_wakeup_source_name) {
-               _E("Failed to allocate memory");
-               return -errno;
+               _E("Failed to allocate memory for new_wakeup_source_name");
+               ret = -ENOMEM;
+               goto parse_err;
        }
 
        while (!feof(fp)) {
                if (source_index >= MAX_NUM_WAKEUP_SOURCES) {
                        _E("Exceed max wakeup source number");
-                       for (int i = 0; i < changed_source_number; ++i)
-                               free(new_wakeup_source_name[i]);
-                       free(new_wakeup_source_name);
-                       return -EPERM;
+                       ret = -EPERM;
+                       goto parse_err;
                }
 
                ret = fscanf(fp, "%50s %d %d %d %d %d %d %d %d %d\n", input.name,
@@ -89,13 +90,20 @@ static int read_wakeup_sources(char ***wakeup_source_name, int *wakeup_source_nu
                        &input.max_time, &input.last_change,
                        &input.prevent_suspend_time);
 
-               if (ret < 10)
+               if (ret < 10) {
+                       ret = -EINVAL;
                        goto parse_err;
+               }
 
                /* check whether the wakeup count increases */
                if (source[source_index].wakeup_count < input.wakeup_count) {
                        new_wakeup_source_name[changed_source_number] =
                                calloc(MAX_NAME_LENGTH + 1, sizeof(char));
+                       if (!new_wakeup_source_name[changed_source_number]) {
+                               _E("Failed to allocate memory for new_wakeup_source_name");
+                               ret = -ENOMEM;
+                               goto parse_err;
+                       }
                        strncpy(new_wakeup_source_name[changed_source_number++],
                                input.name, MAX_NAME_LENGTH + 1);
                        _D("%s wakeup source detected", input.name);
@@ -111,11 +119,14 @@ static int read_wakeup_sources(char ***wakeup_source_name, int *wakeup_source_nu
 
 parse_err:
        _E("Failed to parse %s", WAKEUP_SOURCES_PATH);
-       for (int i = 0; i < changed_source_number; ++i)
-               free(new_wakeup_source_name[i]);
-       free(new_wakeup_source_name);
+       if (new_wakeup_source_name!= NULL) {
+               for (int i = 0; i < changed_source_number; ++i) {
+                       free(new_wakeup_source_name[i]);
+               }
+               free(new_wakeup_source_name);
+       }
        fclose(fp);
-       return -EINVAL;
+       return ret;
 }
 
 EXPORT int hal_backend_device_common_read_wakeup_sources(char ***wakeup_source_name, int *wakeup_source_number)