diff mbox

[3/3] savevm: avoid leaking popen(3) file pointer

Message ID 1369923286-22260-4-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi May 30, 2013, 2:14 p.m. UTC
I'm not sure why we check the mode only after invoking popen(3) but we
need to close the file pointer.

Spotted by Coverity.

Cc: Juan Quintela <quintela@redhat.com>
Cc: qemu-stable@nongnu.org
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Eric Blake May 30, 2013, 2:25 p.m. UTC | #1
On 05/30/2013 08:14 AM, Stefan Hajnoczi wrote:
> I'm not sure why we check the mode only after invoking popen(3) but we
> need to close the file pointer.
> 
> Spotted by Coverity.
> 
> Cc: Juan Quintela <quintela@redhat.com>
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  savevm.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/savevm.c b/savevm.c
> index 31dcce9..75cc72e 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
>  
>      if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
>          fprintf(stderr, "qemu_popen: Argument validity check failed\n");
> +        fclose(stdio_file);

You MUST use pclose() (not fclose) on any FILE obtained by popen(), to
avoid resource leaks.
Stefan Hajnoczi May 30, 2013, 7:34 p.m. UTC | #2
On Thu, May 30, 2013 at 4:25 PM, Eric Blake <eblake@redhat.com> wrote:
> On 05/30/2013 08:14 AM, Stefan Hajnoczi wrote:
>> I'm not sure why we check the mode only after invoking popen(3) but we
>> need to close the file pointer.
>>
>> Spotted by Coverity.
>>
>> Cc: Juan Quintela <quintela@redhat.com>
>> Cc: qemu-stable@nongnu.org
>> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
>> ---
>>  savevm.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/savevm.c b/savevm.c
>> index 31dcce9..75cc72e 100644
>> --- a/savevm.c
>> +++ b/savevm.c
>> @@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
>>
>>      if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
>>          fprintf(stderr, "qemu_popen: Argument validity check failed\n");
>> +        fclose(stdio_file);
>
> You MUST use pclose() (not fclose) on any FILE obtained by popen(), to
> avoid resource leaks.

Thanks, I didn't know that.  Should have checked the popen(3) man page.

Will fix.

Stefan
Michael Roth June 11, 2013, 10:59 p.m. UTC | #3
On Thu, May 30, 2013 at 04:14:46PM +0200, Stefan Hajnoczi wrote:
> I'm not sure why we check the mode only after invoking popen(3) but we
> need to close the file pointer.
> 
> Spotted by Coverity.
> 
> Cc: Juan Quintela <quintela@redhat.com>
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

Ping, looking to get this in for 1.5.1

> ---
>  savevm.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/savevm.c b/savevm.c
> index 31dcce9..75cc72e 100644
> --- a/savevm.c
> +++ b/savevm.c
> @@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
> 
>      if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
>          fprintf(stderr, "qemu_popen: Argument validity check failed\n");
> +        fclose(stdio_file);
>          return NULL;
>      }
> 
> -- 
> 1.8.1.4
> 
>
Stefan Hajnoczi June 12, 2013, 7:16 a.m. UTC | #4
On Tue, Jun 11, 2013 at 05:59:31PM -0500, mdroth wrote:
> On Thu, May 30, 2013 at 04:14:46PM +0200, Stefan Hajnoczi wrote:
> > I'm not sure why we check the mode only after invoking popen(3) but we
> > need to close the file pointer.
> > 
> > Spotted by Coverity.
> > 
> > Cc: Juan Quintela <quintela@redhat.com>
> > Cc: qemu-stable@nongnu.org
> > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> 
> Ping, looking to get this in for 1.5.1
> 
> > ---
> >  savevm.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/savevm.c b/savevm.c
> > index 31dcce9..75cc72e 100644
> > --- a/savevm.c
> > +++ b/savevm.c
> > @@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
> > 
> >      if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
> >          fprintf(stderr, "qemu_popen: Argument validity check failed\n");
> > +        fclose(stdio_file);
> >          return NULL;
> >      }

The v2 I sent had Patch 1 picked up by Paolo and Patch 2 is not yet
merged by anyone.

Stefan
diff mbox

Patch

diff --git a/savevm.c b/savevm.c
index 31dcce9..75cc72e 100644
--- a/savevm.c
+++ b/savevm.c
@@ -329,6 +329,7 @@  QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);
         return NULL;
     }