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/io.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 70f0ede..5bdba77 100644 --- a/src/io.c +++ b/src/io.c @@ -1135,25 +1135,32 @@ void io_import_data(enum import_type type, const char *stream_name) struct io_file *io_log_init(void) { char *logprefix, *logname; - struct io_file *log; + struct io_file *log = mem_malloc(sizeof(struct io_file)); - asprintf(&logprefix, "%s/calcurse_log.", get_tempdir()); - logname = new_tempfile(logprefix, TMPEXTSIZ); - RETVAL_IF(logname == NULL, 0, - _("Warning: could not create temporary log file, Aborting...")); - log = mem_malloc(sizeof(struct io_file)); - RETVAL_IF(log == NULL, 0, - _("Warning: could not open temporary log file, Aborting...")); - snprintf(log->name, sizeof(log->name), "%s%s", logprefix, logname); - mem_free(logprefix); - mem_free(logname); + if (!log) { + ERROR_MSG(_("Warning: could not open temporary log file, Aborting...")); + return NULL; + } + asprintf(&logprefix, "%s/calcurse_log", get_tempdir()); + logname = new_tempfile(logprefix); + if (!logname) { + ERROR_MSG(_("Warning: could not create temporary log file, Aborting...")); + goto error; + } + strncpy(log->name, logname, sizeof(log->name)); log->fd = fopen(log->name, "w"); if (log->fd == NULL) { ERROR_MSG(_("Warning: could not open temporary log file, Aborting...")); - mem_free(log); - return 0; + goto error; } + goto cleanup; +error: + mem_free(log); + log = NULL; +cleanup: + mem_free(logprefix); + mem_free(logname); return log; } -- cgit v1.2.3-54-g00ecf