From 66ce00153bd35bb83a296f7eb31efec6d6e6768b Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 21 Jul 2014 22:56:37 +0200 Subject: Refactor new_tempfile() Avoid preallocating buffers on the stack, use dynamic memory allocation instead. Also, change the semantics of new_tempfile() so that it returns the full name of the temporary file and fix all call sites. Signed-off-by: Lukas Fleischer --- src/config.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/config.c') diff --git a/src/config.c b/src/config.c index 5ce7001..6cc3e6e 100644 --- a/src/config.c +++ b/src/config.c @@ -590,25 +590,21 @@ static int config_save_junk_cb(const char *data, void *status) /* Save the user configuration. */ unsigned config_save(void) { - char tmppath[BUFSIZ]; - char *tmpext; + char *tmpprefix = NULL, *tmppath = NULL; struct config_save_status status; int i; + int ret = 0; if (read_only) return 1; - strncpy(tmppath, get_tempdir(), BUFSIZ); - tmppath[BUFSIZ - 1] = '\0'; - strncat(tmppath, "/" CONF_PATH_NAME ".", BUFSIZ - strlen(tmppath) - 1); - if ((tmpext = new_tempfile(tmppath, TMPEXTSIZ)) == NULL) - return 0; - strncat(tmppath, tmpext, BUFSIZ - strlen(tmppath) - 1); - mem_free(tmpext); + asprintf(&tmpprefix, "%s/%s", get_tempdir(), CONF_PATH_NAME); + if ((tmppath = new_tempfile(tmpprefix)) == NULL) + goto cleanup; status.fp = fopen(tmppath, "w"); if (!status.fp) - return 0; + goto cleanup; memset(status.done, 0, sizeof(status.done)); @@ -626,5 +622,9 @@ unsigned config_save(void) if (io_file_cp(tmppath, path_conf)) unlink(tmppath); - return 1; + ret = 1; +cleanup: + mem_free(tmpprefix); + mem_free(tmppath); + return ret; } -- cgit v1.2.3-54-g00ecf