diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2014-07-21 22:51:54 +0200 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2014-07-22 11:47:14 +0200 |
commit | 21fc7a4b7422f8b441a6266a11cc8e337aae190d (patch) | |
tree | ea32977883135de411f75f82eb5089792fc3ac2d | |
parent | 6203966fbf1f8886ff59cc9d6350b034ee5374c9 (diff) | |
download | 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>
-rw-r--r-- | src/calcurse.c | 43 | ||||
-rw-r--r-- | src/config.c | 3 | ||||
-rw-r--r-- | src/custom.c | 5 | ||||
-rw-r--r-- | src/help.c | 47 | ||||
-rw-r--r-- | src/io.c | 71 | ||||
-rw-r--r-- | src/note.c | 24 | ||||
-rw-r--r-- | src/ui-calendar.c | 6 | ||||
-rw-r--r-- | src/ui-day.c | 67 | ||||
-rw-r--r-- | src/utils.c | 19 |
9 files changed, 150 insertions, 135 deletions
diff --git a/src/calcurse.c b/src/calcurse.c index 81f668d..2e015b4 100644 --- a/src/calcurse.c +++ b/src/calcurse.c @@ -256,6 +256,8 @@ static inline void key_generic_save(void) static inline void key_generic_reload(void) { + char *msg_um_asktype = NULL; + if (io_get_modified()) { const char *msg_um_prefix = _("There are unsaved modifications:"); @@ -264,10 +266,8 @@ static inline void key_generic_reload(void) const char *msg_um_keep = _("(k)eep and cancel"); const char *msg_um_choice = _("[dmk]"); - char msg_um_asktype[BUFSIZ]; - snprintf(msg_um_asktype, BUFSIZ, "%s %s, %s, %s", - msg_um_prefix, msg_um_discard, msg_um_merge, - msg_um_keep); + asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix, + msg_um_discard, msg_um_merge, msg_um_keep); char *path_apts_backup, *path_todo_backup; const char *backup_ext = ".sav"; @@ -276,14 +276,10 @@ static inline void key_generic_reload(void) case 1: break; case 2: - path_apts_backup = mem_malloc(strlen(path_apts) + - strlen(backup_ext) + 1); - path_todo_backup = mem_malloc(strlen(path_todo) + - strlen(backup_ext) + 1); - sprintf(path_apts_backup, "%s%s", path_apts, - backup_ext); - sprintf(path_todo_backup, "%s%s", path_todo, - backup_ext); + asprintf(&path_apts_backup, "%s%s", path_apts, + backup_ext); + asprintf(&path_todo_backup, "%s%s", path_todo, + backup_ext); io_save_mutex_lock(); io_save_apts(path_apts_backup); @@ -313,7 +309,7 @@ static inline void key_generic_reload(void) /* FALLTHROUGH */ default: wins_update(FLAG_STA); - return; + goto cleanup; } } @@ -346,6 +342,9 @@ static inline void key_generic_reload(void) notify_check_next_app(1); ui_calendar_monthly_view_cache_set_invalid(); wins_update(FLAG_ALL); + +cleanup: + mem_free(msg_um_asktype); } static inline void key_generic_import(void) @@ -533,6 +532,7 @@ static inline void key_generic_cmd(void) char cmd[BUFSIZ] = ""; char *cmd_name; int valid = 0, force = 0; + char *error_msg; status_mesg(_("Command:"), ""); if (getstring(win[STA].p, cmd, BUFSIZ, 0, 1) != GETSTRING_VALID) @@ -563,26 +563,19 @@ static inline void key_generic_cmd(void) char *topic = strtok(NULL, " "); if (!display_help(topic)) { - char error_msg[BUFSIZ]; - - snprintf(error_msg, BUFSIZ, - _("Help topic does not exist: %s"), - topic); - error_msg[BUFSIZ - 1] = '\0'; - + asprintf(&error_msg, + _("Help topic does not exist: %s"), topic); warnbox(error_msg); + mem_free(error_msg); } valid = 1; } if (!valid) { - char error_msg[BUFSIZ]; - - snprintf(error_msg, BUFSIZ, _("No such command: %s"), cmd); - error_msg[BUFSIZ - 1] = '\0'; - + asprintf(&error_msg, _("No such command: %s"), cmd); warnbox(error_msg); + mem_free(error_msg); } cleanup: diff --git a/src/config.c b/src/config.c index 5eb526a..5ce7001 100644 --- a/src/config.c +++ b/src/config.c @@ -449,8 +449,7 @@ config_serialize_conf(char *buf, const char *key, for (i = 0; i < ARRAY_SIZE(confmap); i++) { if (!strcmp(confmap[i].key, key)) { - if (confmap[i]. - fn_serialize(buf, confmap[i].target)) { + if (confmap[i].fn_serialize(buf, confmap[i].target)) { if (status) status->done[i] = 1; return 1; diff --git a/src/custom.c b/src/custom.c index b3d5da5..cccf7b1 100644 --- a/src/custom.c +++ b/src/custom.c @@ -803,14 +803,15 @@ print_keys_bindings(WINDOW * win, int selected_row, int selected_elm, noelm = y = 0; for (action = 0; action < NBKEYS; action++) { - char actionstr[BUFSIZ]; + char *actionstr; int nbkeys; nbkeys = keys_action_count_keys(action); - snprintf(actionstr, BUFSIZ, "%s", keys_get_label(action)); + asprintf(&actionstr, "%s", keys_get_label(action)); if (action == selected_row) custom_apply_attr(win, ATTR_HIGHEST); mvwprintw(win, y, XPOS, "%s ", actionstr); + mem_free(actionstr); mvwaddstr(win, y, EQUALPOS, "="); if (nbkeys == 0) mvwaddstr(win, y, KEYPOS, _("undefined")); @@ -36,7 +36,7 @@ #include "calcurse.h" -static int find_basedir(const char *locale_info[], unsigned n, char *basedir) +static int find_basedir(const char *locale_info[], unsigned n, char **basedir) { int i; char *locale = NULL; @@ -47,29 +47,33 @@ static int find_basedir(const char *locale_info[], unsigned n, char *basedir) continue; locale = strdup(locale_info[i]); - snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); - if (io_dir_exists(basedir)) { + asprintf(basedir, "%s/%s", DOCDIR, locale); + if (io_dir_exists(*basedir)) { ret = 1; goto cleanup; } strtok(locale, ".@"); - snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); - if (io_dir_exists(basedir)) { + mem_free(*basedir); + asprintf(basedir, "%s/%s", DOCDIR, locale); + if (io_dir_exists(*basedir)) { ret = 1; goto cleanup; } strtok(locale, "_"); - snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); - if (io_dir_exists(basedir)) { + mem_free(*basedir); + asprintf(basedir, "%s/%s", DOCDIR, locale); + if (io_dir_exists(*basedir)) { ret = 1; goto cleanup; } - free(locale); + mem_free(*basedir); + basedir = NULL; + mem_free(locale); locale = NULL; } @@ -87,23 +91,27 @@ int display_help(const char *topic) getenv("LC_MESSAGE"), getenv("LANG") }; - char basedir[BUFSIZ]; - char path[BUFSIZ]; + char *basedir; + char *path; + int ret = 0; if (!topic) topic = "intro"; - if (!find_basedir(locale_info, ARRAY_SIZE(locale_info), basedir)) - snprintf(basedir, BUFSIZ, DOCDIR); + if (!find_basedir(locale_info, ARRAY_SIZE(locale_info), &basedir)) { + mem_free(basedir); + asprintf(&basedir, "%s", DOCDIR); + } - snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); + asprintf(&path, "%s/%s.txt", basedir, topic); if (!io_file_exists(path) && keys_str2int(topic) > 0 && keys_get_action(keys_str2int(topic)) > 0) { int ch = keys_str2int(topic); enum key action = keys_get_action(ch); topic = keys_get_label(action); - snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); + mem_free(path); + asprintf(&path, "%s/%s.txt", basedir, topic); } if (!io_file_exists(path)) { @@ -185,14 +193,17 @@ int display_help(const char *topic) topic = "priority"; else if (!strcmp(topic, "lower-priority")) topic = "priority"; - snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); + mem_free(path); + asprintf(&path, "%s/%s.txt", basedir, topic); } if (io_file_exists(path)) { const char *arg[] = { conf.pager, path, NULL }; wins_launch_external(arg); - return 1; - } else { - return 0; + ret = 1; } + + mem_free(basedir); + mem_free(path); + return ret; } @@ -166,13 +166,11 @@ static FILE *get_export_stream(enum export_type type) const char *file_ext[IO_EXPORT_NBTYPES] = { "ical", "txt" }; stream = NULL; - stream_name = (char *)mem_malloc(BUFSIZ); if ((home = getenv("HOME")) != NULL) - snprintf(stream_name, BUFSIZ, "%s/calcurse.%s", home, - file_ext[type]); + asprintf(&stream_name, "%s/calcurse.%s", home, file_ext[type]); else - snprintf(stream_name, BUFSIZ, "%s/calcurse.%s", - get_tempdir(), file_ext[type]); + asprintf(&stream_name, "%s/calcurse.%s", get_tempdir(), + file_ext[type]); while (stream == NULL) { status_mesg(question, ""); @@ -186,8 +184,8 @@ static FILE *get_export_stream(enum export_type type) wgetch(win[KEY].p); } } - mem_free(stream_name); + mem_free(stream_name); return stream; } @@ -196,7 +194,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...) { FILE *fp; va_list ap; - char buf[BUFSIZ]; + char *buf; int ret; fp = fopen(fname, "a"); @@ -204,7 +202,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...) strerror(errno)); va_start(ap, fmt); - ret = vsnprintf(buf, sizeof buf, fmt, ap); + ret = vasprintf(&buf, fmt, ap); RETVAL_IF(ret < 0, 0, _("Failed to build message\n")); va_end(ap); @@ -215,6 +213,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...) RETVAL_IF(ret != 0, 0, _("Failed to close \"%s\" - %s\n"), fname, strerror(errno)); + mem_free(buf); return 1; } @@ -809,14 +808,14 @@ void io_load_keys(const char *pager) int ch; if ((ch = keys_str2int(key_ch)) < 0) { - char unknown_key[BUFSIZ]; + char *unknown_key; skipped++; - (void)snprintf(unknown_key, BUFSIZ, - _("Error reading key: \"%s\""), - key_ch); - io_log_print(log, line, - unknown_key); + asprintf(&unknown_key, + _("Error reading key: \"%s\""), + key_ch); + io_log_print(log, line, unknown_key); + mem_free(unknown_key); } else { int used; @@ -825,18 +824,15 @@ void io_load_keys(const char *pager) ht_elm-> key); if (used) { - char already_assigned - [BUFSIZ]; + char *already_assigned; skipped++; - (void) - snprintf - (already_assigned, - BUFSIZ, - _("\"%s\" assigned multiple times!"), - key_ch); + asprintf(&already_assigned, + _("\"%s\" assigned multiple times!"), + key_ch); io_log_print(log, line, already_assigned); + mem_free(already_assigned); } else { assigned++; } @@ -1048,7 +1044,7 @@ void io_import_data(enum import_type type, const char *stream_name) { const char *proc_report = _("Import process report: %04d lines read "); - char stats_str[4][BUFSIZ]; + char *stats_str[4]; FILE *stream = NULL; struct io_file *log; struct { @@ -1092,25 +1088,25 @@ void io_import_data(enum import_type type, const char *stream_name) if (stream != stdin) file_close(stream, __FILE_POS__); - snprintf(stats_str[0], BUFSIZ, - ngettext("%d app", "%d apps", stats.apoints), + asprintf(&stats_str[0], ngettext("%d app", "%d apps", stats.apoints), stats.apoints); - snprintf(stats_str[1], BUFSIZ, + asprintf(&stats_str[1], ngettext("%d event", "%d events", stats.events), stats.events); - snprintf(stats_str[2], BUFSIZ, - ngettext("%d todo", "%d todos", stats.todos), + asprintf(&stats_str[2], ngettext("%d todo", "%d todos", stats.todos), stats.todos); - snprintf(stats_str[3], BUFSIZ, _("%d skipped"), stats.skipped); + asprintf(&stats_str[3], _("%d skipped"), stats.skipped); if (ui_mode == UI_CURSES && conf.system_dialogs) { - char read[BUFSIZ], stat[BUFSIZ]; + char *read, *stat; - snprintf(read, BUFSIZ, proc_report, stats.lines); - snprintf(stat, BUFSIZ, "%s / %s / %s / %s (%s)", + asprintf(&read, proc_report, stats.lines); + asprintf(&stat, "%s / %s / %s / %s (%s)", stats_str[0], stats_str[1], stats_str[2], stats_str[3], _("Press [ENTER] to continue")); status_mesg(read, stat); + mem_free(read); + mem_free(stat); wgetch(win[KEY].p); } else if (ui_mode == UI_CMDLINE) { printf(proc_report, stats.lines); @@ -1128,16 +1124,20 @@ void io_import_data(enum import_type type, const char *stream_name) io_log_display(log, view_log, conf.pager); } + + mem_free(stats_str[0]); + mem_free(stats_str[1]); + mem_free(stats_str[2]); + mem_free(stats_str[3]); io_log_free(log); } struct io_file *io_log_init(void) { - char logprefix[BUFSIZ]; - char *logname; + char *logprefix, *logname; struct io_file *log; - snprintf(logprefix, BUFSIZ, "%s/calcurse_log.", get_tempdir()); + 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...")); @@ -1145,6 +1145,7 @@ struct io_file *io_log_init(void) 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); log->fd = fopen(log->name, "w"); if (log->fd == NULL) { @@ -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); } } diff --git a/src/ui-calendar.c b/src/ui-calendar.c index b24d555..bd8d7fe 100644 --- a/src/ui-calendar.c +++ b/src/ui-calendar.c @@ -629,7 +629,7 @@ void ui_calendar_change_day(int datefmt) { #define LDAY 11 char selected_day[LDAY] = ""; - char outstr[BUFSIZ]; + char *outstr; int dday, dmonth, dyear; int wrong_day = 1; const char *mesg_line1 = @@ -640,9 +640,9 @@ void ui_calendar_change_day(int datefmt) _("Enter the day to go to [ENTER for today] : %s"); while (wrong_day) { - snprintf(outstr, BUFSIZ, request_date, - DATEFMT_DESC(datefmt)); + asprintf(&outstr, request_date, DATEFMT_DESC(datefmt)); status_mesg(outstr, ""); + mem_free(outstr); if (getstring(win[STA].p, selected_day, LDAY, 0, 1) == GETSTRING_ESC) { return; diff --git a/src/ui-day.c b/src/ui-day.c index 269d818..39fa53c 100644 --- a/src/ui-day.c +++ b/src/ui-day.c @@ -156,8 +156,7 @@ static void update_rept(struct rpt **rpt, const long start) { int newtype, newfreq; long newuntil; - char outstr[BUFSIZ]; - char *freqstr, *timstr; + char *freqstr = NULL, *timstr, *outstr = NULL; const char *msg_rpt_prefix = _("Enter the new repetition type:"); const char *msg_rpt_daily = _("(d)aily"); const char *msg_rpt_weekly = _("(w)eekly"); @@ -166,7 +165,7 @@ static void update_rept(struct rpt **rpt, const long start) /* Find the current repetition type. */ const char *rpt_current; - char msg_rpt_current[BUFSIZ]; + char *msg_rpt_current; switch (recur_def2char((*rpt)->type)) { case 'D': rpt_current = msg_rpt_daily; @@ -185,15 +184,12 @@ static void update_rept(struct rpt **rpt, const long start) rpt_current = msg_rpt_daily; } - snprintf(msg_rpt_current, BUFSIZ, _("(currently using %s)"), - rpt_current); + asprintf(&msg_rpt_current, _("(currently using %s)"), rpt_current); - char msg_rpt_asktype[BUFSIZ]; - snprintf(msg_rpt_asktype, BUFSIZ, "%s %s, %s, %s, %s ? %s", - msg_rpt_prefix, - msg_rpt_daily, - msg_rpt_weekly, msg_rpt_monthly, msg_rpt_yearly, - msg_rpt_current); + char *msg_rpt_asktype; + asprintf(&msg_rpt_asktype, "%s %s, %s, %s, %s ? %s", msg_rpt_prefix, + msg_rpt_daily, msg_rpt_weekly, msg_rpt_monthly, + msg_rpt_yearly, msg_rpt_current); const char *msg_rpt_choice = _("[dwmy]"); const char *msg_wrong_freq = @@ -219,17 +215,16 @@ static void update_rept(struct rpt **rpt, const long start) newtype = 'Y'; break; default: - return; + goto cleanup; } do { status_mesg(_("Enter the new repetition frequence:"), ""); - freqstr = mem_malloc(BUFSIZ); - snprintf(freqstr, BUFSIZ, "%d", (*rpt)->freq); + asprintf(&freqstr, "%d", (*rpt)->freq); if (updatestring(win[STA].p, &freqstr, 0, 1) != GETSTRING_VALID) { mem_free(freqstr); - return; + goto cleanup; } newfreq = atoi(freqstr); mem_free(freqstr); @@ -246,8 +241,7 @@ static void update_rept(struct rpt **rpt, const long start) struct date new_date; int newmonth, newday, newyear; - snprintf(outstr, BUFSIZ, - _("Enter the new ending date: [%s] or '0'"), + asprintf(&outstr, _("Enter the new ending date: [%s] or '0'"), DATEFMT_DESC(conf.input_datefmt)); status_mesg(outstr, ""); timstr = @@ -255,8 +249,7 @@ static void update_rept(struct rpt **rpt, const long start) DATEFMT(conf.input_datefmt)); if (updatestring(win[STA].p, &timstr, 0, 1) != GETSTRING_VALID) { - mem_free(timstr); - return; + goto cleanup; } if (strcmp(timstr, "0") == 0) { newuntil = 0; @@ -265,7 +258,7 @@ static void update_rept(struct rpt **rpt, const long start) if (!parse_date (timstr, conf.input_datefmt, &newyear, &newmonth, &newday, ui_calendar_get_slctd_day())) { - snprintf(outstr, BUFSIZ, msg_fmts, + asprintf(&outstr, msg_fmts, DATEFMT_DESC(conf.input_datefmt)); status_mesg(msg_wrong_date, outstr); wgetch(win[KEY].p); @@ -283,10 +276,16 @@ static void update_rept(struct rpt **rpt, const long start) wgetch(win[KEY].p); } - mem_free(timstr); (*rpt)->type = recur_char2def(newtype); (*rpt)->freq = newfreq; (*rpt)->until = newuntil; + +cleanup: + mem_free(msg_rpt_current); + mem_free(msg_rpt_asktype); + mem_free(freqstr); + mem_free(outstr); + mem_free(timstr); } /* Edit an already existing item. */ @@ -650,7 +649,7 @@ void ui_day_item_repeat(void) time_t t; int year = 0, month = 0, day = 0; struct date until_date; - char outstr[BUFSIZ]; + char *outstr = NULL; char user_input[BUFSIZ] = ""; const char *msg_rpt_prefix = _("Enter the repetition type:"); const char *msg_rpt_daily = _("(d)aily"); @@ -672,9 +671,8 @@ void ui_day_item_repeat(void) const char *mesg_older = _("Sorry, the date you entered is older than the item start time."); - char msg_asktype[BUFSIZ]; - snprintf(msg_asktype, BUFSIZ, "%s %s, %s, %s, %s", - msg_rpt_prefix, + char *msg_asktype; + asprintf(&msg_asktype, "%s %s, %s, %s, %s", msg_rpt_prefix, msg_rpt_daily, msg_rpt_weekly, msg_rpt_monthly, msg_rpt_yearly); @@ -685,14 +683,14 @@ void ui_day_item_repeat(void) long until, date; if (day_item_count(0) <= 0) - return; + goto cleanup; item_nb = listbox_get_sel(&lb_apt); p = day_get_item(item_nb); if (p->type != APPT && p->type != EVNT) { status_mesg(wrong_type_1, wrong_type_2); wgetch(win[KEY].p); - return; + goto cleanup; } switch (status_ask_choice(msg_asktype, msg_type_choice, 4)) { @@ -709,14 +707,14 @@ void ui_day_item_repeat(void) type = RECUR_YEARLY; break; default: - return; + goto cleanup; } while (freq == 0) { status_mesg(mesg_freq_1, ""); if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) != GETSTRING_VALID) - return; + goto cleanup; freq = atoi(user_input); if (freq == 0) { status_mesg(mesg_wrong_freq, wrong_type_2); @@ -726,12 +724,12 @@ void ui_day_item_repeat(void) } for (;;) { - snprintf(outstr, BUFSIZ, mesg_until_1, + asprintf(&outstr, mesg_until_1, DATEFMT_DESC(conf.input_datefmt)); status_mesg(outstr, ""); if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) != GETSTRING_VALID) - return; + goto cleanup; if (strlen(user_input) == 1 && strcmp(user_input, "0") == 0) { until = 0; @@ -752,7 +750,8 @@ void ui_day_item_repeat(void) status_mesg(mesg_older, wrong_type_2); wgetch(win[KEY].p); } else { - snprintf(outstr, BUFSIZ, mesg_wrong_2, + mem_free(outstr); + asprintf(&outstr, mesg_wrong_2, DATEFMT_DESC(conf.input_datefmt)); status_mesg(mesg_wrong_1, outstr); wgetch(win[KEY].p); @@ -783,6 +782,10 @@ void ui_day_item_repeat(void) io_set_modified(); ui_calendar_monthly_view_cache_set_invalid(); + +cleanup: + mem_free(msg_asktype); + mem_free(outstr); } /* Free the current cut item, if any. */ diff --git a/src/utils.c b/src/utils.c index c675518..34bdd5e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -197,7 +197,7 @@ int status_ask_choice(const char *message, const char choice[], int nb_choice) { int i, ch; - char tmp[BUFSIZ]; + char *tmp; /* "[4/2/f/t/w/.../Z] " */ char avail_choice[2 * nb_choice + 3]; @@ -205,9 +205,9 @@ int status_ask_choice(const char *message, const char choice[], avail_choice[1] = '\0'; for (i = 1; i <= nb_choice; i++) { - snprintf(tmp, BUFSIZ, (i == nb_choice) ? "%c] " : "%c/", - choice[i]); + asprintf(&tmp, (i == nb_choice) ? "%c] " : "%c/", choice[i]); strcat(avail_choice, tmp); + mem_free(tmp); } status_mesg(message, avail_choice); @@ -248,7 +248,7 @@ status_ask_simplechoice(const char *prefix, const char *choice[], int nb_choice) { int i; - char tmp[BUFSIZ]; + char *tmp; /* "(1) Choice1, (2) Choice2, (3) Choice3?" */ char choicestr[BUFSIZ]; /* Holds the characters to choose from ('1', '2', etc) */ @@ -261,10 +261,11 @@ status_ask_simplechoice(const char *prefix, const char *choice[], strcpy(choicestr, prefix); for (i = 0; i < nb_choice; i++) { - snprintf(tmp, BUFSIZ, + asprintf(&tmp, ((i + 1) == nb_choice) ? "(%d) %s?" : "(%d) %s, ", (i + 1), choice[i]); strcat(choicestr, tmp); + mem_free(tmp); } return (status_ask_choice(choicestr, char_choice, nb_choice)); @@ -984,8 +985,7 @@ shell_exec(int *pfdin, int *pfdout, const char *path, narg[1] = "-c"; if (argc > 1) { - arg0 = mem_malloc(strlen(path) + 6); - sprintf(arg0, "%s \"$@\"", path); + asprintf(&arg0, "%s \"$@\"", path); narg[2] = arg0; for (i = 0; i < argc; i++) @@ -1049,7 +1049,7 @@ void press_any_key(void) */ static void print_notefile(FILE * out, const char *filename, int nbtab) { - char path_to_notefile[BUFSIZ]; + char *path_to_notefile; FILE *notefile; char linestarter[BUFSIZ]; char buffer[BUFSIZ]; @@ -1064,8 +1064,9 @@ static void print_notefile(FILE * out, const char *filename, int nbtab) linestarter[0] = '\0'; } - snprintf(path_to_notefile, BUFSIZ, "%s/%s", path_notes, filename); + asprintf(&path_to_notefile, "%s/%s", path_notes, filename); notefile = fopen(path_to_notefile, "r"); + mem_free(path_to_notefile); if (notefile) { while (fgets(buffer, BUFSIZ, notefile) != 0) { if (printlinestarter) { |