[Qemu-devel] [PATCH 32/42] tpm-passthrough: make it safer to destroy after creation

Marc-André Lureau posted 42 patches 8 years, 4 months ago
[Qemu-devel] [PATCH 32/42] tpm-passthrough: make it safer to destroy after creation
Posted by Marc-André Lureau 8 years, 4 months ago
Check fds values before closing, to avoid close(-1).

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 hw/tpm/tpm_passthrough.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c
index 7371d50739..aa9167e3c6 100644
--- a/hw/tpm/tpm_passthrough.c
+++ b/hw/tpm/tpm_passthrough.c
@@ -347,8 +347,12 @@ static void tpm_passthrough_inst_finalize(Object *obj)
 
     tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
 
-    qemu_close(tpm_pt->tpm_fd);
-    qemu_close(tpm_pt->cancel_fd);
+    if (tpm_pt->tpm_fd >= 0) {
+        qemu_close(tpm_pt->tpm_fd);
+    }
+    if (tpm_pt->cancel_fd >= 0) {
+        qemu_close(tpm_pt->cancel_fd);
+    }
     qapi_free_TPMPassthroughOptions(tpm_pt->options);
 }
 
-- 
2.14.1.146.gd35faa819


Re: [Qemu-devel] [PATCH 32/42] tpm-passthrough: make it safer to destroy after creation
Posted by Stefan Berger 8 years, 4 months ago
On 10/09/2017 06:56 PM, Marc-André Lureau wrote:
> Check fds values before closing, to avoid close(-1).
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Typicall free() can be called with a NULL pointer.  I am also fine with 
this change here,  but could we move the fd < 0 check into qemu_close?

Reviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com>

> ---
>   hw/tpm/tpm_passthrough.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c
> index 7371d50739..aa9167e3c6 100644
> --- a/hw/tpm/tpm_passthrough.c
> +++ b/hw/tpm/tpm_passthrough.c
> @@ -347,8 +347,12 @@ static void tpm_passthrough_inst_finalize(Object *obj)
>   
>       tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
>   
> -    qemu_close(tpm_pt->tpm_fd);
> -    qemu_close(tpm_pt->cancel_fd);
> +    if (tpm_pt->tpm_fd >= 0) {
> +        qemu_close(tpm_pt->tpm_fd);
> +    }
> +    if (tpm_pt->cancel_fd >= 0) {
> +        qemu_close(tpm_pt->cancel_fd);
> +    }
>       qapi_free_TPMPassthroughOptions(tpm_pt->options);
>   }
>