]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Fix t_get_working_dir() to properly allocate memory in data stack.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 30 Jan 2017 17:17:33 +0000 (19:17 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 30 Jan 2017 20:50:23 +0000 (22:50 +0200)
src/lib/path-util.c

index 2c4285be879398c6d8d8b99000b0d18c9f32849e..555194b2344c563d296bec86600db55c6b563d4c 100644 (file)
@@ -11,8 +11,8 @@
 #define PATH_UTIL_MAX_PATH      8*1024
 #define PATH_UTIL_MAX_SYMLINKS  80
 
-static int t_getcwd_alloc(char **dir_r, size_t *asize_r,
-                         const char **error_r) ATTR_NULL(2)
+static int t_getcwd_noalloc(char **dir_r, size_t *asize_r,
+                           const char **error_r) ATTR_NULL(2)
 {
        /* @UNSAFE */
        char *dir;
@@ -48,7 +48,7 @@ static int path_normalize(const char *path, bool resolve_links,
 
        if (path[0] != '/') {
                /* relative; initialize npath with current directory */
-               if (t_getcwd_alloc(&npath, &asize, error_r) < 0)
+               if (t_getcwd_noalloc(&npath, &asize, error_r) < 0)
                        return -1;
                npath_pos = npath + strlen(npath);
                i_assert(npath[0] == '/');
@@ -304,9 +304,16 @@ const char *t_abspath_to(const char *path, const char *root)
 
 int t_get_working_dir(const char **dir_r, const char **error_r)
 {
+       char *dir;
+
        i_assert(dir_r != NULL);
        i_assert(error_r != NULL);
-       return t_getcwd_alloc((char**)dir_r, NULL, error_r);
+       if (t_getcwd_noalloc(&dir, NULL, error_r) < 0)
+               return -1;
+
+       t_buffer_alloc(strlen(dir) + 1);
+       *dir_r = dir;
+       return 0;
 }
 
 int t_readlink(const char *path, const char **dest_r, const char **error_r)