aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2014-07-21 22:56:37 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2014-07-22 11:47:18 +0200
commit66ce00153bd35bb83a296f7eb31efec6d6e6768b (patch)
treef3afa343afd79ef7a854b21e15289c43d4537281 /src/config.c
parent21fc7a4b7422f8b441a6266a11cc8e337aae190d (diff)
downloadcalcurse-66ce00153bd35bb83a296f7eb31efec6d6e6768b.tar.gz
calcurse-66ce00153bd35bb83a296f7eb31efec6d6e6768b.zip
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 <calcurse@cryptocrack.de>
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c22
1 files changed, 11 insertions, 11 deletions
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;
}