diff mbox series

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

Message ID 20171009225623.29232-33-marcandre.lureau@redhat.com
State New
Headers show
Series TPM: code cleanup & CRB device | expand

Commit Message

Marc-André Lureau Oct. 9, 2017, 10:56 p.m. UTC
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(-)

Comments

Stefan Berger Oct. 10, 2017, 6:13 p.m. UTC | #1
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);
>   }
>
diff mbox series

Patch

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);
 }