This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[RFA] Avoid memory leaks in set_language function.
- From: "Pierre Muller" <pierre dot muller at ics-cnrs dot unistra dot fr>
- To: <gdb-patches at sourceware dot org>
- Date: Wed, 5 Dec 2012 18:11:13 +0100
- Subject: [RFA] Avoid memory leaks in set_language function.
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 <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: 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;