From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23071 invoked by alias); 5 Dec 2012 17:11:52 -0000 Received: (qmail 23061 invoked by uid 22791); 5 Dec 2012 17:11:50 -0000 X-SWARE-Spam-Status: No, hits=-1.0 required=5.0 tests=AWL,BAYES_00,MSGID_MULTIPLE_AT X-Spam-Check-By: sourceware.org Received: from mailhost.u-strasbg.fr (HELO mailhost.u-strasbg.fr) (130.79.200.157) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 05 Dec 2012 17:11:20 +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 qB5HBD80039024 for ; Wed, 5 Dec 2012 18:11:13 +0100 (CET) (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from mailserver.u-strasbg.fr (ms17.u-strasbg.fr [130.79.204.117]) by md14.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qB5HBCYO015457 for ; Wed, 5 Dec 2012 18:11:12 +0100 Received: from E6510Muller (gw-ics.u-strasbg.fr [130.79.210.225]) (user=mullerp mech=LOGIN) by mailserver.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id qB5HBCHR026234 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Wed, 5 Dec 2012 18:11:12 +0100 (envelope-from pierre.muller@ics-cnrs.unistra.fr) From: "Pierre Muller" To: Subject: [RFA] Avoid memory leaks in set_language function. Date: Wed, 05 Dec 2012 17:11:00 -0000 Message-ID: <000301cdd30b$876a6410$963f2c30$@muller@ics-cnrs.unistra.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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/msg00078.txt.bz2 With the troubles that I have with my patch, I started looking into a memory debugger... I finally started to use drmemory for mingw compiled GDB executables. This also revealed a few memory leaks related to repeated add_language function calls. Error #76: LEAK 34 direct bytes 0x0370aa58-0x0370aa7a + 0 indirect bytes # 0 xmalloc [../../puresrc/gdb/common/common-utils.c:50] # 1 xstrdup [../../puresrc/libiberty/xstrdup.c:34] # 2 add_setshow_cmd_full [../../puresrc/gdb/cli/cli-decode.c:450] # 3 add_setshow_enum_cmd [../../puresrc/gdb/cli/cli-decode.c:489] # 4 add_language [../../puresrc/gdb/language.c:588] # 5 _initialize_m2_language [../../puresrc/gdb/m2-lang.c:446] # 6 initialize_all_files [e:\cygwin-1.7\usr\local\src\gdbcvs\mingw32-puresrc-dwarf\gdb/init.c:212] # 7 gdb_init [../../puresrc/gdb/top.c:1707] # 8 captured_main [../../puresrc/gdb/main.c:731] # 9 catch_errors [../../puresrc/gdb/exceptions.c:546] #10 gdb_main [../../puresrc/gdb/main.c:1041] #11 main [../../puresrc/gdb/gdb.c:34] The patch below fixes that memory leaks by xfree'ing doc fields if flags contains a new flag called DOC_ALLOCATED. This flag is set in add_setshom_cmd_full and sometimes also in add_alias_cmd. Pierre Muller GDB pascal language maintainer 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: src/gdb/cli/cli-decode.c =================================================================== RCS file: /cvs/src/src/gdb/cli/cli-decode.c,v retrieving revision 1.110 diff -u -p -r1.110 cli-decode.c --- src/gdb/cli/cli-decode.c 20 Nov 2012 21:02:35 -0000 1.110 +++ src/gdb/cli/cli-decode.c 5 Dec 2012 17:02:25 -0000 @@ -306,6 +306,13 @@ add_alias_cmd (char *name, char *oldname } c = add_cmd (name, class, NULL, old->doc, list); + + /* If OLD->DOC can be freed, we should make another copy. */ + if (old->flags & DOC_ALLOCATED) + { + c->doc = xstrdup (old->doc); + c->flags |= DOC_ALLOCATED; + } /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */ c->func = old->func; c->function = old->function; @@ -451,6 +458,8 @@ add_setshow_cmd_full (char *name, } set = add_set_or_show_cmd (name, set_cmd, class, var_type, var, full_set_doc, set_list); + set->flags |= DOC_ALLOCATED; + if (set_func != NULL) set_cmd_sfunc (set, set_func); @@ -458,6 +467,7 @@ add_setshow_cmd_full (char *name, show = add_set_or_show_cmd (name, show_cmd, class, var_type, var, full_show_doc, show_list); + show->flags |= DOC_ALLOCATED; show->show_value_func = show_func; if (set_result != NULL) @@ -769,6 +779,8 @@ delete_cmd (char *name, struct cmd_list_ *prehookee = iter->hookee_pre; if (iter->hookee_post) iter->hookee_post->hook_post = 0; + if (iter->doc && iter->flags & DOC_ALLOCATED) + xfree (iter->doc); *posthook = iter->hook_post; *posthookee = iter->hookee_post; Index: src/gdb/cli/cli-decode.h =================================================================== RCS file: /cvs/src/src/gdb/cli/cli-decode.h,v retrieving revision 1.43 diff -u -p -r1.43 cli-decode.h --- src/gdb/cli/cli-decode.h 9 Aug 2012 12:53:43 -0000 1.43 +++ src/gdb/cli/cli-decode.h 5 Dec 2012 17:02:25 -0000 @@ -51,7 +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 { /* Points to next command in this list. */ @@ -112,7 +112,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;