From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7506 invoked by alias); 5 Dec 2012 23:46:32 -0000 Received: (qmail 7497 invoked by uid 22791); 5 Dec 2012 23:46:31 -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.156) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 05 Dec 2012 23:46:27 +0000 Received: from md14.u-strasbg.fr (md14.u-strasbg.fr [130.79.200.249]) by mailhost.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qB5NkMUo098959 ; Thu, 6 Dec 2012 00:46:22 +0100 (CET) (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from mailserver.u-strasbg.fr (ms16.u-strasbg.fr [130.79.204.116]) by md14.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qB5NkLaQ032693 ; Thu, 6 Dec 2012 00:46:22 +0100 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 qB5NkK4d015073 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) ; Thu, 6 Dec 2012 00:46:21 +0100 (envelope-from pierre.muller@ics-cnrs.unistra.fr) From: "Pierre Muller" To: "'Tom Tromey'" Cc: References: <1043.9041689553$1354727529@news.gmane.org> <87obi8s6pg.fsf@fleche.redhat.com> In-Reply-To: <87obi8s6pg.fsf@fleche.redhat.com> Subject: RE: [RFA] Avoid memory leaks in set_language function. Date: Wed, 05 Dec 2012 23:46:00 -0000 Message-ID: <004101cdd342$bb32e6f0$3198b4d0$@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/msg00095.txt.bz2 > -----Message d'origine----- > De=A0: gdb-patches-owner@sourceware.org [mailto:gdb-patches- > owner@sourceware.org] De la part de Tom Tromey > Envoy=E9=A0: mercredi 5 d=E9cembre 2012 19:19 > =C0=A0: Pierre Muller > Cc=A0: gdb-patches@sourceware.org > Objet=A0: Re: [RFA] Avoid memory leaks in set_language function. >=20 > >>>>> "Pierre" =3D=3D Pierre Muller writes: >=20 > Pierre> This also revealed a few memory leaks > Pierre> related to repeated add_language function calls. >=20 > Wow, add_language is gross. I had no idea. >=20 > Pierre> + if (iter->doc && iter->flags & DOC_ALLOCATED) >=20 > I think parens around the second clause here. > Actually I tend to prefer the wordier >=20 > (iter->flags & DOC_ALLOCATED) !=3D 0 >=20 > Pierre> #define MALLOCED_REPLACEMENT 0x4 > Pierre> - > Pierre> +#define DOC_ALLOCATED 0x8 > Pierre> struct cmd_list_element >=20 > Please preserve the blank line after the block of #defines. >=20 > Ok with those changes. >=20 > Tom Patch committed with your changes, see below. Thanks for the approval. Pierre 2012-12-05 Pierre Muller Avoid memory leaks on struct cmd_list_element.doc field. * cli/cli-decode.c (add_alias_cmd): Make a copy of doc field if flags contains DOC_ALLOCATED. (add_setshow_cmd_full): Add DOC_ALLOCATED to set and show flags. (delete_cmd): Handle DOC_ALLOCATED flag. * cli/cli-decode.h (DOC_ALLOCATED): New macro for use in flags filed of struct cmd_list_element. (struct cmd_list_element): Document new flag item. Index: cli/cli-decode.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/cli/cli-decode.c,v retrieving revision 1.110 diff -u -p -r1.110 cli-decode.c --- cli/cli-decode.c 20 Nov 2012 21:02:35 -0000 1.110 +++ cli/cli-decode.c 5 Dec 2012 23:21:11 -0000 @@ -306,6 +306,13 @@ add_alias_cmd (char *name, char *oldname } c =3D add_cmd (name, class, NULL, old->doc, list); + + /* If OLD->DOC can be freed, we should make another copy. */ + if ((old->flags & DOC_ALLOCATED) !=3D 0) + { + c->doc =3D xstrdup (old->doc); + c->flags |=3D DOC_ALLOCATED; + } /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */ c->func =3D old->func; c->function =3D old->function; @@ -451,6 +458,8 @@ add_setshow_cmd_full (char *name, } set =3D add_set_or_show_cmd (name, set_cmd, class, var_type, var, full_set_doc, set_list); + set->flags |=3D DOC_ALLOCATED; + if (set_func !=3D NULL) set_cmd_sfunc (set, set_func); @@ -458,6 +467,7 @@ add_setshow_cmd_full (char *name, show =3D add_set_or_show_cmd (name, show_cmd, class, var_type, var, full_show_doc, show_list); + show->flags |=3D DOC_ALLOCATED; show->show_value_func =3D show_func; if (set_result !=3D NULL) @@ -769,6 +779,8 @@ delete_cmd (char *name, struct cmd_list_ *prehookee =3D iter->hookee_pre; if (iter->hookee_post) iter->hookee_post->hook_post =3D 0; + if (iter->doc && (iter->flags & DOC_ALLOCATED) !=3D 0) + xfree (iter->doc); *posthook =3D iter->hook_post; *posthookee =3D iter->hookee_post; Index: cli/cli-decode.h =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/cli/cli-decode.h,v retrieving revision 1.43 diff -u -p -r1.43 cli-decode.h --- cli/cli-decode.h 9 Aug 2012 12:53:43 -0000 1.43 +++ cli/cli-decode.h 5 Dec 2012 23:21:11 -0000 @@ -51,6 +51,7 @@ cmd_types; #define CMD_DEPRECATED 0x1 #define DEPRECATED_WARN_USER 0x2 #define MALLOCED_REPLACEMENT 0x4 +#define DOC_ALLOCATED 0x8 struct cmd_list_element { @@ -112,7 +113,9 @@ struct cmd_list_element memory for replacement is malloc'ed. When a command is undeprecated or re-deprecated at runtime we don't want to risk calling free on statically allocated memory, so we check this - flag. */ + flag. + + bit 3: DOC_ALLOCATED, set if the doc field should be xfree'd. */ int flags;