]> granicus.if.org Git - neomutt/commitdiff
fix dirname for static strings 1171/head
authorRichard Russon <rich@flatcap.org>
Thu, 3 May 2018 10:50:40 +0000 (11:50 +0100)
committerRichard Russon <rich@flatcap.org>
Thu, 3 May 2018 11:51:19 +0000 (12:51 +0100)
When passed NULL or a single directory, dirname() can return a static
string of ".", so we need to strdup() it.

mutt/file.c
mutt/file.h
mutt/string.c

index 708fc7634264f05aa56231a6da2df9dbdf1af79f..5e5926154d43a3a8dbdc59f885b72afd438eabdb 100644 (file)
@@ -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));
 }
+
index e15ca8100c0e9f32ab621578540d6aa5d259c787..ad1ed571a545083117194a0fce0b9922100bf8e7 100644 (file)
@@ -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);
index 85edab6c12c73e32d4ee2ed9d5be8ba46378036e..818ac32655eb62485fbdf6747796799f21d5859f 100644 (file)
@@ -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'))