diff options
author | Yunhee Seo <yuni.seo@samsung.com> | 2024-09-12 11:14:17 +0900 |
---|---|---|
committer | Yunhee Seo <yuni.seo@samsung.com> | 2024-09-12 17:33:48 +0900 |
commit | ce46cf3f6b2112f6b6c5c168d90139a3cfd35d7d (patch) | |
tree | be5f05ead5ccac227a4a767521da423e18c50f00 /src | |
parent | 3894702b6baee32ef7bb96504b2b746a3842bd44 (diff) | |
download | device-common-accepted/tizen_9.0_unified.tar.gz device-common-accepted/tizen_9.0_unified.tar.bz2 device-common-accepted/tizen_9.0_unified.zip |
power: Add return value check and file descriptor handling codetizen_9.0_m2_releaseaccepted/tizen/unified/x/asan/20241014.000203accepted/tizen/unified/x/20240919.040607accepted/tizen/unified/toolchain/20241004.101827accepted/tizen/unified/dev/20240919.040413accepted/tizen/unified/20240913.101427accepted/tizen/9.0/unified/20241030.234707tizen_9.0tizenaccepted/tizen_unified_x_asanaccepted/tizen_unified_xaccepted/tizen_unified_toolchainaccepted/tizen_unified_devaccepted/tizen_unifiedaccepted/tizen_9.0_unified
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/power/power.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/power/power.c b/src/power/power.c index d5f62dd..05fbd62 100644 --- a/src/power/power.c +++ b/src/power/power.c @@ -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) |