s3: VFS: shadow_copy2: Fix a memory leak in the connectpath function.
authorJeremy Allison <jra@samba.org>
Mon, 23 Jan 2017 18:06:44 +0000 (10:06 -0800)
committerJeremy Allison <jra@samba.org>
Mon, 30 Jan 2017 17:39:19 +0000 (18:39 +0100)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12531

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Uri Simchoni <uri@samba.org>
source3/modules/vfs_shadow_copy2.c

index f116c553ac4d9834c8d0b5e77349c1d7afc820c1..137dad8bf4fb24556409a44f0f4312ae0fafdb2d 100644 (file)
@@ -78,6 +78,8 @@ struct shadow_copy2_private {
        char *shadow_cwd; /* Absolute $cwd path. */
        /* Absolute connectpath - can vary depending on $cwd. */
        char *shadow_connectpath;
+       /* malloc'ed realpath return. */
+       char *shadow_realpath;
 };
 
 static int shadow_copy2_get_shadow_copy_data(
@@ -2494,6 +2496,13 @@ static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
                goto done;
        }
 
+       /*
+        * SMB_VFS_NEXT_REALPATH returns a malloc'ed string.
+        * Don't leak memory.
+        */
+       SAFE_FREE(priv->shadow_realpath);
+       priv->shadow_realpath = result;
+
        DBG_DEBUG("connect path is [%s]\n", result);
 
 done:
@@ -2572,6 +2581,12 @@ static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
        return ret;
 }
 
+static int shadow_copy2_private_destructor(struct shadow_copy2_private *priv)
+{
+       SAFE_FREE(priv->shadow_realpath);
+       return 0;
+}
+
 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                const char *service, const char *user)
 {
@@ -2603,6 +2618,8 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                return -1;
        }
 
+       talloc_set_destructor(priv, shadow_copy2_private_destructor);
+
        priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
        if (priv->snaps == NULL) {
                DBG_ERR("talloc_zero() failed\n");