cgmnlm

colorful gemini line mode browser
git clone https://git.clttr.info/cgmnlm.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit cb63b8ddf093711607ff98e933d4bd04154a854b
parent ec88f4558c48b3eece906143867dfba6d81e5e49
Author: Giuseppe Lumia <g.lumia@outlook.com>
Date:   Wed, 11 Nov 2020 23:52:03 +0100

Fix bug on mkdirs calls

On some systems dirname uses a static string for its return value, so
we were calling mkdirs recursively on a string that was continuosly changing.

A check was also added after the `snprintf` to make sure there's no
information loss since there is no limit to the length of the string
returned by `get_data_pathfmt`.

Closes #48.

Diffstat:
Msrc/gmnlm.c | 18++++++++++++++----
Msrc/tofu.c | 11++++++++---
2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -154,8 +154,13 @@ save_bookmark(struct browser *browser) { char *path_fmt = get_data_pathfmt(); static char path[PATH_MAX+1]; - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - if (mkdirs(dirname(path), 0755) != 0) { + static char dname[PATH_MAX+1]; + size_t n; + + n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); + assert(n < sizeof(path)); + strncpy(dname, dirname(path), sizeof(dname)); + if (mkdirs(dname, 0755) != 0) { snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); free(path_fmt); fprintf(stderr, "Error creating directory %s: %s\n", @@ -190,8 +195,13 @@ open_bookmarks(struct browser *browser) { char *path_fmt = get_data_pathfmt(); static char path[PATH_MAX+1]; - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - if (mkdirs(dirname(path), 0755) != 0) { + static char dname[PATH_MAX+1]; + size_t n; + + n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); + assert(n < sizeof(path)); + strncpy(dname, dirname(path), sizeof(dname)); + if (mkdirs(dname, 0755) != 0) { snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); free(path_fmt); fprintf(stderr, "Error creating directory %s: %s\n", diff --git a/src/tofu.c b/src/tofu.c @@ -157,10 +157,15 @@ gemini_tofu_init(struct gemini_tofu *tofu, {.var = "HOME", .path = "/.local/share/gemini/%s"} }; char *path_fmt = getpath(paths, sizeof(paths) / sizeof(paths[0])); - snprintf(tofu->known_hosts_path, sizeof(tofu->known_hosts_path), + char dname[PATH_MAX+1]; + size_t n = 0; + + n = snprintf(tofu->known_hosts_path, sizeof(tofu->known_hosts_path), path_fmt, "known_hosts"); + assert(n < sizeof(tofu->known_hosts_path)); - if (mkdirs(dirname(tofu->known_hosts_path), 0755) != 0) { + strncpy(dname, dirname(tofu->known_hosts_path), sizeof(dname)); + if (mkdirs(dname, 0755) != 0) { snprintf(tofu->known_hosts_path, sizeof(tofu->known_hosts_path), path_fmt, "known_hosts"); fprintf(stderr, "Error creating directory %s: %s\n", @@ -182,7 +187,7 @@ gemini_tofu_init(struct gemini_tofu *tofu, if (!f) { return; } - size_t n = 0; + n = 0; char *line = NULL; while (getline(&line, &n, f) != -1) { struct known_host *host = calloc(1, sizeof(struct known_host));