This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
RE: [RFA] Fix memory leak in windows_xfer_shared_libraries
- From: "Pierre Muller" <pierre dot muller at ics-cnrs dot unistra dot fr>
- To: <gdb-patches at sourceware dot org>
- Date: Thu, 13 Dec 2012 12:23:19 +0100
- Subject: RE: [RFA] Fix memory leak in windows_xfer_shared_libraries
- References: <008a01cdd922$971dcb00$c5596100$@muller@ics-cnrs.unistra.fr>
Whoops,
I forgot to test my patch :(
Once again, got bitten by a difference
in syntax between C and pascal...
a semicolon was missing before the else keyword...
Sorry about that,
Pierre
> -----Message d'origine-----
> De?: gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Pierre Muller
> Envoyé?: jeudi 13 décembre 2012 12:11
> À?: gdb-patches@sourceware.org
> Objet?: [RFA] Fix memory leak in windows_xfer_shared_libraries
>
> The current mechanism of getting the list of DLLs when command
> infl dll
> is given to gdb prompt,
> info_shared_library function in solib.c calls
> windows_xfer_shared_libraries in windows-nat.c
>
> using target_read_stralloc, which calls target_read_alloc_1.
>
> That function reiterates calls to target_read_partial
> until the number of transferred bytes is zero...
>
> This results even if the buffer is large enough to contain all data at
> first
> call in a second call in which the same xml answer is computed again,
> and nothing is done, because the offset correspond to the end of the
> resulting
> string.
>
> The current code has a memory leak that is fixed by the patch below.
>
> I was also wondering if it would not be better to keep the obstack in
> between the two calls, but that would probably require some static
variable
> :(
>
Fixed patch:
2012-12-13 Pierre Muller <muller@sourceware.org>
* windows-nat.c (windows_xfer_shared_libraries): Avoid
memory leak when OFFSET >= LEN_AVAIL.
Index: windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.236
diff -u -p -r1.236 windows-nat.c
--- windows-nat.c 13 Nov 2012 09:46:10 -0000 1.236
+++ windows-nat.c 13 Dec 2012 10:54:18 -0000
@@ -2411,11 +2411,11 @@ windows_xfer_shared_libraries (struct ta
buf = obstack_finish (&obstack);
len_avail = strlen (buf);
if (offset >= len_avail)
- return 0;
-
- if (len > len_avail - offset)
+ len= 0;
+ else if (len > len_avail - offset)
len = len_avail - offset;
- memcpy (readbuf, buf + offset, len);
+ if (len > 0)
+ memcpy (readbuf, buf + offset, len);
obstack_free (&obstack, NULL);
return len;