From 4dff35c4c605f81c92b0d0f6c7e3be4d5a222463 Mon Sep 17 00:00:00 2001 From: Mateusz Moscicki Date: Wed, 5 Sep 2018 10:42:58 +0200 Subject: [PATCH] Use dynamic allocated memory instead of fixed-size arrays Change-Id: I03e91bf530204de6f03f1db79e2aa9bdd2608f95 --- src/crash-manager/crash-manager.c | 119 ++++++++++++++++++++++---------------- src/crash-stack/crash-stack.c | 9 ++- src/shared/util.c | 45 +++++++++----- src/shared/util.h | 4 +- 4 files changed, 105 insertions(+), 72 deletions(-) diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index 8add845..419e16a 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -120,20 +120,20 @@ struct crash_info { int uid_info; int gid_info; int sig_info; - char cmd_line[PATH_MAX]; - char cmd_path[PATH_MAX]; + char *cmd_line; + char *cmd_path; time_t time_info; - char temp_dir[PATH_MAX]; - char name[FILENAME_MAX]; - char result_path[PATH_MAX]; - char pfx[PATH_MAX]; - char info_path[PATH_MAX]; - char core_path[PATH_MAX]; - char log_path[PATH_MAX]; + char *temp_dir; + char *name; + char *result_path; + char *pfx; + char *info_path; + char *core_path; + char *log_path; char appid[APPID_MAX]; char pkgid[PKGNAME_MAX]; #ifdef SYS_ASSERT - char sysassert_cs_path[PATH_MAX]; + char *sysassert_cs_path; bool have_sysassert_report; #endif int prstatus_fd; @@ -432,8 +432,10 @@ static int make_dump_dir(void) static int get_cmd_info(struct crash_info *cinfo) { - return get_cmd_line(cinfo->pid_info, cinfo->cmd_line, sizeof(cinfo->cmd_path)) && - get_exe_path(cinfo->pid_info, cinfo->cmd_path, sizeof(cinfo->cmd_path)); + cinfo->cmd_line = get_cmd_line(cinfo->pid_info); + cinfo->cmd_path = get_exe_path(cinfo->pid_info); + + return (cinfo->cmd_line != NULL && cinfo->cmd_path != NULL); } static int set_prstatus(struct crash_info *cinfo) @@ -514,44 +516,40 @@ static int set_crash_info(struct crash_info *cinfo, int argc, char *argv[]) localtime_r(&cinfo->time_info, &loc_tm); strftime(date, sizeof(date), "%Y%m%d%H%M%S", &loc_tm); - ret = snprintf(cinfo->temp_dir, sizeof(cinfo->temp_dir), - "%s/crash.XXXXXX", crash_temp_path); - if (ret < 0) { - _E("Failed to snprintf for temp_dir"); + if (asprintf(&cinfo->temp_dir, "%s/crash.XXXXXX", crash_temp_path) == -1) { + _E("Failed to asprintf for temp_dir"); + cinfo->temp_dir = NULL; return -1; } + temp_dir_ret = mkdtemp(cinfo->temp_dir); if (!temp_dir_ret || access(temp_dir_ret, F_OK)) { _E("Failed to mkdtemp for temp_dir"); return -1; } - ret = snprintf(cinfo->name, sizeof(cinfo->name), "%s_%d_%s", - basename(cinfo->cmd_line), - cinfo->pid_info, - date); - if (ret < 0) { + if (asprintf(&cinfo->name, "%s_%d_%s", basename(cinfo->cmd_line), + cinfo->pid_info, date) == -1) { _E("Failed to snprintf for name"); + cinfo->name = NULL; goto rm_temp; } if (allow_zip) - ret = snprintf(cinfo->result_path, - sizeof(cinfo->result_path), + ret = asprintf(&cinfo->result_path, "%s/%s.zip", crash_crash_path, cinfo->name); else - ret = snprintf(cinfo->result_path, - sizeof(cinfo->result_path), + ret = asprintf(&cinfo->result_path, "%s/%s", crash_crash_path, cinfo->name); - if (ret < 0) { - _E("Failed to snprintf for result path"); + if (ret == -1) { + _E("Failed to asprintf for result path"); + cinfo->result_path = NULL; goto rm_temp; } - ret = snprintf(cinfo->pfx, sizeof(cinfo->pfx), "%s/%s", - cinfo->temp_dir, cinfo->name); - if (ret < 0) { - _E("Failed to snprintf for pfx"); + if (asprintf(&cinfo->pfx, "%s/%s", cinfo->temp_dir, cinfo->name) == -1) { + _E("Failed to asprintf for pfx"); + cinfo->pfx = NULL; goto rm_temp; } ret = mkdir(cinfo->pfx, 0775); @@ -560,34 +558,29 @@ static int set_crash_info(struct crash_info *cinfo, int argc, char *argv[]) goto rm_temp; } - ret = snprintf(cinfo->info_path, sizeof(cinfo->info_path), - "%s/%s.info", cinfo->pfx, cinfo->name); - if (ret < 0) { - _E("Failed to snprintf for info path"); + if (asprintf(&cinfo->info_path, "%s/%s.info", cinfo->pfx, cinfo->name) == -1) { + _E("Failed to asprintf for info path"); + cinfo->info_path = NULL; goto rm_temp; } - ret = snprintf(cinfo->core_path, sizeof(cinfo->core_path), - "%s/%s.coredump", cinfo->pfx, cinfo->name); - if (ret < 0) { - _E("Failed to snprintf for core path"); + if (asprintf(&cinfo->core_path, "%s/%s.coredump", cinfo->pfx, cinfo->name) == -1) { + _E("Failed to asprintf for core path"); + cinfo->core_path = NULL; goto rm_temp; } - ret = snprintf(cinfo->log_path, sizeof(cinfo->log_path), - "%s/%s.log", cinfo->pfx, cinfo->name); - if (ret < 0) { - _E("Failed to snprintf for log path"); + if (asprintf(&cinfo->log_path, "%s/%s.log", cinfo->pfx, cinfo->name) == -1) { + _E("Failed to asprintf for log path"); + cinfo->log_path = NULL; goto rm_temp; } #ifdef SYS_ASSERT - ret = snprintf(cinfo->sysassert_cs_path, - sizeof(cinfo->sysassert_cs_path), - "/tmp/crash_stack/%s_%d.info", - basename(cinfo->cmd_line), cinfo->pid_info); - if (ret < 0) { + if (asprintf(&cinfo->sysassert_cs_path, "/tmp/crash_stack/%s_%d.info", + basename(cinfo->cmd_line), cinfo->pid_info) == -1) { _E("Failed to snprintf for sys-assert callstack path"); + cinfo->sysassert_cs_path = NULL; goto rm_temp; } #endif @@ -1213,9 +1206,25 @@ static int wait_for_opt(unsigned int timeout) return 1; } +static void free_crash_info(struct crash_info *cinfo) +{ + free(cinfo->cmd_line); + free(cinfo->cmd_path); + free(cinfo->temp_dir); + free(cinfo->result_path); + free(cinfo->pfx); + free(cinfo->info_path); + free(cinfo->core_path); + free(cinfo->log_path); + +#ifdef SYS_ASSERT + free(cinfo->sysassert_cs_path); +#endif +} + int main(int argc, char *argv[]) { - struct crash_info cinfo; + struct crash_info cinfo = {0}; /* Execute dump_systemstate in parallel */ static int dump_state_pid; @@ -1289,8 +1298,14 @@ int main(int argc, char *argv[]) else move_dump_data(cinfo.pfx, &cinfo); } else { - snprintf(cinfo.result_path, sizeof(cinfo.result_path), "%s/%s.info", - crash_crash_path, cinfo.name); + free(cinfo.result_path); + if (asprintf(&cinfo.result_path, "%s/%s.info", + crash_crash_path, cinfo.name) == -1) { + cinfo.result_path = NULL; + _E("asprintf() error: %m"); + res = EXIT_FAILURE; + goto exit; + } move_dump_data(cinfo.info_path, &cinfo); } /* launch crash-popup only if the .debugmode file is exist*/ @@ -1316,5 +1331,7 @@ exit: free(crash_root_path); free(crash_crash_path); + free_crash_info(&cinfo); + return res; } diff --git a/src/crash-stack/crash-stack.c b/src/crash-stack/crash-stack.c index 35516eb..99f60b3 100644 --- a/src/crash-stack/crash-stack.c +++ b/src/crash-stack/crash-stack.c @@ -258,12 +258,15 @@ void callstack_destructor(Callstack *callstack) */ static void __crash_stack_print_exe(FILE* outputfile, pid_t pid) { - char file_path[PATH_MAX]; + char *file_path; - if (get_exe_path(pid, file_path, sizeof(file_path)) == 0) - snprintf(file_path, sizeof(file_path), ""); + file_path = get_exe_path(pid); + if (file_path == NULL) + file_path = strdup(""); fprintf(outputfile, "Executable File Path: %s\n", file_path); + + free(file_path); } /** diff --git a/src/shared/util.c b/src/shared/util.c index 97f3499..86e95e4 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -819,7 +819,7 @@ int find_crash_tid(int pid) return -1; } -int get_cmd_line(pid_t pid, char *cmd, size_t len) +char* get_cmd_line(pid_t pid) { char cmdline_path[PATH_MAX]; @@ -828,45 +828,58 @@ int get_cmd_line(pid_t pid, char *cmd, size_t len) int fd = open(cmdline_path, O_RDONLY); if (fd < 0) { - _E("Failed to open %s: %m", cmdline_path); - return 0; + _E("Failed to open %s: %m\n", cmdline_path); + return NULL; } - ssize_t ret = read(fd, cmd, len - 1); + char buffer[PATH_MAX]; + ssize_t ret = read(fd, buffer, sizeof(buffer) - 1); + buffer[ret] = '\0'; close(fd); + if (ret <= 0) { - _E("Failed to read %s: %m", cmdline_path); - return 0; + _E("Failed to read %s: %m\n", cmdline_path); + return NULL; } - cmd[ret] = '\0'; + char *result; + if (asprintf(&result, "%s", buffer) == -1) { + _E("asprintf() error: %m\n"); + return NULL; + } - return 1; + return result; } -int get_exe_path(pid_t pid, char *path, size_t len) +char* get_exe_path(pid_t pid) { char exe_link[PATH_MAX]; + char buffer[PATH_MAX]; snprintf(exe_link, sizeof(exe_link), "/proc/%d/exe", pid); - ssize_t ret = readlink(exe_link, path, len - 1); + ssize_t ret = readlink(exe_link, buffer, sizeof(buffer) - 1); + buffer[ret] = '\0'; if (ret <= 0) { _E("Failed to read link %s: %m", exe_link); - return 0; + return NULL; } - path[ret] = '\0'; + if (access(buffer, F_OK) == -1) { + _E("Invalid path %s", buffer); + return NULL; + } - if (access(path, F_OK) == -1) { - _E("Invalid path %s", path); - return 0; + char *result; + if (asprintf(&result, "%s", buffer) == -1) { + _E("asprintf() error: %m\n"); + return NULL; } - return 1; + return result; } /** * @} diff --git a/src/shared/util.h b/src/shared/util.h index 5d40d91..e0978ac 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -63,9 +63,9 @@ int log_kmsg(char *fmt, ...); int find_crash_tid(int pid); -int get_exe_path(pid_t pid, char *path, size_t len); +char* get_exe_path(pid_t pid); -int get_cmd_line(pid_t pid, char *cmd, size_t len); +char* get_cmd_line(pid_t pid); /** * @} */ -- 2.7.4