diff options
-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) |