From cd7cf765d09bd1d80d640847afce0627c0b14870 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Thu, 3 May 2018 11:50:40 +0100 Subject: [PATCH] fix dirname for static strings When passed NULL or a single directory, dirname() can return a static string of ".", so we need to strdup() it. --- mutt/file.c | 11 ++++++----- mutt/file.h | 2 +- mutt/string.c | 7 ++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/mutt/file.c b/mutt/file.c index 708fc7634..5e5926154 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -921,18 +921,18 @@ time_t mutt_file_decrease_mtime(const char *f, struct stat *st) /** * mutt_file_dirname - Return a path up to, but not including, the final '/' - * @param p Path + * @param path Path * @retval ptr The directory containing p * * Unlike the IEEE Std 1003.1-2001 specification of dirname(3), this * implementation does not modify its parameter, so callers need not manually * copy their paths into a modifiable buffer prior to calling this function. */ -char *mutt_file_dirname(const char *p) +char *mutt_file_dirname(const char *path) { - char *buf = mutt_mem_malloc(_POSIX_PATH_MAX); - mutt_str_strfcpy(buf, p, sizeof(buf)); - return dirname(buf); + char buf[PATH_MAX] = { 0 }; + mutt_str_strfcpy(buf, path, sizeof(buf)); + return mutt_str_strdup(dirname(buf)); } /** @@ -1388,3 +1388,4 @@ int mutt_file_check_empty(const char *path) return ((st.st_size == 0)); } + diff --git a/mutt/file.h b/mutt/file.h index e15ca8100..ad1ed571a 100644 --- a/mutt/file.h +++ b/mutt/file.h @@ -46,7 +46,7 @@ char * mutt_file_concat_path(char *d, const char *dir, const char *fname, s int mutt_file_copy_bytes(FILE *in, FILE *out, size_t size); int mutt_file_copy_stream(FILE *fin, FILE *fout); time_t mutt_file_decrease_mtime(const char *f, struct stat *st); -char * mutt_file_dirname(const char *p); +char * mutt_file_dirname(const char *path); int mutt_file_fclose(FILE **f); FILE * mutt_file_fopen(const char *path, const char *mode); int mutt_file_fsync_close(FILE **f); diff --git a/mutt/string.c b/mutt/string.c index 85edab6c1..818ac3265 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -636,8 +636,13 @@ void mutt_str_remove_trailing_ws(char *s) */ size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize) { - if (dsize == 0) + if (!dest || (dsize == 0)) return 0; + if (!src) + { + dest[0] = '\0'; + return 0; + } char *dest0 = dest; while ((--dsize > 0) && (*src != '\0')) -- 2.40.0