summaryrefslogtreecommitdiff
path: root/src/help.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/help.c
parent6203966fbf1f8886ff59cc9d6350b034ee5374c9 (diff)
downloadcalcurse-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/help.c')
-rw-r--r--src/help.c47
1 files changed, 29 insertions, 18 deletions
diff --git a/src/help.c b/src/help.c
index 09f9197..fe144f4 100644
--- a/src/help.c
+++ b/src/help.c
@@ -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;
}