This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: fix memory leak in apropos_command


I'm checking this in.

I happened to notice a memory leak in apropos_command -- it never called
regfree.  This patch fixes the problem.  It also makes the
error-reporting code more robust.

Built and regtested on x86-64 (compile farm).

Tom

2011-01-17  Tom Tromey  <tromey@redhat.com>

	* cli/cli-cmds.c (apropos_command): Free the compiled regex.  Use
	get_regcomp_error.
	* utils.c: Include gdb_regex.h.
	(do_regfree_cleanup): New function.
	(make_regfree_cleanup): Likewise.
	(get_regcomp_error): Likewise.
	* gdb_regex.h (make_regfree_cleanup, get_regcomp_error): Declare.

Index: gdb_regex.h
===================================================================
RCS file: /cvs/src/src/gdb/gdb_regex.h,v
retrieving revision 1.10
diff -u -r1.10 gdb_regex.h
--- gdb_regex.h	1 Jan 2011 15:33:05 -0000	1.10
+++ gdb_regex.h	17 Jan 2011 16:47:13 -0000
@@ -28,4 +28,8 @@
 # include <regex.h>
 #endif
 
+/* From utils.c.  */
+struct cleanup *make_regfree_cleanup (regex_t *);
+char *get_regcomp_error (int, regex_t *);
+
 #endif /* not GDB_REGEX_H */
Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.244
diff -u -r1.244 utils.c
--- utils.c	12 Jan 2011 01:23:28 -0000	1.244
+++ utils.c	17 Jan 2011 16:47:13 -0000
@@ -73,6 +73,7 @@
 
 #include "gdb_usleep.h"
 #include "interps.h"
+#include "gdb_regex.h"
 
 #if !HAVE_DECL_MALLOC
 extern PTR malloc ();		/* ARI: PTR */
@@ -1643,6 +1644,37 @@
 }
 
 
+/* A cleanup function that calls regfree.  */
+
+static void
+do_regfree_cleanup (void *r)
+{
+  regfree (r);
+}
+
+/* Create a new cleanup that frees the compiled regular expression R.  */
+
+struct cleanup *
+make_regfree_cleanup (regex_t *r)
+{
+  return make_cleanup (do_regfree_cleanup, r);
+}
+
+/* Return an xmalloc'd error message resulting from a regular
+   expression compilation failure.  */
+
+char *
+get_regcomp_error (int code, regex_t *rx)
+{
+  size_t length = regerror (code, rx, NULL, 0);
+  char *result = xmalloc (length);
+
+  regerror (code, rx, result, length);
+  return result;
+}
+
+
+
 /* This function supports the query, nquery, and yquery functions.
    Ask user a y-or-n question and return 0 if answer is no, 1 if
    answer is yes, or default the answer to the specified default
Index: cli/cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.109
diff -u -r1.109 cli-cmds.c
--- cli/cli-cmds.c	17 Jan 2011 16:20:56 -0000	1.109
+++ cli/cli-cmds.c	17 Jan 2011 16:47:13 -0000
@@ -1254,18 +1254,26 @@
 apropos_command (char *searchstr, int from_tty)
 {
   regex_t pattern;
-  char errorbuffer[512];
+  int code;
 
   if (searchstr == NULL)
     error (_("REGEXP string is empty"));
 
-  if (regcomp (&pattern, searchstr, REG_ICASE) == 0)
-    apropos_cmd (gdb_stdout, cmdlist, &pattern, "");
+  code = regcomp (&pattern, searchstr, REG_ICASE);
+  if (code == 0)
+    {
+      struct cleanup *cleanups;
+
+      cleanups = make_regfree_cleanup (&pattern);
+      apropos_cmd (gdb_stdout, cmdlist, &pattern, "");
+      do_cleanups (cleanups);
+    }
   else
     {
-      regerror (regcomp (&pattern, searchstr, REG_ICASE), NULL,
-		errorbuffer, 512);
-      error (_("Error in regular expression: %s"), errorbuffer);
+      char *err = get_regcomp_error (code, &pattern);
+
+      make_cleanup (xfree, err);
+      error (_("Error in regular expression: %s"), err);
     }
 }
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]