From 21fc7a4b7422f8b441a6266a11cc8e337aae190d Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 21 Jul 2014 22:51:54 +0200 Subject: Replace several uses of snprintf() by asprintf() Use asprintf() in some cold code paths. While allocating memory on the heap is a bit slower, using asprintf() is a bit more memory efficient and less prone to buffer overflow errors. Signed-off-by: Lukas Fleischer --- src/note.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src/note.c') diff --git a/src/note.c b/src/note.c index 882d1a6..ce627b9 100644 --- a/src/note.c +++ b/src/note.c @@ -59,17 +59,18 @@ HTABLE_PROTOTYPE(htp, note_gc_hash) char *generate_note(const char *str) { char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1); - char notepath[BUFSIZ]; + char *notepath; FILE *fp; sha1_digest(str, sha1); - snprintf(notepath, BUFSIZ, "%s%s", path_notes, sha1); + asprintf(¬epath, "%s%s", path_notes, sha1); fp = fopen(notepath, "w"); EXIT_IF(fp == NULL, _("Warning: could not open %s, Aborting..."), notepath); fputs(str, fp); file_close(fp, __FILE_POS__); + mem_free(notepath); return sha1; } @@ -78,7 +79,7 @@ void edit_note(char **note, const char *editor) { char tmppath[BUFSIZ]; char *tmpext; - char notepath[BUFSIZ]; + char *notepath = NULL; char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1); FILE *fp; @@ -91,7 +92,7 @@ void edit_note(char **note, const char *editor) mem_free(tmpext); if (*note != NULL) { - snprintf(notepath, BUFSIZ, "%s%s", path_notes, *note); + asprintf(¬epath, "%s%s", path_notes, *note); io_file_cp(notepath, tmppath); } @@ -105,8 +106,10 @@ void edit_note(char **note, const char *editor) fclose(fp); *note = sha1; - snprintf(notepath, BUFSIZ, "%s%s", path_notes, *note); + mem_free(notepath); + asprintf(¬epath, "%s%s", path_notes, *note); io_file_cp(tmppath, notepath); + mem_free(notepath); } unlink(tmppath); @@ -115,14 +118,16 @@ void edit_note(char **note, const char *editor) /* View a note in an external pager. */ void view_note(const char *note, const char *pager) { - char fullname[BUFSIZ]; + char *fullname; if (note == NULL) return; - snprintf(fullname, BUFSIZ, "%s%s", path_notes, note); + asprintf(&fullname, "%s%s", path_notes, note); const char *arg[] = { pager, fullname, NULL }; wins_launch_external(arg); + + mem_free(fullname); } /* Erase a note previously attached to an item. */ @@ -172,7 +177,7 @@ void note_gc(void) struct dirent *dp; llist_item_t *i; struct note_gc_hash tmph; - char notepath[BUFSIZ]; + char *notepath; if (!(dirp = opendir(path_notes))) return; @@ -235,7 +240,8 @@ void note_gc(void) /* Unlink unused note files. */ HTABLE_FOREACH(hp, htp, &gc_htable) { - snprintf(notepath, BUFSIZ, "%s%s", path_notes, hp->hash); + asprintf(¬epath, "%s%s", path_notes, hp->hash); unlink(notepath); + mem_free(notepath); } } -- cgit v1.2.3-54-g00ecf