[Qemu-devel] [PATCH] qemu-io: Don't leak pattern file in error path

Kevin Wolf posted 1 patch 4 years, 7 months ago
Test docker-clang@ubuntu passed
Test FreeBSD passed
Test checkpatch passed
Test docker-quick@centos7 passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20190910070949.16256-1-kwolf@redhat.com
Maintainers: Max Reitz <mreitz@redhat.com>, Kevin Wolf <kwolf@redhat.com>
qemu-io-cmds.c | 4 ++++
1 file changed, 4 insertions(+)
[Qemu-devel] [PATCH] qemu-io: Don't leak pattern file in error path
Posted by Kevin Wolf 4 years, 7 months ago
qemu_io_alloc_from_file() needs to close the pattern file even if some
error occurred.

Setting f = NULL in the success path and checking it for NULL in the
error path isn't strictly necessary at this point, but let's do it
anyway in case someone later adds a 'goto error' after closing the file.

Coverity: CID 1405303
Fixes: 4d731510d34f280ed45a6de621d016f67a49ea48
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu-io-cmds.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
index d46fa166d3..349256a5fe 100644
--- a/qemu-io-cmds.c
+++ b/qemu-io-cmds.c
@@ -401,6 +401,7 @@ static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
     }
 
     fclose(f);
+    f = NULL;
 
     if (len > pattern_len) {
         len -= pattern_len;
@@ -420,6 +421,9 @@ static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
 
 error:
     qemu_io_free(buf_origin);
+    if (f) {
+        fclose(f);
+    }
     return NULL;
 }
 
-- 
2.20.1


Re: [Qemu-devel] [PATCH] qemu-io: Don't leak pattern file in error path
Posted by Max Reitz 4 years, 7 months ago
On 10.09.19 09:09, Kevin Wolf wrote:
> qemu_io_alloc_from_file() needs to close the pattern file even if some
> error occurred.
> 
> Setting f = NULL in the success path and checking it for NULL in the
> error path isn't strictly necessary at this point, but let's do it
> anyway in case someone later adds a 'goto error' after closing the file.
> 
> Coverity: CID 1405303
> Fixes: 4d731510d34f280ed45a6de621d016f67a49ea48
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  qemu-io-cmds.c | 4 ++++
>  1 file changed, 4 insertions(+)

Reviewed-by: Max Reitz <mreitz@redhat.com>

Re: [Qemu-devel] [Qemu-block] [PATCH] qemu-io: Don't leak pattern file in error path
Posted by Stefano Garzarella 4 years, 7 months ago
On Tue, Sep 10, 2019 at 09:09:49AM +0200, Kevin Wolf wrote:
> qemu_io_alloc_from_file() needs to close the pattern file even if some
> error occurred.
> 
> Setting f = NULL in the success path and checking it for NULL in the
> error path isn't strictly necessary at this point, but let's do it
> anyway in case someone later adds a 'goto error' after closing the file.
> 
> Coverity: CID 1405303
> Fixes: 4d731510d34f280ed45a6de621d016f67a49ea48
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  qemu-io-cmds.c | 4 ++++
>  1 file changed, 4 insertions(+)

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>