Skip to content

Commit

Permalink
virtio: Fix memory leaks reported by Coverity
Browse files Browse the repository at this point in the history
All four leaks are similar, so fix them in one patch.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
  • Loading branch information
stweil authored and mstsirkin committed Mar 16, 2015
1 parent 9e0f5b8 commit 6c936b7
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions hw/9pfs/virtio-9p-local.c
Expand Up @@ -488,7 +488,7 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
int err = -1;
int serrno = 0;
V9fsString fullname;
char *buffer;
char *buffer = NULL;

v9fs_string_init(&fullname);
v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
Expand All @@ -499,7 +499,6 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
if (err == -1) {
g_free(buffer);
goto out;
}
err = local_set_xattr(buffer, credp);
Expand All @@ -512,7 +511,6 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0);
if (err == -1) {
g_free(buffer);
goto out;
}
err = local_set_mapped_file_attr(fs_ctx, path, credp);
Expand All @@ -525,7 +523,6 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mknod(buffer, credp->fc_mode, credp->fc_rdev);
if (err == -1) {
g_free(buffer);
goto out;
}
err = local_post_create_passthrough(fs_ctx, path, credp);
Expand All @@ -539,8 +536,8 @@ static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
err_end:
remove(buffer);
errno = serrno;
g_free(buffer);
out:
g_free(buffer);
v9fs_string_free(&fullname);
return err;
}
Expand All @@ -552,7 +549,7 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
int err = -1;
int serrno = 0;
V9fsString fullname;
char *buffer;
char *buffer = NULL;

v9fs_string_init(&fullname);
v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
Expand All @@ -563,7 +560,6 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
if (err == -1) {
g_free(buffer);
goto out;
}
credp->fc_mode = credp->fc_mode|S_IFDIR;
Expand All @@ -576,7 +572,6 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS);
if (err == -1) {
g_free(buffer);
goto out;
}
credp->fc_mode = credp->fc_mode|S_IFDIR;
Expand All @@ -590,7 +585,6 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
buffer = rpath(fs_ctx, path);
err = mkdir(buffer, credp->fc_mode);
if (err == -1) {
g_free(buffer);
goto out;
}
err = local_post_create_passthrough(fs_ctx, path, credp);
Expand All @@ -604,8 +598,8 @@ static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
err_end:
remove(buffer);
errno = serrno;
g_free(buffer);
out:
g_free(buffer);
v9fs_string_free(&fullname);
return err;
}
Expand Down Expand Up @@ -659,7 +653,7 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
int err = -1;
int serrno = 0;
V9fsString fullname;
char *buffer;
char *buffer = NULL;

/*
* Mark all the open to not follow symlinks
Expand All @@ -675,7 +669,6 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
buffer = rpath(fs_ctx, path);
fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
if (fd == -1) {
g_free(buffer);
err = fd;
goto out;
}
Expand All @@ -690,7 +683,6 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
buffer = rpath(fs_ctx, path);
fd = open(buffer, flags, SM_LOCAL_MODE_BITS);
if (fd == -1) {
g_free(buffer);
err = fd;
goto out;
}
Expand All @@ -706,7 +698,6 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
buffer = rpath(fs_ctx, path);
fd = open(buffer, flags, credp->fc_mode);
if (fd == -1) {
g_free(buffer);
err = fd;
goto out;
}
Expand All @@ -724,8 +715,8 @@ static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
close(fd);
remove(buffer);
errno = serrno;
g_free(buffer);
out:
g_free(buffer);
v9fs_string_free(&fullname);
return err;
}
Expand All @@ -738,7 +729,7 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
int serrno = 0;
char *newpath;
V9fsString fullname;
char *buffer;
char *buffer = NULL;

v9fs_string_init(&fullname);
v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
Expand All @@ -751,7 +742,6 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
buffer = rpath(fs_ctx, newpath);
fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
if (fd == -1) {
g_free(buffer);
err = fd;
goto out;
}
Expand Down Expand Up @@ -781,7 +771,6 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
buffer = rpath(fs_ctx, newpath);
fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS);
if (fd == -1) {
g_free(buffer);
err = fd;
goto out;
}
Expand Down Expand Up @@ -810,7 +799,6 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
buffer = rpath(fs_ctx, newpath);
err = symlink(oldpath, buffer);
if (err) {
g_free(buffer);
goto out;
}
err = lchown(buffer, credp->fc_uid, credp->fc_gid);
Expand All @@ -831,8 +819,8 @@ static int local_symlink(FsContext *fs_ctx, const char *oldpath,
err_end:
remove(buffer);
errno = serrno;
g_free(buffer);
out:
g_free(buffer);
v9fs_string_free(&fullname);
return err;
}
Expand Down

0 comments on commit 6c936b7

Please sign in to comment.