diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2014-07-21 22:56:37 +0200 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2014-07-22 11:47:18 +0200 |
commit | 66ce00153bd35bb83a296f7eb31efec6d6e6768b (patch) | |
tree | f3afa343afd79ef7a854b21e15289c43d4537281 /src/note.c | |
parent | 21fc7a4b7422f8b441a6266a11cc8e337aae190d (diff) | |
download | 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/note.c')
-rw-r--r-- | src/note.c | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -77,19 +77,14 @@ char *generate_note(const char *str) /* Edit a note with an external editor. */ void edit_note(char **note, const char *editor) { - char tmppath[BUFSIZ]; - char *tmpext; + char *tmpprefix = NULL, *tmppath = NULL; char *notepath = NULL; char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1); FILE *fp; - strncpy(tmppath, get_tempdir(), BUFSIZ); - tmppath[BUFSIZ - 1] = '\0'; - strncat(tmppath, "/calcurse-note.", BUFSIZ - strlen(tmppath) - 1); - if ((tmpext = new_tempfile(tmppath, TMPEXTSIZ)) == NULL) - return; - strncat(tmppath, tmpext, BUFSIZ - strlen(tmppath) - 1); - mem_free(tmpext); + asprintf(&tmpprefix, "%s/calcurse-note", get_tempdir()); + if ((tmppath = new_tempfile(tmpprefix)) == NULL) + goto cleanup; if (*note != NULL) { asprintf(¬epath, "%s%s", path_notes, *note); @@ -113,6 +108,10 @@ void edit_note(char **note, const char *editor) } unlink(tmppath); + +cleanup: + mem_free(tmpprefix); + mem_free(tmppath); } /* View a note in an external pager. */ |