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>
This commit is contained in:
Lukas Fleischer 2014-07-21 22:51:54 +02:00
parent 6203966fbf
commit 21fc7a4b74
9 changed files with 150 additions and 135 deletions

View File

@ -256,6 +256,8 @@ static inline void key_generic_save(void)
static inline void key_generic_reload(void) static inline void key_generic_reload(void)
{ {
char *msg_um_asktype = NULL;
if (io_get_modified()) { if (io_get_modified()) {
const char *msg_um_prefix = const char *msg_um_prefix =
_("There are unsaved modifications:"); _("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_keep = _("(k)eep and cancel");
const char *msg_um_choice = _("[dmk]"); const char *msg_um_choice = _("[dmk]");
char msg_um_asktype[BUFSIZ]; asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix,
snprintf(msg_um_asktype, BUFSIZ, "%s %s, %s, %s", msg_um_discard, msg_um_merge, msg_um_keep);
msg_um_prefix, msg_um_discard, msg_um_merge,
msg_um_keep);
char *path_apts_backup, *path_todo_backup; char *path_apts_backup, *path_todo_backup;
const char *backup_ext = ".sav"; const char *backup_ext = ".sav";
@ -276,14 +276,10 @@ static inline void key_generic_reload(void)
case 1: case 1:
break; break;
case 2: case 2:
path_apts_backup = mem_malloc(strlen(path_apts) + asprintf(&path_apts_backup, "%s%s", path_apts,
strlen(backup_ext) + 1); backup_ext);
path_todo_backup = mem_malloc(strlen(path_todo) + asprintf(&path_todo_backup, "%s%s", path_todo,
strlen(backup_ext) + 1); backup_ext);
sprintf(path_apts_backup, "%s%s", path_apts,
backup_ext);
sprintf(path_todo_backup, "%s%s", path_todo,
backup_ext);
io_save_mutex_lock(); io_save_mutex_lock();
io_save_apts(path_apts_backup); io_save_apts(path_apts_backup);
@ -313,7 +309,7 @@ static inline void key_generic_reload(void)
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
wins_update(FLAG_STA); wins_update(FLAG_STA);
return; goto cleanup;
} }
} }
@ -346,6 +342,9 @@ static inline void key_generic_reload(void)
notify_check_next_app(1); notify_check_next_app(1);
ui_calendar_monthly_view_cache_set_invalid(); ui_calendar_monthly_view_cache_set_invalid();
wins_update(FLAG_ALL); wins_update(FLAG_ALL);
cleanup:
mem_free(msg_um_asktype);
} }
static inline void key_generic_import(void) static inline void key_generic_import(void)
@ -533,6 +532,7 @@ static inline void key_generic_cmd(void)
char cmd[BUFSIZ] = ""; char cmd[BUFSIZ] = "";
char *cmd_name; char *cmd_name;
int valid = 0, force = 0; int valid = 0, force = 0;
char *error_msg;
status_mesg(_("Command:"), ""); status_mesg(_("Command:"), "");
if (getstring(win[STA].p, cmd, BUFSIZ, 0, 1) != GETSTRING_VALID) 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, " "); char *topic = strtok(NULL, " ");
if (!display_help(topic)) { if (!display_help(topic)) {
char error_msg[BUFSIZ]; asprintf(&error_msg,
_("Help topic does not exist: %s"), topic);
snprintf(error_msg, BUFSIZ,
_("Help topic does not exist: %s"),
topic);
error_msg[BUFSIZ - 1] = '\0';
warnbox(error_msg); warnbox(error_msg);
mem_free(error_msg);
} }
valid = 1; valid = 1;
} }
if (!valid) { if (!valid) {
char error_msg[BUFSIZ]; asprintf(&error_msg, _("No such command: %s"), cmd);
snprintf(error_msg, BUFSIZ, _("No such command: %s"), cmd);
error_msg[BUFSIZ - 1] = '\0';
warnbox(error_msg); warnbox(error_msg);
mem_free(error_msg);
} }
cleanup: cleanup:

View File

@ -449,8 +449,7 @@ config_serialize_conf(char *buf, const char *key,
for (i = 0; i < ARRAY_SIZE(confmap); i++) { for (i = 0; i < ARRAY_SIZE(confmap); i++) {
if (!strcmp(confmap[i].key, key)) { if (!strcmp(confmap[i].key, key)) {
if (confmap[i]. if (confmap[i].fn_serialize(buf, confmap[i].target)) {
fn_serialize(buf, confmap[i].target)) {
if (status) if (status)
status->done[i] = 1; status->done[i] = 1;
return 1; return 1;

View File

@ -803,14 +803,15 @@ print_keys_bindings(WINDOW * win, int selected_row, int selected_elm,
noelm = y = 0; noelm = y = 0;
for (action = 0; action < NBKEYS; action++) { for (action = 0; action < NBKEYS; action++) {
char actionstr[BUFSIZ]; char *actionstr;
int nbkeys; int nbkeys;
nbkeys = keys_action_count_keys(action); 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) if (action == selected_row)
custom_apply_attr(win, ATTR_HIGHEST); custom_apply_attr(win, ATTR_HIGHEST);
mvwprintw(win, y, XPOS, "%s ", actionstr); mvwprintw(win, y, XPOS, "%s ", actionstr);
mem_free(actionstr);
mvwaddstr(win, y, EQUALPOS, "="); mvwaddstr(win, y, EQUALPOS, "=");
if (nbkeys == 0) if (nbkeys == 0)
mvwaddstr(win, y, KEYPOS, _("undefined")); mvwaddstr(win, y, KEYPOS, _("undefined"));

View File

@ -36,7 +36,7 @@
#include "calcurse.h" #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; int i;
char *locale = NULL; char *locale = NULL;
@ -47,29 +47,33 @@ static int find_basedir(const char *locale_info[], unsigned n, char *basedir)
continue; continue;
locale = strdup(locale_info[i]); locale = strdup(locale_info[i]);
snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); asprintf(basedir, "%s/%s", DOCDIR, locale);
if (io_dir_exists(basedir)) { if (io_dir_exists(*basedir)) {
ret = 1; ret = 1;
goto cleanup; goto cleanup;
} }
strtok(locale, ".@"); strtok(locale, ".@");
snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); mem_free(*basedir);
if (io_dir_exists(basedir)) { asprintf(basedir, "%s/%s", DOCDIR, locale);
if (io_dir_exists(*basedir)) {
ret = 1; ret = 1;
goto cleanup; goto cleanup;
} }
strtok(locale, "_"); strtok(locale, "_");
snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); mem_free(*basedir);
if (io_dir_exists(basedir)) { asprintf(basedir, "%s/%s", DOCDIR, locale);
if (io_dir_exists(*basedir)) {
ret = 1; ret = 1;
goto cleanup; goto cleanup;
} }
free(locale); mem_free(*basedir);
basedir = NULL;
mem_free(locale);
locale = NULL; locale = NULL;
} }
@ -87,23 +91,27 @@ int display_help(const char *topic)
getenv("LC_MESSAGE"), getenv("LC_MESSAGE"),
getenv("LANG") getenv("LANG")
}; };
char basedir[BUFSIZ]; char *basedir;
char path[BUFSIZ]; char *path;
int ret = 0;
if (!topic) if (!topic)
topic = "intro"; topic = "intro";
if (!find_basedir(locale_info, ARRAY_SIZE(locale_info), basedir)) if (!find_basedir(locale_info, ARRAY_SIZE(locale_info), &basedir)) {
snprintf(basedir, BUFSIZ, DOCDIR); 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 && if (!io_file_exists(path) && keys_str2int(topic) > 0 &&
keys_get_action(keys_str2int(topic)) > 0) { keys_get_action(keys_str2int(topic)) > 0) {
int ch = keys_str2int(topic); int ch = keys_str2int(topic);
enum key action = keys_get_action(ch); enum key action = keys_get_action(ch);
topic = keys_get_label(action); 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)) { if (!io_file_exists(path)) {
@ -185,14 +193,17 @@ int display_help(const char *topic)
topic = "priority"; topic = "priority";
else if (!strcmp(topic, "lower-priority")) else if (!strcmp(topic, "lower-priority"))
topic = "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)) { if (io_file_exists(path)) {
const char *arg[] = { conf.pager, path, NULL }; const char *arg[] = { conf.pager, path, NULL };
wins_launch_external(arg); wins_launch_external(arg);
return 1; ret = 1;
} else {
return 0;
} }
mem_free(basedir);
mem_free(path);
return ret;
} }

View File

@ -166,13 +166,11 @@ static FILE *get_export_stream(enum export_type type)
const char *file_ext[IO_EXPORT_NBTYPES] = { "ical", "txt" }; const char *file_ext[IO_EXPORT_NBTYPES] = { "ical", "txt" };
stream = NULL; stream = NULL;
stream_name = (char *)mem_malloc(BUFSIZ);
if ((home = getenv("HOME")) != NULL) if ((home = getenv("HOME")) != NULL)
snprintf(stream_name, BUFSIZ, "%s/calcurse.%s", home, asprintf(&stream_name, "%s/calcurse.%s", home, file_ext[type]);
file_ext[type]);
else else
snprintf(stream_name, BUFSIZ, "%s/calcurse.%s", asprintf(&stream_name, "%s/calcurse.%s", get_tempdir(),
get_tempdir(), file_ext[type]); file_ext[type]);
while (stream == NULL) { while (stream == NULL) {
status_mesg(question, ""); status_mesg(question, "");
@ -186,8 +184,8 @@ static FILE *get_export_stream(enum export_type type)
wgetch(win[KEY].p); wgetch(win[KEY].p);
} }
} }
mem_free(stream_name);
mem_free(stream_name);
return stream; return stream;
} }
@ -196,7 +194,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...)
{ {
FILE *fp; FILE *fp;
va_list ap; va_list ap;
char buf[BUFSIZ]; char *buf;
int ret; int ret;
fp = fopen(fname, "a"); fp = fopen(fname, "a");
@ -204,7 +202,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...)
strerror(errno)); strerror(errno));
va_start(ap, fmt); 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")); RETVAL_IF(ret < 0, 0, _("Failed to build message\n"));
va_end(ap); 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"), RETVAL_IF(ret != 0, 0, _("Failed to close \"%s\" - %s\n"),
fname, strerror(errno)); fname, strerror(errno));
mem_free(buf);
return 1; return 1;
} }
@ -809,14 +808,14 @@ void io_load_keys(const char *pager)
int ch; int ch;
if ((ch = keys_str2int(key_ch)) < 0) { if ((ch = keys_str2int(key_ch)) < 0) {
char unknown_key[BUFSIZ]; char *unknown_key;
skipped++; skipped++;
(void)snprintf(unknown_key, BUFSIZ, asprintf(&unknown_key,
_("Error reading key: \"%s\""), _("Error reading key: \"%s\""),
key_ch); key_ch);
io_log_print(log, line, io_log_print(log, line, unknown_key);
unknown_key); mem_free(unknown_key);
} else { } else {
int used; int used;
@ -825,18 +824,15 @@ void io_load_keys(const char *pager)
ht_elm-> ht_elm->
key); key);
if (used) { if (used) {
char already_assigned char *already_assigned;
[BUFSIZ];
skipped++; skipped++;
(void) asprintf(&already_assigned,
snprintf _("\"%s\" assigned multiple times!"),
(already_assigned, key_ch);
BUFSIZ,
_("\"%s\" assigned multiple times!"),
key_ch);
io_log_print(log, line, io_log_print(log, line,
already_assigned); already_assigned);
mem_free(already_assigned);
} else { } else {
assigned++; assigned++;
} }
@ -1048,7 +1044,7 @@ void io_import_data(enum import_type type, const char *stream_name)
{ {
const char *proc_report = const char *proc_report =
_("Import process report: %04d lines read "); _("Import process report: %04d lines read ");
char stats_str[4][BUFSIZ]; char *stats_str[4];
FILE *stream = NULL; FILE *stream = NULL;
struct io_file *log; struct io_file *log;
struct { struct {
@ -1092,25 +1088,25 @@ void io_import_data(enum import_type type, const char *stream_name)
if (stream != stdin) if (stream != stdin)
file_close(stream, __FILE_POS__); file_close(stream, __FILE_POS__);
snprintf(stats_str[0], BUFSIZ, asprintf(&stats_str[0], ngettext("%d app", "%d apps", stats.apoints),
ngettext("%d app", "%d apps", stats.apoints),
stats.apoints); stats.apoints);
snprintf(stats_str[1], BUFSIZ, asprintf(&stats_str[1],
ngettext("%d event", "%d events", stats.events), ngettext("%d event", "%d events", stats.events),
stats.events); stats.events);
snprintf(stats_str[2], BUFSIZ, asprintf(&stats_str[2], ngettext("%d todo", "%d todos", stats.todos),
ngettext("%d todo", "%d todos", stats.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) { if (ui_mode == UI_CURSES && conf.system_dialogs) {
char read[BUFSIZ], stat[BUFSIZ]; char *read, *stat;
snprintf(read, BUFSIZ, proc_report, stats.lines); asprintf(&read, proc_report, stats.lines);
snprintf(stat, BUFSIZ, "%s / %s / %s / %s (%s)", asprintf(&stat, "%s / %s / %s / %s (%s)",
stats_str[0], stats_str[1], stats_str[2], stats_str[0], stats_str[1], stats_str[2],
stats_str[3], _("Press [ENTER] to continue")); stats_str[3], _("Press [ENTER] to continue"));
status_mesg(read, stat); status_mesg(read, stat);
mem_free(read);
mem_free(stat);
wgetch(win[KEY].p); wgetch(win[KEY].p);
} else if (ui_mode == UI_CMDLINE) { } else if (ui_mode == UI_CMDLINE) {
printf(proc_report, stats.lines); 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); 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); io_log_free(log);
} }
struct io_file *io_log_init(void) struct io_file *io_log_init(void)
{ {
char logprefix[BUFSIZ]; char *logprefix, *logname;
char *logname;
struct io_file *log; struct io_file *log;
snprintf(logprefix, BUFSIZ, "%s/calcurse_log.", get_tempdir()); asprintf(&logprefix, "%s/calcurse_log.", get_tempdir());
logname = new_tempfile(logprefix, TMPEXTSIZ); logname = new_tempfile(logprefix, TMPEXTSIZ);
RETVAL_IF(logname == NULL, 0, RETVAL_IF(logname == NULL, 0,
_("Warning: could not create temporary log file, Aborting...")); _("Warning: could not create temporary log file, Aborting..."));
@ -1145,6 +1145,7 @@ struct io_file *io_log_init(void)
RETVAL_IF(log == NULL, 0, RETVAL_IF(log == NULL, 0,
_("Warning: could not open temporary log file, Aborting...")); _("Warning: could not open temporary log file, Aborting..."));
snprintf(log->name, sizeof(log->name), "%s%s", logprefix, logname); snprintf(log->name, sizeof(log->name), "%s%s", logprefix, logname);
mem_free(logprefix);
mem_free(logname); mem_free(logname);
log->fd = fopen(log->name, "w"); log->fd = fopen(log->name, "w");
if (log->fd == NULL) { if (log->fd == NULL) {

View File

@ -59,17 +59,18 @@ HTABLE_PROTOTYPE(htp, note_gc_hash)
char *generate_note(const char *str) char *generate_note(const char *str)
{ {
char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1); char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
char notepath[BUFSIZ]; char *notepath;
FILE *fp; FILE *fp;
sha1_digest(str, sha1); sha1_digest(str, sha1);
snprintf(notepath, BUFSIZ, "%s%s", path_notes, sha1); asprintf(&notepath, "%s%s", path_notes, sha1);
fp = fopen(notepath, "w"); fp = fopen(notepath, "w");
EXIT_IF(fp == NULL, _("Warning: could not open %s, Aborting..."), EXIT_IF(fp == NULL, _("Warning: could not open %s, Aborting..."),
notepath); notepath);
fputs(str, fp); fputs(str, fp);
file_close(fp, __FILE_POS__); file_close(fp, __FILE_POS__);
mem_free(notepath);
return sha1; return sha1;
} }
@ -78,7 +79,7 @@ void edit_note(char **note, const char *editor)
{ {
char tmppath[BUFSIZ]; char tmppath[BUFSIZ];
char *tmpext; char *tmpext;
char notepath[BUFSIZ]; char *notepath = NULL;
char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1); char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
FILE *fp; FILE *fp;
@ -91,7 +92,7 @@ void edit_note(char **note, const char *editor)
mem_free(tmpext); mem_free(tmpext);
if (*note != NULL) { if (*note != NULL) {
snprintf(notepath, BUFSIZ, "%s%s", path_notes, *note); asprintf(&notepath, "%s%s", path_notes, *note);
io_file_cp(notepath, tmppath); io_file_cp(notepath, tmppath);
} }
@ -105,8 +106,10 @@ void edit_note(char **note, const char *editor)
fclose(fp); fclose(fp);
*note = sha1; *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); io_file_cp(tmppath, notepath);
mem_free(notepath);
} }
unlink(tmppath); unlink(tmppath);
@ -115,14 +118,16 @@ void edit_note(char **note, const char *editor)
/* View a note in an external pager. */ /* View a note in an external pager. */
void view_note(const char *note, const char *pager) void view_note(const char *note, const char *pager)
{ {
char fullname[BUFSIZ]; char *fullname;
if (note == NULL) if (note == NULL)
return; return;
snprintf(fullname, BUFSIZ, "%s%s", path_notes, note); asprintf(&fullname, "%s%s", path_notes, note);
const char *arg[] = { pager, fullname, NULL }; const char *arg[] = { pager, fullname, NULL };
wins_launch_external(arg); wins_launch_external(arg);
mem_free(fullname);
} }
/* Erase a note previously attached to an item. */ /* Erase a note previously attached to an item. */
@ -172,7 +177,7 @@ void note_gc(void)
struct dirent *dp; struct dirent *dp;
llist_item_t *i; llist_item_t *i;
struct note_gc_hash tmph; struct note_gc_hash tmph;
char notepath[BUFSIZ]; char *notepath;
if (!(dirp = opendir(path_notes))) if (!(dirp = opendir(path_notes)))
return; return;
@ -235,7 +240,8 @@ void note_gc(void)
/* Unlink unused note files. */ /* Unlink unused note files. */
HTABLE_FOREACH(hp, htp, &gc_htable) { 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); unlink(notepath);
mem_free(notepath);
} }
} }

View File

@ -629,7 +629,7 @@ void ui_calendar_change_day(int datefmt)
{ {
#define LDAY 11 #define LDAY 11
char selected_day[LDAY] = ""; char selected_day[LDAY] = "";
char outstr[BUFSIZ]; char *outstr;
int dday, dmonth, dyear; int dday, dmonth, dyear;
int wrong_day = 1; int wrong_day = 1;
const char *mesg_line1 = 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"); _("Enter the day to go to [ENTER for today] : %s");
while (wrong_day) { while (wrong_day) {
snprintf(outstr, BUFSIZ, request_date, asprintf(&outstr, request_date, DATEFMT_DESC(datefmt));
DATEFMT_DESC(datefmt));
status_mesg(outstr, ""); status_mesg(outstr, "");
mem_free(outstr);
if (getstring(win[STA].p, selected_day, LDAY, 0, 1) == if (getstring(win[STA].p, selected_day, LDAY, 0, 1) ==
GETSTRING_ESC) { GETSTRING_ESC) {
return; return;

View File

@ -156,8 +156,7 @@ static void update_rept(struct rpt **rpt, const long start)
{ {
int newtype, newfreq; int newtype, newfreq;
long newuntil; long newuntil;
char outstr[BUFSIZ]; char *freqstr = NULL, *timstr, *outstr = NULL;
char *freqstr, *timstr;
const char *msg_rpt_prefix = _("Enter the new repetition type:"); const char *msg_rpt_prefix = _("Enter the new repetition type:");
const char *msg_rpt_daily = _("(d)aily"); const char *msg_rpt_daily = _("(d)aily");
const char *msg_rpt_weekly = _("(w)eekly"); 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. */ /* Find the current repetition type. */
const char *rpt_current; const char *rpt_current;
char msg_rpt_current[BUFSIZ]; char *msg_rpt_current;
switch (recur_def2char((*rpt)->type)) { switch (recur_def2char((*rpt)->type)) {
case 'D': case 'D':
rpt_current = msg_rpt_daily; rpt_current = msg_rpt_daily;
@ -185,15 +184,12 @@ static void update_rept(struct rpt **rpt, const long start)
rpt_current = msg_rpt_daily; rpt_current = msg_rpt_daily;
} }
snprintf(msg_rpt_current, BUFSIZ, _("(currently using %s)"), asprintf(&msg_rpt_current, _("(currently using %s)"), rpt_current);
rpt_current);
char msg_rpt_asktype[BUFSIZ]; char *msg_rpt_asktype;
snprintf(msg_rpt_asktype, BUFSIZ, "%s %s, %s, %s, %s ? %s", asprintf(&msg_rpt_asktype, "%s %s, %s, %s, %s ? %s", msg_rpt_prefix,
msg_rpt_prefix, msg_rpt_daily, msg_rpt_weekly, msg_rpt_monthly,
msg_rpt_daily, msg_rpt_yearly, msg_rpt_current);
msg_rpt_weekly, msg_rpt_monthly, msg_rpt_yearly,
msg_rpt_current);
const char *msg_rpt_choice = _("[dwmy]"); const char *msg_rpt_choice = _("[dwmy]");
const char *msg_wrong_freq = const char *msg_wrong_freq =
@ -219,17 +215,16 @@ static void update_rept(struct rpt **rpt, const long start)
newtype = 'Y'; newtype = 'Y';
break; break;
default: default:
return; goto cleanup;
} }
do { do {
status_mesg(_("Enter the new repetition frequence:"), ""); status_mesg(_("Enter the new repetition frequence:"), "");
freqstr = mem_malloc(BUFSIZ); asprintf(&freqstr, "%d", (*rpt)->freq);
snprintf(freqstr, BUFSIZ, "%d", (*rpt)->freq);
if (updatestring(win[STA].p, &freqstr, 0, 1) != if (updatestring(win[STA].p, &freqstr, 0, 1) !=
GETSTRING_VALID) { GETSTRING_VALID) {
mem_free(freqstr); mem_free(freqstr);
return; goto cleanup;
} }
newfreq = atoi(freqstr); newfreq = atoi(freqstr);
mem_free(freqstr); mem_free(freqstr);
@ -246,8 +241,7 @@ static void update_rept(struct rpt **rpt, const long start)
struct date new_date; struct date new_date;
int newmonth, newday, newyear; int newmonth, newday, newyear;
snprintf(outstr, BUFSIZ, asprintf(&outstr, _("Enter the new ending date: [%s] or '0'"),
_("Enter the new ending date: [%s] or '0'"),
DATEFMT_DESC(conf.input_datefmt)); DATEFMT_DESC(conf.input_datefmt));
status_mesg(outstr, ""); status_mesg(outstr, "");
timstr = timstr =
@ -255,8 +249,7 @@ static void update_rept(struct rpt **rpt, const long start)
DATEFMT(conf.input_datefmt)); DATEFMT(conf.input_datefmt));
if (updatestring(win[STA].p, &timstr, 0, 1) != if (updatestring(win[STA].p, &timstr, 0, 1) !=
GETSTRING_VALID) { GETSTRING_VALID) {
mem_free(timstr); goto cleanup;
return;
} }
if (strcmp(timstr, "0") == 0) { if (strcmp(timstr, "0") == 0) {
newuntil = 0; newuntil = 0;
@ -265,7 +258,7 @@ static void update_rept(struct rpt **rpt, const long start)
if (!parse_date if (!parse_date
(timstr, conf.input_datefmt, &newyear, &newmonth, (timstr, conf.input_datefmt, &newyear, &newmonth,
&newday, ui_calendar_get_slctd_day())) { &newday, ui_calendar_get_slctd_day())) {
snprintf(outstr, BUFSIZ, msg_fmts, asprintf(&outstr, msg_fmts,
DATEFMT_DESC(conf.input_datefmt)); DATEFMT_DESC(conf.input_datefmt));
status_mesg(msg_wrong_date, outstr); status_mesg(msg_wrong_date, outstr);
wgetch(win[KEY].p); wgetch(win[KEY].p);
@ -283,10 +276,16 @@ static void update_rept(struct rpt **rpt, const long start)
wgetch(win[KEY].p); wgetch(win[KEY].p);
} }
mem_free(timstr);
(*rpt)->type = recur_char2def(newtype); (*rpt)->type = recur_char2def(newtype);
(*rpt)->freq = newfreq; (*rpt)->freq = newfreq;
(*rpt)->until = newuntil; (*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. */ /* Edit an already existing item. */
@ -650,7 +649,7 @@ void ui_day_item_repeat(void)
time_t t; time_t t;
int year = 0, month = 0, day = 0; int year = 0, month = 0, day = 0;
struct date until_date; struct date until_date;
char outstr[BUFSIZ]; char *outstr = NULL;
char user_input[BUFSIZ] = ""; char user_input[BUFSIZ] = "";
const char *msg_rpt_prefix = _("Enter the repetition type:"); const char *msg_rpt_prefix = _("Enter the repetition type:");
const char *msg_rpt_daily = _("(d)aily"); const char *msg_rpt_daily = _("(d)aily");
@ -672,9 +671,8 @@ void ui_day_item_repeat(void)
const char *mesg_older = const char *mesg_older =
_("Sorry, the date you entered is older than the item start time."); _("Sorry, the date you entered is older than the item start time.");
char msg_asktype[BUFSIZ]; char *msg_asktype;
snprintf(msg_asktype, BUFSIZ, "%s %s, %s, %s, %s", asprintf(&msg_asktype, "%s %s, %s, %s, %s", msg_rpt_prefix,
msg_rpt_prefix,
msg_rpt_daily, msg_rpt_weekly, msg_rpt_monthly, msg_rpt_daily, msg_rpt_weekly, msg_rpt_monthly,
msg_rpt_yearly); msg_rpt_yearly);
@ -685,14 +683,14 @@ void ui_day_item_repeat(void)
long until, date; long until, date;
if (day_item_count(0) <= 0) if (day_item_count(0) <= 0)
return; goto cleanup;
item_nb = listbox_get_sel(&lb_apt); item_nb = listbox_get_sel(&lb_apt);
p = day_get_item(item_nb); p = day_get_item(item_nb);
if (p->type != APPT && p->type != EVNT) { if (p->type != APPT && p->type != EVNT) {
status_mesg(wrong_type_1, wrong_type_2); status_mesg(wrong_type_1, wrong_type_2);
wgetch(win[KEY].p); wgetch(win[KEY].p);
return; goto cleanup;
} }
switch (status_ask_choice(msg_asktype, msg_type_choice, 4)) { switch (status_ask_choice(msg_asktype, msg_type_choice, 4)) {
@ -709,14 +707,14 @@ void ui_day_item_repeat(void)
type = RECUR_YEARLY; type = RECUR_YEARLY;
break; break;
default: default:
return; goto cleanup;
} }
while (freq == 0) { while (freq == 0) {
status_mesg(mesg_freq_1, ""); status_mesg(mesg_freq_1, "");
if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) != if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) !=
GETSTRING_VALID) GETSTRING_VALID)
return; goto cleanup;
freq = atoi(user_input); freq = atoi(user_input);
if (freq == 0) { if (freq == 0) {
status_mesg(mesg_wrong_freq, wrong_type_2); status_mesg(mesg_wrong_freq, wrong_type_2);
@ -726,12 +724,12 @@ void ui_day_item_repeat(void)
} }
for (;;) { for (;;) {
snprintf(outstr, BUFSIZ, mesg_until_1, asprintf(&outstr, mesg_until_1,
DATEFMT_DESC(conf.input_datefmt)); DATEFMT_DESC(conf.input_datefmt));
status_mesg(outstr, ""); status_mesg(outstr, "");
if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) != if (getstring(win[STA].p, user_input, BUFSIZ, 0, 1) !=
GETSTRING_VALID) GETSTRING_VALID)
return; goto cleanup;
if (strlen(user_input) == 1 if (strlen(user_input) == 1
&& strcmp(user_input, "0") == 0) { && strcmp(user_input, "0") == 0) {
until = 0; until = 0;
@ -752,7 +750,8 @@ void ui_day_item_repeat(void)
status_mesg(mesg_older, wrong_type_2); status_mesg(mesg_older, wrong_type_2);
wgetch(win[KEY].p); wgetch(win[KEY].p);
} else { } else {
snprintf(outstr, BUFSIZ, mesg_wrong_2, mem_free(outstr);
asprintf(&outstr, mesg_wrong_2,
DATEFMT_DESC(conf.input_datefmt)); DATEFMT_DESC(conf.input_datefmt));
status_mesg(mesg_wrong_1, outstr); status_mesg(mesg_wrong_1, outstr);
wgetch(win[KEY].p); wgetch(win[KEY].p);
@ -783,6 +782,10 @@ void ui_day_item_repeat(void)
io_set_modified(); io_set_modified();
ui_calendar_monthly_view_cache_set_invalid(); ui_calendar_monthly_view_cache_set_invalid();
cleanup:
mem_free(msg_asktype);
mem_free(outstr);
} }
/* Free the current cut item, if any. */ /* Free the current cut item, if any. */

View File

@ -197,7 +197,7 @@ int status_ask_choice(const char *message, const char choice[],
int nb_choice) int nb_choice)
{ {
int i, ch; int i, ch;
char tmp[BUFSIZ]; char *tmp;
/* "[4/2/f/t/w/.../Z] " */ /* "[4/2/f/t/w/.../Z] " */
char avail_choice[2 * nb_choice + 3]; 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'; avail_choice[1] = '\0';
for (i = 1; i <= nb_choice; i++) { for (i = 1; i <= nb_choice; i++) {
snprintf(tmp, BUFSIZ, (i == nb_choice) ? "%c] " : "%c/", asprintf(&tmp, (i == nb_choice) ? "%c] " : "%c/", choice[i]);
choice[i]);
strcat(avail_choice, tmp); strcat(avail_choice, tmp);
mem_free(tmp);
} }
status_mesg(message, avail_choice); status_mesg(message, avail_choice);
@ -248,7 +248,7 @@ status_ask_simplechoice(const char *prefix, const char *choice[],
int nb_choice) int nb_choice)
{ {
int i; int i;
char tmp[BUFSIZ]; char *tmp;
/* "(1) Choice1, (2) Choice2, (3) Choice3?" */ /* "(1) Choice1, (2) Choice2, (3) Choice3?" */
char choicestr[BUFSIZ]; char choicestr[BUFSIZ];
/* Holds the characters to choose from ('1', '2', etc) */ /* 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); strcpy(choicestr, prefix);
for (i = 0; i < nb_choice; i++) { for (i = 0; i < nb_choice; i++) {
snprintf(tmp, BUFSIZ, asprintf(&tmp,
((i + 1) == nb_choice) ? "(%d) %s?" : "(%d) %s, ", ((i + 1) == nb_choice) ? "(%d) %s?" : "(%d) %s, ",
(i + 1), choice[i]); (i + 1), choice[i]);
strcat(choicestr, tmp); strcat(choicestr, tmp);
mem_free(tmp);
} }
return (status_ask_choice(choicestr, char_choice, nb_choice)); 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"; narg[1] = "-c";
if (argc > 1) { if (argc > 1) {
arg0 = mem_malloc(strlen(path) + 6); asprintf(&arg0, "%s \"$@\"", path);
sprintf(arg0, "%s \"$@\"", path);
narg[2] = arg0; narg[2] = arg0;
for (i = 0; i < argc; i++) 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) static void print_notefile(FILE * out, const char *filename, int nbtab)
{ {
char path_to_notefile[BUFSIZ]; char *path_to_notefile;
FILE *notefile; FILE *notefile;
char linestarter[BUFSIZ]; char linestarter[BUFSIZ];
char buffer[BUFSIZ]; char buffer[BUFSIZ];
@ -1064,8 +1064,9 @@ static void print_notefile(FILE * out, const char *filename, int nbtab)
linestarter[0] = '\0'; 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"); notefile = fopen(path_to_notefile, "r");
mem_free(path_to_notefile);
if (notefile) { if (notefile) {
while (fgets(buffer, BUFSIZ, notefile) != 0) { while (fgets(buffer, BUFSIZ, notefile) != 0) {
if (printlinestarter) { if (printlinestarter) {