From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
To: "'Tom Tromey'" <tromey@redhat.com>
Cc: <gdb-patches@sourceware.org>
Subject: RE: [RFA] Avoid memory leaks in set_language function.
Date: Wed, 05 Dec 2012 23:46:00 -0000 [thread overview]
Message-ID: <004101cdd342$bb32e6f0$3198b4d0$@muller@ics-cnrs.unistra.fr> (raw)
In-Reply-To: <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;
next prev parent reply other threads:[~2012-12-05 23:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1043.9041689553$1354727529@news.gmane.org>
2012-12-05 18:19 ` Tom Tromey
2012-12-05 23:46 ` Pierre Muller [this message]
2012-12-05 17:11 Pierre Muller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='004101cdd342$bb32e6f0$3198b4d0$@muller@ics-cnrs.unistra.fr' \
--to=pierre.muller@ics-cnrs.unistra.fr \
--cc=gdb-patches@sourceware.org \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox