2009-06-26 18:57:38

by Eric Sandeen

[permalink] [raw]
Subject: [PATCH] debugfs: don't try to fclose NULL

do_logdump may jump to errout if fopen(out_file) fails,
but in that case out_file is NULL, and fclose will segfault.

Signed-off-by: Eric Sandeen <[email protected]>
---

diff --git a/debugfs/logdump.c b/debugfs/logdump.c
index 4818bc6..9a7108a 100644
--- a/debugfs/logdump.c
+++ b/debugfs/logdump.c
@@ -259,7 +259,7 @@ void do_logdump(int argc, char **argv)
close(journal_fd);

errout:
- if (out_file != stdout)
+ if (out_file && (out_file != stdout))
fclose(out_file);

return;



2009-06-29 05:08:31

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH] debugfs: don't try to fclose NULL

On Fri, Jun 26, 2009 at 01:57:39PM -0500, Eric Sandeen wrote:
> do_logdump may jump to errout if fopen(out_file) fails,
> but in that case out_file is NULL, and fclose will segfault.
>
> Signed-off-by: Eric Sandeen <[email protected]>

Thanks, applied.


- Ted

2009-06-30 15:44:23

by Thierry Vignaud

[permalink] [raw]
Subject: Re: [PATCH] debugfs: don't try to fclose NULL

Eric Sandeen <[email protected]> writes:

> do_logdump may jump to errout if fopen(out_file) fails,
> but in that case out_file is NULL, and fclose will segfault.

You should report that segfault to glibc authors too.

2009-06-30 16:39:56

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH] debugfs: don't try to fclose NULL

On Tue, Jun 30, 2009 at 05:44:55PM +0200, Thierry Vignaud wrote:
> Eric Sandeen <[email protected]> writes:
>
> > do_logdump may jump to errout if fopen(out_file) fails,
> > but in that case out_file is NULL, and fclose will segfault.
>
> You should report that segfault to glibc authors too.

It would be robust for glibc to code fclose() defensively such that it
would survive a null pointer, but I don't think it's technically
required by the ANSI or POSIX spec.

- Ted

2009-06-30 17:06:39

by Alex Buell

[permalink] [raw]
Subject: Re: [PATCH] debugfs: don't try to fclose NULL

On Tue, 2009-06-30 at 12:39 -0400, Theodore Tso wrote:
> On Tue, Jun 30, 2009 at 05:44:55PM +0200, Thierry Vignaud wrote:
> > Eric Sandeen <[email protected]> writes:
> >
> > > do_logdump may jump to errout if fopen(out_file) fails,
> > > but in that case out_file is NULL, and fclose will segfault.
> >
> > You should report that segfault to glibc authors too.
>
> It would be robust for glibc to code fclose() defensively such that it
> would survive a null pointer, but I don't think it's technically
> required by the ANSI or POSIX spec.

It's the programmer's responsibility to write the following:

if (fp)
fclose(fp);

--
http://www.munted.org.uk

One very high maintenance cat living here.