This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
RE: [RFA] Avoid memory leaks in set_language function.
- From: "Pierre Muller" <pierre dot muller at ics-cnrs dot unistra dot fr>
- To: "'Tom Tromey'" <tromey at redhat dot com>
- Cc: <gdb-patches at sourceware dot org>
- Date: Thu, 6 Dec 2012 00:46:21 +0100
- Subject: RE: [RFA] Avoid memory leaks in set_language function.
- References: <1043.9041689553$1354727529@news.gmane.org> <87obi8s6pg.fsf@fleche.redhat.com>
> -----Message d'origine-----
> De?: gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Tom Tromey
> Envoyé?: mercredi 5 décembre 2012 19:19
> À?: Pierre Muller
> Cc?: gdb-patches@sourceware.org
> Objet?: Re: [RFA] Avoid memory leaks in set_language function.
>
> >>>>> "Pierre" == Pierre Muller <pierre.muller@ics-cnrs.unistra.fr>
writes:
>
> Pierre> This also revealed a few memory leaks
> Pierre> related to repeated add_language function calls.
>
> Wow, add_language is gross. I had no idea.
>
> Pierre> + if (iter->doc && iter->flags & DOC_ALLOCATED)
>
> I think parens around the second clause here.
> Actually I tend to prefer the wordier
>
> (iter->flags & DOC_ALLOCATED) != 0
>
> Pierre> #define MALLOCED_REPLACEMENT 0x4
> Pierre> -
> Pierre> +#define DOC_ALLOCATED 0x8
> Pierre> struct cmd_list_element
>
> Please preserve the blank line after the block of #defines.
>
> Ok with those changes.
>
> Tom
Patch committed with your changes, see below.
Thanks for the approval.
Pierre
2012-12-05 Pierre Muller <muller@sourceware.org>
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
===================================================================
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 = add_cmd (name, class, NULL, old->doc, list);
+
+ /* If OLD->DOC can be freed, we should make another copy. */
+ if ((old->flags & DOC_ALLOCATED) != 0)
+ {
+ 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) != 0)
+ xfree (iter->doc);
*posthook = iter->hook_post;
*posthookee = iter->hookee_post;
Index: 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
--- 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;