aboutsummaryrefslogtreecommitdiffstats
path: root/src/note.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2014-07-21 22:51:54 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2014-07-22 11:47:14 +0200
commit21fc7a4b7422f8b441a6266a11cc8e337aae190d (patch)
treeea32977883135de411f75f82eb5089792fc3ac2d /src/note.c
parent6203966fbf1f8886ff59cc9d6350b034ee5374c9 (diff)
downloadcalcurse-21fc7a4b7422f8b441a6266a11cc8e337aae190d.tar.gz
calcurse-21fc7a4b7422f8b441a6266a11cc8e337aae190d.zip
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 <calcurse@cryptocrack.de>
Diffstat (limited to 'src/note.c')
-rw-r--r--src/note.c24
1 files changed, 15 insertions, 9 deletions
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(&notepath, "%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(&notepath, "%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(&notepath, "%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(&notepath, "%s%s", path_notes, hp->hash);
unlink(notepath);
+ mem_free(notepath);
}
}