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]

[commit] Make the length argument of ui_file_xstrdup optional.


It has bothered me that most callers of ui_file_xstdup don't have
a real use for the LENGTH output argument, and hence
end up having to provide a dummy local variable to satisfy the
interface.  (raise your hand if you have been bothered by this too.)
Worse, several callers actually do an strlen call on the returned
buffer, when they could very well make use of the length as returned
by ui_file_xstrdup.  I was just adding yet another length
dummy, and that got me passed the tipping point.  The patch below
makes the length argument of ui_file_xstrdup optional, and goes
through all the callers.

Built with --enable-targets=all, tested on x86_64-unknown-linux-gnu,
and checked in.

-- 
Pedro Alves

2009-08-14  Pedro Alves  <pedro@codesourcery.com>

	* ui-file.h (ui_file_xstrdup): Mention that the length argument
	may be NULL.
	* ui-file.c (ui_file_xstrdup): Don't dereference LENGTH if it is
	NULL.
	* aix-thread.c (aix_thread_extra_thread_info): Pass NULL as length
	parameter to ui_file_xstrdup.
	* arm-tdep.c (_initialize_arm_tdep): Ditto.
	* infrun.c (print_target_wait_results): Ditto.
	* language.c (add_language): Ditto.
	* linespec.c (cplusplus_error): Ditto.
	* remote.c (escape_buffer): Ditto.
	* typeprint.c (type_to_string): Ditto.
	* utils.c (error_stream): Ditto.
	* varobj.c (value_get_print_value): Ditto.
	* xtensa-tdep.c (xtensa_verify_config): Replace `dummy' local with
	`length' local.  Pass it to ui_file_xstrdup, and avoid an strlen
	call.
	* gdbarch.sh (verify_gdbarch): Ditto.
	* gdbarch.c: Regenerate.
	* cli/cli-setshow.c (do_setshow_command): Pass NULL as length
	parameter to ui_file_xstrdup.
	* python/python-frame.c (frapy_str): Ditto.
	* python/python-type.c (typy_str): Use the length local instead of
	calling strlen.
	* python/python-value.c (valpy_str): Pass NULL as length parameter
	to ui_file_xstrdup.

---
 gdb/aix-thread.c          |    3 +--
 gdb/arm-tdep.c            |    2 +-
 gdb/cli/cli-setshow.c     |    3 +--
 gdb/gdbarch.c             |    6 +++---
 gdb/gdbarch.sh            |    6 +++---
 gdb/infrun.c              |    3 +--
 gdb/language.c            |    3 +--
 gdb/linespec.c            |    7 +++----
 gdb/python/python-frame.c |    3 +--
 gdb/python/python-type.c  |    4 ++--
 gdb/python/python-value.c |    3 +--
 gdb/remote.c              |    3 +--
 gdb/typeprint.c           |    3 +--
 gdb/ui-file.c             |    6 +++---
 gdb/ui-file.h             |    4 ++--
 gdb/utils.c               |    3 +--
 gdb/varobj.c              |    3 +--
 gdb/xtensa-tdep.c         |    6 +++---
 18 files changed, 30 insertions(+), 41 deletions(-)

Index: src/gdb/ui-file.h
===================================================================
--- src.orig/gdb/ui-file.h	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/ui-file.h	2009-08-14 01:13:54.000000000 +0100
@@ -73,8 +73,8 @@ extern void ui_file_put (struct ui_file 
 
 /* Returns a freshly allocated buffer containing the entire contents
    of FILE (as determined by ui_file_put()) with a NUL character
-   appended.  LENGTH is set to the size of the buffer minus that
-   appended NUL. */
+   appended.  LENGTH, if not NULL, is set to the size of the buffer
+   minus that appended NUL. */
 extern char *ui_file_xstrdup (struct ui_file *file, long *length);
 
 
Index: src/gdb/ui-file.c
===================================================================
--- src.orig/gdb/ui-file.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/ui-file.c	2009-08-14 01:13:54.000000000 +0100
@@ -285,8 +285,7 @@ do_ui_file_xstrdup (void *context, const
 }
 
 char *
-ui_file_xstrdup (struct ui_file *file,
-		  long *length)
+ui_file_xstrdup (struct ui_file *file, long *length)
 {
   struct accumulated_ui_file acc;
   acc.buffer = NULL;
@@ -294,7 +293,8 @@ ui_file_xstrdup (struct ui_file *file,
   ui_file_put (file, do_ui_file_xstrdup, &acc);
   if (acc.buffer == NULL)
     acc.buffer = xstrdup ("");
-  *length = acc.length;
+  if (length != NULL)
+    *length = acc.length;
   return acc.buffer;
 }
 
Index: src/gdb/aix-thread.c
===================================================================
--- src.orig/gdb/aix-thread.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/aix-thread.c	2009-08-14 01:13:54.000000000 +0100
@@ -1734,7 +1734,6 @@ aix_thread_extra_thread_info (struct thr
   pthdb_suspendstate_t suspendstate;
   pthdb_detachstate_t detachstate;
   int cancelpend;
-  long length;
   static char *ret = NULL;
 
   if (!PD_TID (thread->ptid))
@@ -1775,7 +1774,7 @@ aix_thread_extra_thread_info (struct thr
 
   xfree (ret);			/* Free old buffer.  */
 
-  ret = ui_file_xstrdup (buf, &length);
+  ret = ui_file_xstrdup (buf, NULL);
   ui_file_delete (buf);
 
   return ret;
Index: src/gdb/arm-tdep.c
===================================================================
--- src.orig/gdb/arm-tdep.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/arm-tdep.c	2009-08-14 01:13:54.000000000 +0100
@@ -6081,7 +6081,7 @@ _initialize_arm_tdep (void)
 		      _("The valid values are:\n"),
 		      regdesc,
 		      _("The default is \"std\"."));
-  helptext = ui_file_xstrdup (stb, &length);
+  helptext = ui_file_xstrdup (stb, NULL);
   ui_file_delete (stb);
 
   add_setshow_enum_cmd("disassembler", no_class,
Index: src/gdb/infrun.c
===================================================================
--- src.orig/gdb/infrun.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/infrun.c	2009-08-14 01:13:54.000000000 +0100
@@ -1943,7 +1943,6 @@ print_target_wait_results (ptid_t waiton
   char *status_string = target_waitstatus_to_string (ws);
   struct ui_file *tmp_stream = mem_fileopen ();
   char *text;
-  long len;
 
   /* The text is split over several lines because it was getting too long.
      Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
@@ -1963,7 +1962,7 @@ print_target_wait_results (ptid_t waiton
 		      "infrun:   %s\n",
 		      status_string);
 
-  text = ui_file_xstrdup (tmp_stream, &len);
+  text = ui_file_xstrdup (tmp_stream, NULL);
 
   /* This uses %s in part to handle %'s in the text, but also to avoid
      a gcc error: the format attribute requires a string literal.  */
Index: src/gdb/language.c
===================================================================
--- src.orig/gdb/language.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/language.c	2009-08-14 01:13:54.000000000 +0100
@@ -888,7 +888,6 @@ add_language (const struct language_defn
 
   int i;
   struct ui_file *tmp_stream;
-  long len;
 
   if (lang->la_magic != LANG_MAGIC)
     {
@@ -945,7 +944,7 @@ local or auto    Automatic setting based
     }
 
   xfree (language_set_doc);
-  language_set_doc = ui_file_xstrdup (tmp_stream, &len);
+  language_set_doc = ui_file_xstrdup (tmp_stream, NULL);
   ui_file_delete (tmp_stream);
 
   add_setshow_enum_cmd ("language", class_support,
Index: src/gdb/linespec.c
===================================================================
--- src.orig/gdb/linespec.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/linespec.c	2009-08-14 01:13:54.000000000 +0100
@@ -150,7 +150,6 @@ static NORETURN void
 cplusplus_error (const char *name, const char *fmt, ...)
 {
   struct ui_file *tmp_stream;
-  long len;
   char *message;
   tmp_stream = mem_fileopen ();
   make_cleanup_ui_file_delete (tmp_stream);
@@ -169,9 +168,9 @@ cplusplus_error (const char *name, const
 		       "(Note leading single quote.)"),
 		      name, name);
 
-  message = ui_file_xstrdup (tmp_stream, &len);                                   
-  make_cleanup (xfree, message);                                              
-  throw_error (NOT_FOUND_ERROR, "%s", message);  
+  message = ui_file_xstrdup (tmp_stream, NULL);
+  make_cleanup (xfree, message);
+  throw_error (NOT_FOUND_ERROR, "%s", message);
 }
 
 /* Return the number of methods described for TYPE, including the
Index: src/gdb/remote.c
===================================================================
--- src.orig/gdb/remote.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/remote.c	2009-08-14 01:13:54.000000000 +0100
@@ -5947,13 +5947,12 @@ escape_buffer (const char *buf, int n)
   struct cleanup *old_chain;
   struct ui_file *stb;
   char *str;
-  long length;
 
   stb = mem_fileopen ();
   old_chain = make_cleanup_ui_file_delete (stb);
 
   fputstrn_unfiltered (buf, n, 0, stb);
-  str = ui_file_xstrdup (stb, &length);
+  str = ui_file_xstrdup (stb, NULL);
   do_cleanups (old_chain);
   return str;
 }
Index: src/gdb/typeprint.c
===================================================================
--- src.orig/gdb/typeprint.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/typeprint.c	2009-08-14 01:13:54.000000000 +0100
@@ -86,7 +86,6 @@ char *
 type_to_string (struct type *type)
 {
   char *s = NULL;
-  long dummy;
   struct ui_file *stb;
   struct cleanup *old_chain;
   volatile struct gdb_exception except;
@@ -97,7 +96,7 @@ type_to_string (struct type *type)
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
       type_print (type, "", stb, -1);
-      s = ui_file_xstrdup (stb, &dummy);
+      s = ui_file_xstrdup (stb, NULL);
     }
   if (except.reason < 0)
     s = NULL;
Index: src/gdb/utils.c
===================================================================
--- src.orig/gdb/utils.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/utils.c	2009-08-14 01:13:54.000000000 +0100
@@ -838,8 +838,7 @@ fatal (const char *string, ...)
 NORETURN void
 error_stream (struct ui_file *stream)
 {
-  long len;
-  char *message = ui_file_xstrdup (stream, &len);
+  char *message = ui_file_xstrdup (stream, NULL);
   make_cleanup (xfree, message);
   error (("%s"), message);
 }
Index: src/gdb/varobj.c
===================================================================
--- src.orig/gdb/varobj.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/varobj.c	2009-08-14 01:13:54.000000000 +0100
@@ -2175,7 +2175,6 @@ static char *
 value_get_print_value (struct value *value, enum varobj_display_formats format,
 		       struct varobj *var)
 {
-  long dummy;
   struct ui_file *stb;
   struct cleanup *old_chain;
   gdb_byte *thevalue = NULL;
@@ -2247,7 +2246,7 @@ value_get_print_value (struct value *val
     }
   else
     common_val_print (value, stb, 0, &opts, current_language);
-  thevalue = ui_file_xstrdup (stb, &dummy);
+  thevalue = ui_file_xstrdup (stb, NULL);
 
   do_cleanups (old_chain);
   return thevalue;
Index: src/gdb/xtensa-tdep.c
===================================================================
--- src.orig/gdb/xtensa-tdep.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/xtensa-tdep.c	2009-08-14 01:13:54.000000000 +0100
@@ -2498,7 +2498,7 @@ xtensa_verify_config (struct gdbarch *gd
   struct ui_file *log;
   struct cleanup *cleanups;
   struct gdbarch_tdep *tdep;
-  long dummy;
+  long length;
   char *buf;
 
   tdep = gdbarch_tdep (gdbarch);
@@ -2531,9 +2531,9 @@ xtensa_verify_config (struct gdbarch *gd
   if (tdep->a0_base == -1)
     fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
 
-  buf = ui_file_xstrdup (log, &dummy);
+  buf = ui_file_xstrdup (log, &length);
   make_cleanup (xfree, buf);
-  if (strlen (buf) > 0)
+  if (length > 0)
     internal_error (__FILE__, __LINE__,
 		    _("the following are invalid: %s"), buf);
   do_cleanups (cleanups);
Index: src/gdb/gdbarch.sh
===================================================================
--- src.orig/gdb/gdbarch.sh	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/gdbarch.sh	2009-08-14 01:13:54.000000000 +0100
@@ -1417,7 +1417,7 @@ verify_gdbarch (struct gdbarch *gdbarch)
 {
   struct ui_file *log;
   struct cleanup *cleanups;
-  long dummy;
+  long length;
   char *buf;
   log = mem_fileopen ();
   cleanups = make_cleanup_ui_file_delete (log);
@@ -1463,9 +1463,9 @@ do
     fi
 done
 cat <<EOF
-  buf = ui_file_xstrdup (log, &dummy);
+  buf = ui_file_xstrdup (log, &length);
   make_cleanup (xfree, buf);
-  if (strlen (buf) > 0)
+  if (length > 0)
     internal_error (__FILE__, __LINE__,
                     _("verify_gdbarch: the following are invalid ...%s"),
                     buf);
Index: src/gdb/gdbarch.c
===================================================================
--- src.orig/gdb/gdbarch.c	2009-08-14 01:13:38.000000000 +0100
+++ src/gdb/gdbarch.c	2009-08-14 01:13:54.000000000 +0100
@@ -507,7 +507,7 @@ verify_gdbarch (struct gdbarch *gdbarch)
 {
   struct ui_file *log;
   struct cleanup *cleanups;
-  long dummy;
+  long length;
   char *buf;
   log = mem_fileopen ();
   cleanups = make_cleanup_ui_file_delete (log);
@@ -639,9 +639,9 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of record_special_symbol, has predicate */
   /* Skip verify of has_global_solist, invalid_p == 0 */
   /* Skip verify of has_global_breakpoints, invalid_p == 0 */
-  buf = ui_file_xstrdup (log, &dummy);
+  buf = ui_file_xstrdup (log, &length);
   make_cleanup (xfree, buf);
-  if (strlen (buf) > 0)
+  if (length > 0)
     internal_error (__FILE__, __LINE__,
                     _("verify_gdbarch: the following are invalid ...%s"),
                     buf);
Index: src/gdb/cli/cli-setshow.c
===================================================================
--- src.orig/gdb/cli/cli-setshow.c	2009-08-14 01:19:51.000000000 +0100
+++ src/gdb/cli/cli-setshow.c	2009-08-14 01:20:18.000000000 +0100
@@ -383,8 +383,7 @@ do_setshow_command (char *arg, int from_
 	ui_out_field_stream (uiout, "value", stb);
       else
 	{
-	  long length;
-	  char *value = ui_file_xstrdup (stb->stream, &length);
+	  char *value = ui_file_xstrdup (stb->stream, NULL);
 	  make_cleanup (xfree, value);
 	  if (c->show_value_func != NULL)
 	    c->show_value_func (gdb_stdout, from_tty, c, value);
Index: src/gdb/python/python-frame.c
===================================================================
--- src.orig/gdb/python/python-frame.c	2009-08-14 01:17:50.000000000 +0100
+++ src/gdb/python/python-frame.c	2009-08-14 01:18:04.000000000 +0100
@@ -80,13 +80,12 @@ static PyObject *
 frapy_str (PyObject *self)
 {
   char *s;
-  long len;
   PyObject *result;
   struct ui_file *strfile;
 
   strfile = mem_fileopen ();
   fprint_frame_id (strfile, ((frame_object *) self)->frame_id);
-  s = ui_file_xstrdup (strfile, &len);
+  s = ui_file_xstrdup (strfile, NULL);
   result = PyString_FromString (s);
   xfree (s);
 
Index: src/gdb/python/python-type.c
===================================================================
--- src.orig/gdb/python/python-type.c	2009-08-14 01:18:19.000000000 +0100
+++ src/gdb/python/python-type.c	2009-08-14 01:19:25.000000000 +0100
@@ -494,13 +494,13 @@ typy_str (PyObject *self)
 {
   volatile struct gdb_exception except;
   char *thetype = NULL;
+  long length = 0;
   PyObject *result;
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
       struct cleanup *old_chain;
       struct ui_file *stb;
-      long length;
 
       stb = mem_fileopen ();
       old_chain = make_cleanup_ui_file_delete (stb);
@@ -516,7 +516,7 @@ typy_str (PyObject *self)
       GDB_PY_HANDLE_EXCEPTION (except);
     }
 
-  result = PyUnicode_Decode (thetype, strlen (thetype), host_charset (), NULL);
+  result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
   xfree (thetype);
 
   return result;
Index: src/gdb/python/python-value.c
===================================================================
--- src.orig/gdb/python/python-value.c	2009-08-14 01:17:20.000000000 +0100
+++ src/gdb/python/python-value.c	2009-08-14 01:17:42.000000000 +0100
@@ -348,7 +348,6 @@ static PyObject *
 valpy_str (PyObject *self)
 {
   char *s = NULL;
-  long dummy;
   struct ui_file *stb;
   struct cleanup *old_chain;
   PyObject *result;
@@ -365,7 +364,7 @@ valpy_str (PyObject *self)
     {
       common_val_print (((value_object *) self)->value, stb, 0,
 			&opts, python_language);
-      s = ui_file_xstrdup (stb, &dummy);
+      s = ui_file_xstrdup (stb, NULL);
     }
   GDB_PY_HANDLE_EXCEPTION (except);
 


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