* [PATCH] transport-helper.c: don't leak fdopen'd stream buffers
@ 2009-09-12 9:38 Jim Meyering
2009-09-13 2:27 ` Junio C Hamano
0 siblings, 1 reply; 4+ messages in thread
From: Jim Meyering @ 2009-09-12 9:38 UTC (permalink / raw)
To: git list
Looking in the vicinity today, I noticed that these
fdopen'd streams were never fclosed; technically a leak
of both the FILE buffer and the file descriptor.
>From aeae4edb1146b107f6a397118db8b0ac06b884d9 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@redhat.com>
Date: Sat, 12 Sep 2009 11:35:17 +0200
Subject: [PATCH] transport-helper.c: don't leak fdopen'd stream buffers
* transport-helper.c (get_helper, fetch_with_fetch, get_refs_list):
Call fclose on each just-created FILE* pointer, when done.
Signed-off-by: Jim Meyering <meyering@redhat.com>
---
transport-helper.c | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/transport-helper.c b/transport-helper.c
index f57e84c..0bbd014 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -49,6 +49,7 @@ static struct child_process *get_helper(struct transport *transport)
if (!strcmp(buf.buf, "fetch"))
data->fetch = 1;
}
+ fclose (file);
return data->helper;
}
@@ -88,6 +89,7 @@ static int fetch_with_fetch(struct transport *transport,
if (strbuf_getline(&buf, file, '\n') == EOF)
exit(128); /* child died, message supplied already */
}
+ fclose (file);
return 0;
}
@@ -147,6 +149,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
get_sha1_hex(buf.buf, (*tail)->old_sha1);
tail = &((*tail)->next);
}
+ fclose (file);
strbuf_release(&buf);
for (posn = ret; posn; posn = posn->next)
--
1.6.5.rc0.190.g15871
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] transport-helper.c: don't leak fdopen'd stream buffers
2009-09-12 9:38 [PATCH] transport-helper.c: don't leak fdopen'd stream buffers Jim Meyering
@ 2009-09-13 2:27 ` Junio C Hamano
2009-09-13 7:45 ` Jim Meyering
2009-09-13 15:20 ` Johannes Sixt
0 siblings, 2 replies; 4+ messages in thread
From: Junio C Hamano @ 2009-09-13 2:27 UTC (permalink / raw)
To: Jim Meyering; +Cc: git list, Daniel Barkalow, Johannes Sixt
Jim Meyering <jim@meyering.net> writes:
> diff --git a/transport-helper.c b/transport-helper.c
> index f57e84c..0bbd014 100644
> --- a/transport-helper.c
> +++ b/transport-helper.c
> @@ -49,6 +49,7 @@ static struct child_process *get_helper(struct transport *transport)
> if (!strcmp(buf.buf, "fetch"))
> data->fetch = 1;
> }
> + fclose (file);
> return data->helper;
> }
>
> @@ -88,6 +89,7 @@ static int fetch_with_fetch(struct transport *transport,
> if (strbuf_getline(&buf, file, '\n') == EOF)
> exit(128); /* child died, message supplied already */
> }
> + fclose (file);
> return 0;
> }
The callchain of fetch_with_fetch() looks like:
fetch_with_fetch()
helper = get_helper();
--> get_helper()
- start helper with start_command();
- read from helper->out until it sees an empty line;
- break out of the loop;
<-- return helper
- file = xfdopen(helper->out) to get another FILE on the fd
- read the rest of the output from helper->out via file
It seems to me that the fclose() in get_helper() will close the underlying
fd and would break the caller, no?
I think "struct helper_data" should get a new FILE* field and once
somebody creates a FILE* out of its helper->out, that FILE* can be passed
around without a new xfdopen().
Or something like that.
Who is responsible for closing the underlying helper->out fd in the
start_command() API, by the way?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] transport-helper.c: don't leak fdopen'd stream buffers
2009-09-13 2:27 ` Junio C Hamano
@ 2009-09-13 7:45 ` Jim Meyering
2009-09-13 15:20 ` Johannes Sixt
1 sibling, 0 replies; 4+ messages in thread
From: Jim Meyering @ 2009-09-13 7:45 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git list, Daniel Barkalow, Johannes Sixt
Junio C Hamano wrote:
> Jim Meyering <jim@meyering.net> writes:
>
>> diff --git a/transport-helper.c b/transport-helper.c
>> index f57e84c..0bbd014 100644
>> --- a/transport-helper.c
>> +++ b/transport-helper.c
>> @@ -49,6 +49,7 @@ static struct child_process *get_helper(struct transport *transport)
>> if (!strcmp(buf.buf, "fetch"))
>> data->fetch = 1;
>> }
>> + fclose (file);
>> return data->helper;
>> }
>>
>> @@ -88,6 +89,7 @@ static int fetch_with_fetch(struct transport *transport,
>> if (strbuf_getline(&buf, file, '\n') == EOF)
>> exit(128); /* child died, message supplied already */
>> }
>> + fclose (file);
>> return 0;
>> }
>
> The callchain of fetch_with_fetch() looks like:
>
> fetch_with_fetch()
> helper = get_helper();
> --> get_helper()
> - start helper with start_command();
> - read from helper->out until it sees an empty line;
> - break out of the loop;
> <-- return helper
> - file = xfdopen(helper->out) to get another FILE on the fd
> - read the rest of the output from helper->out via file
>
> It seems to me that the fclose() in get_helper() will close the underlying
> fd and would break the caller, no?
I confess that my sole test was to run "make test", which passed.
If the fd must live on, a slightly less invasive change would be to
xdup each descriptor just before each of the three xfdopen calls, e.g.,
- file = xfdopen(helper->out, "r");
+ file = xfdopen(xdup(helper->out), "r");
> I think "struct helper_data" should get a new FILE* field and once
> somebody creates a FILE* out of its helper->out, that FILE* can be passed
> around without a new xfdopen().
>
> Or something like that.
That's probably best.
> Who is responsible for closing the underlying helper->out fd in the
> start_command() API, by the way?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] transport-helper.c: don't leak fdopen'd stream buffers
2009-09-13 2:27 ` Junio C Hamano
2009-09-13 7:45 ` Jim Meyering
@ 2009-09-13 15:20 ` Johannes Sixt
1 sibling, 0 replies; 4+ messages in thread
From: Johannes Sixt @ 2009-09-13 15:20 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Jim Meyering, git list, Daniel Barkalow
On Sonntag, 13. September 2009, Junio C Hamano wrote:
> Jim Meyering <jim@meyering.net> writes:
> > diff --git a/transport-helper.c b/transport-helper.c
> > index f57e84c..0bbd014 100644
> > --- a/transport-helper.c
> > +++ b/transport-helper.c
> > @@ -49,6 +49,7 @@ static struct child_process *get_helper(struct
> > transport *transport) if (!strcmp(buf.buf, "fetch"))
> > data->fetch = 1;
> > }
> > + fclose (file);
> > return data->helper;
> > }
> >
> > @@ -88,6 +89,7 @@ static int fetch_with_fetch(struct transport
> > *transport, if (strbuf_getline(&buf, file, '\n') == EOF)
> > exit(128); /* child died, message supplied already */
> > }
> > + fclose (file);
> > return 0;
> > }
>
> The callchain of fetch_with_fetch() looks like:
>
> fetch_with_fetch()
> helper = get_helper();
> --> get_helper()
> - start helper with start_command();
> - read from helper->out until it sees an empty line;
> - break out of the loop;
> <-- return helper
> - file = xfdopen(helper->out) to get another FILE on the fd
> - read the rest of the output from helper->out via file
>
> It seems to me that the fclose() in get_helper() will close the underlying
> fd and would break the caller, no?
>
> I think "struct helper_data" should get a new FILE* field and once
> somebody creates a FILE* out of its helper->out, that FILE* can be passed
> around without a new xfdopen().
>
> Or something like that.
>
> Who is responsible for closing the underlying helper->out fd in the
> start_command() API, by the way?
A pipe was requested by setting helper->out = -1 before the call to
start_command(), and in such a case the caller must close the fd.
-- Hannes
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-09-13 15:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-12 9:38 [PATCH] transport-helper.c: don't leak fdopen'd stream buffers Jim Meyering
2009-09-13 2:27 ` Junio C Hamano
2009-09-13 7:45 ` Jim Meyering
2009-09-13 15:20 ` Johannes Sixt
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.