]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
* cli/cli-cmds.c (apropos_command): Free the compiled regex. Use
authorTom Tromey <tromey@redhat.com>
Mon, 17 Jan 2011 16:50:42 +0000 (16:50 +0000)
committerTom Tromey <tromey@redhat.com>
Mon, 17 Jan 2011 16:50:42 +0000 (16:50 +0000)
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.

gdb/ChangeLog
gdb/cli/cli-cmds.c
gdb/gdb_regex.h
gdb/utils.c

index 5a814c4d8b71df9b016580cbf926958b62d5a903..136bb1d8ee2f7273529c320ca15dc4588af1554c 100644 (file)
@@ -1,3 +1,13 @@
+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.
+
 2011-01-17  Tom Tromey  <tromey@redhat.com>
 
        * cli/cli-cmds.c (apropos_command): Fix formatting.  Don't call
index e1d8174f6ad5075740369a35fe2d4902f377fe0f..c11b257eeaeaaa78a26ac799ab50d02d48d71952 100644 (file)
@@ -1254,18 +1254,26 @@ void
 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);
     }
 }
 \f
index ccdf9c360b9b4903d7802feccebf366de2c772e5..835cb09694c296daee6268524234034589975ce4 100644 (file)
@@ -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 c23c4d490e71d00d8836478885e030704d72299e..82e3adbafb7e2c01fb5d6c9178e0a5f5042a975a 100644 (file)
@@ -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 @@ gdb_print_host_address (const void *addr, struct ui_file *stream)
 }
 \f
 
+/* 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;
+}
+
+\f
+
 /* 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