This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
[PATCH] Fix memory leak in solib-svr4.c
- From: Kevin Buettner <kevinb at redhat dot com>
- To: gdb-patches at sources dot redhat dot com
- Date: Thu, 11 Mar 2004 10:36:43 -0700
- Subject: [PATCH] Fix memory leak in solib-svr4.c
- Organization: Red Hat
I've just committed the patch below.
It fixes a memory leak in solib-svr4.c. In enable_break(), the memory
allocated by svr4_current_sos() was never being freed.
I could have iterated over the list returned by this function and
freed each entry, but, instead, I chose to eliminate the call to
svr4_current_sos() altogether. It appears to me that the call to
svr4_current_sos() was being used for two purposes: 1) to determine
whether or not to call solib_add(), and 2) as a means for finding
the dynamic linker's base address. solib_add() makes its own call
to svr4_current_sos(), which means that svr4_current_sos() was
being invoked an extra time for no good reason (that I can see).
The code has been reorganized to instead use the master list
operated on by solib_add().
Kevin
* solist.h (master_so_list): New function.
* solib.c (master_so_list): Likewise.
* solib-svr4.c (enable_break): Iterate over so_list entries
obtained from master list instead of entries obtained directly
via svr4_current_sos().
Index: solib-svr4.c
===================================================================
RCS file: /cvs/src/src/gdb/solib-svr4.c,v
retrieving revision 1.41
diff -c -p -r1.41 solib-svr4.c
*** solib-svr4.c 21 Feb 2004 18:34:45 -0000 1.41
--- solib-svr4.c 11 Mar 2004 16:57:35 -0000
*************** enable_break (void)
*** 1004,1010 ****
char *buf;
CORE_ADDR load_addr = 0;
int load_addr_found = 0;
! struct so_list *inferior_sos;
bfd *tmp_bfd = NULL;
struct target_ops *tmp_bfd_target;
int tmp_fd = -1;
--- 1004,1010 ----
char *buf;
CORE_ADDR load_addr = 0;
int load_addr_found = 0;
! struct so_list *so;
bfd *tmp_bfd = NULL;
struct target_ops *tmp_bfd_target;
int tmp_fd = -1;
*************** enable_break (void)
*** 1047,1069 ****
target will also close the underlying bfd. */
tmp_bfd_target = target_bfd_reopen (tmp_bfd);
! /* If the entry in _DYNAMIC for the dynamic linker has already
! been filled in, we can read its base address from there. */
! inferior_sos = svr4_current_sos ();
! if (inferior_sos)
{
! /* Connected to a running target. Update our shared library table. */
! solib_add (NULL, 0, NULL, auto_solib_add);
! }
! while (inferior_sos)
! {
! if (strcmp (buf, inferior_sos->so_original_name) == 0)
{
load_addr_found = 1;
! load_addr = LM_ADDR (inferior_sos);
break;
}
! inferior_sos = inferior_sos->next;
}
/* Otherwise we find the dynamic linker's base address by examining
--- 1047,1065 ----
target will also close the underlying bfd. */
tmp_bfd_target = target_bfd_reopen (tmp_bfd);
! /* On a running target, we can get the dynamic linker's base
! address from the shared library table. */
! solib_add (NULL, 0, NULL, auto_solib_add);
! so = master_so_list ();
! while (so)
{
! if (strcmp (buf, so->so_original_name) == 0)
{
load_addr_found = 1;
! load_addr = LM_ADDR (so);
break;
}
! so = so->next;
}
/* Otherwise we find the dynamic linker's base address by examining
Index: solib.c
===================================================================
RCS file: /cvs/src/src/gdb/solib.c,v
retrieving revision 1.63
diff -c -p -r1.63 solib.c
*** solib.c 28 Feb 2004 18:04:37 -0000 1.63
--- solib.c 11 Mar 2004 16:57:35 -0000
*************** free_so (struct so_list *so)
*** 333,338 ****
--- 333,346 ----
}
+ /* Return address of first so_list entry in master shared object list. */
+ struct so_list *
+ master_so_list (void)
+ {
+ return so_list_head;
+ }
+
+
/* A small stub to get us past the arg-passing pinhole of catch_errors. */
static int
Index: solist.h
===================================================================
RCS file: /cvs/src/src/gdb/solist.h,v
retrieving revision 1.8
diff -c -p -r1.8 solist.h
*** solist.h 24 Feb 2003 19:11:04 -0000 1.8
--- solist.h 11 Mar 2004 16:57:35 -0000
*************** struct target_so_ops
*** 107,113 ****
--- 107,117 ----
};
+ /* Free the memory associated with a (so_list *). */
void free_so (struct so_list *so);
+
+ /* Return address of first so_list entry in master shared object list. */
+ struct so_list *master_so_list (void);
/* Find solib binary file and open it. */
extern int solib_open (char *in_pathname, char **found_pathname);