From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4186 invoked by alias); 14 Dec 2012 13:07:39 -0000 Received: (qmail 4163 invoked by uid 22791); 14 Dec 2012 13:07:37 -0000 X-SWARE-Spam-Status: No, hits=-1.6 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,MSGID_MULTIPLE_AT X-Spam-Check-By: sourceware.org Received: from mailhost.u-strasbg.fr (HELO mailhost.u-strasbg.fr) (130.79.200.154) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 14 Dec 2012 13:07:27 +0000 Received: from md15.u-strasbg.fr (md15.u-strasbg.fr [130.79.200.204]) by mailhost.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qBED7Lwr017455 ; Fri, 14 Dec 2012 14:07:21 +0100 (CET) (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from mailserver.u-strasbg.fr (ms18.u-strasbg.fr [130.79.204.118]) by md15.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qBED7KXm010362 ; Fri, 14 Dec 2012 14:07:20 +0100 (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from E6510Muller (lec67-4-82-230-53-140.fbx.proxad.net [82.230.53.140]) (user=mullerp mech=LOGIN) by mailserver.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qBED7IBM007602 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) ; Fri, 14 Dec 2012 14:07:19 +0100 (envelope-from pierre.muller@ics-cnrs.unistra.fr) From: "Pierre Muller" To: "'Pedro Alves'" Cc: References: <50c9b7e6.25f2440a.3810.3771SMTPIN_ADDED_BROKEN@mx.google.com> <50CA3784.2030706@redhat.com> <000301cdd9d0$0db345d0$2919d170$@muller@ics-cnrs.unistra.fr> <50CAF521.3040307@redhat.com> <000001cdd9e5$6868f6e0$393ae4a0$@muller@ics-cnrs.unistra.fr> <50CB0574.7040406@redhat.com> In-Reply-To: <50CB0574.7040406@redhat.com> Subject: [RFA-v2] Fix other memory leak in solib_target_current_sos Date: Fri, 14 Dec 2012 13:07:00 -0000 Message-ID: <003c01cdd9fb$f4b9d310$de2d7930$@muller@ics-cnrs.unistra.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-12/txt/msg00493.txt.bz2 > -----Message d'origine----- > De=A0: gdb-patches-owner@sourceware.org [mailto:gdb-patches- > owner@sourceware.org] De la part de Pedro Alves > Envoy=E9=A0: vendredi 14 d=E9cembre 2012 11:55 > =C0=A0: Pierre Muller > Cc=A0: gdb-patches@sourceware.org > Objet=A0: Re: [RFA] Fix other memory leak in solib_target_current_sos >=20 > On 12/14/2012 10:25 AM, Pierre Muller wrote: >=20 > > I found another leak, down the same line, > > solib_target_current_sos > > calls target_read_stralloc > > which as its name suggests allocates the result on the heap... > > But that string was not freed in the current code... > > > > This patch fixes that leak. >=20 > Thanks. >=20 > > 2012-12-14 Pierre Muller > > > > * solib-target.c (solib_target_current_sos): Remove 'const' > > qualifier from type of library_document local variable to be > > able to free it and avoid a memory leak. > > > > Index: src/gdb/solib-target.c > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > RCS file: /cvs/src/src/gdb/solib-target.c,v > > retrieving revision 1.24 > > diff -u -p -r1.24 solib-target.c > > --- src/gdb/solib-target.c 4 Jan 2012 08:17:11 -0000 1.24 > > +++ src/gdb/solib-target.c 14 Dec 2012 10:17:58 -0000 > > @@ -246,7 +246,7 @@ static struct so_list * > > solib_target_current_sos (void) > > { > > struct so_list *new_solib, *start =3D NULL, *last =3D NULL; > > - const char *library_document; > > + char *library_document; > > VEC(lm_info_p) *library_list; > > struct lm_info *info; > > int ix; > > @@ -261,7 +261,10 @@ solib_target_current_sos (void) > > /* Parse the list. */ > > library_list =3D solib_target_parse_libraries (library_document); > > if (library_list =3D=3D NULL) > > - return NULL; > > + { > > + xfree (library_document); > > + return NULL; > > + } > > > > /* Build a struct so_list for each entry on the list. */ > > for (ix =3D 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++) > > @@ -291,6 +294,9 @@ solib_target_current_sos (void) > > /* Free the library list, but not its members. */ > > VEC_free (lm_info_p, library_list); > > > > + /* Also free allocated library_document string. */ > > + xfree (library_document); > > + > > return start; > > } > > >=20 > Parsing XML, within solib_target_parse_libraries, may throw. Thus, a > cleanup would be better: >=20 > + old_chain =3D make_cleanup (xfree, library_document); > library_list =3D solib_target_parse_libraries (library_document); > + do_cleanups (old_chain); Of course... library_document isn't needed after that point anymore. New version of the patch below. Pierre 2012-12-14 Pierre Muller Pedro Alves * solib-target.c (solib_target_current_sos): Remove 'const' qualifier from type of library_document local variable to be able to free it and avoid a memory leak. Use cleanup chain to avoid leak even if exceptino is generated. Index: solib-target.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvs/src/src/gdb/solib-target.c,v retrieving revision 1.24 diff -u -p -r1.24 solib-target.c --- solib-target.c 4 Jan 2012 08:17:11 -0000 1.24 +++ solib-target.c 14 Dec 2012 13:03:13 -0000 @@ -246,7 +246,8 @@ static struct so_list * solib_target_current_sos (void) { struct so_list *new_solib, *start =3D NULL, *last =3D NULL; - const char *library_document; + char *library_document; + struct cleanup *old_chain; VEC(lm_info_p) *library_list; struct lm_info *info; int ix; @@ -258,8 +259,15 @@ solib_target_current_sos (void) if (library_document =3D=3D NULL) return NULL; + /* solib_target_parse_libraries may throw, so we use a cleanup. */ + old_chain =3D make_cleanup (xfree, library_document); + /* Parse the list. */ library_list =3D solib_target_parse_libraries (library_document); + + /* library_document string is not needed behind this point. */ + do_cleanups (old_chain); + if (library_list =3D=3D NULL) return NULL;