This is the mail archive of the gdb-patches@sources.redhat.com 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]

[PATCH RFA] mi-main.c: Fix memory leaks and compilation problems


I ran into some more compilation problems in attempting to compile
mi-main.c with the native compiler on AIX 5.

In mi_cmd_data_write_register_values(), the fact that the xmalloc()
cast didn't match the declaration of ``buffer'' caused the compiler to
complain.  In mi_cmd_data_write_memory(), it was the declaration of
``buffer'' as an ``unsigned char *'' that gave the compiler problems
when it came to one of the later calls in which buffer was passed as a
parameter.  (unsigned char * didn't match char *.)  I changed the type
of buffer to void * and removed the cast to fix this problem.

I also noticed that the space allocated to ``buffer'' in both functions
wasn't being freed up, so I added a cleanup to handle that problem.

Okay to commit?

	* mi-main.c (mi_cmd_data_write_register_values)
	(mi_cmd_data_write_memory): Change type of ``buffer'' to
	``void *''.  Don't cast return value from xmalloc().  Add
	a cleanup to free the xmalloc'd buffer.

Index: mi/mi-main.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-main.c,v
retrieving revision 1.19
diff -u -p -r1.19 mi-main.c
--- mi-main.c	2001/06/25 21:05:11	1.19
+++ mi-main.c	2001/07/09 06:17:35
@@ -551,7 +551,6 @@ mi_cmd_data_write_register_values (char 
   int regnum;
   int i;
   int numregs;
-  char *buffer;
   LONGEST value;
   char format;
 
@@ -602,14 +601,19 @@ mi_cmd_data_write_register_values (char 
 	  && REGISTER_NAME (regnum) != NULL
 	  && *REGISTER_NAME (regnum) != '\000')
 	{
+	  void *buffer;
+	  struct cleanup *old_chain;
+
 	  /* Get the value as a number */
 	  value = parse_and_eval_address (argv[i + 1]);
 	  /* Get the value into an array */
-	  buffer = (unsigned char *) xmalloc (REGISTER_SIZE);
+	  buffer = xmalloc (REGISTER_SIZE);
+	  old_chain = make_cleanup (xfree, buffer);
 	  store_signed_integer (buffer, REGISTER_SIZE, value);
 	  /* Write it down */
 	  write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
-	  /* write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_SIZE); */
+	  /* Free the buffer.  */
+	  do_cleanups (old_chain);
 	}
       else
 	{
@@ -973,7 +977,8 @@ mi_cmd_data_write_memory (char *command,
   /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
      enough when using a compiler other than GCC. */
   LONGEST value;
-  unsigned char *buffer;
+  void *buffer;
+  struct cleanup *old_chain;
   long offset = 0;
   int optind = 0;
   char *optarg;
@@ -1025,10 +1030,13 @@ mi_cmd_data_write_memory (char *command,
   /* Get the value as a number */
   value = parse_and_eval_address (argv[3]);
   /* Get the value into an array */
-  buffer = (unsigned char *) xmalloc (word_size);
+  buffer = xmalloc (word_size);
+  old_chain = make_cleanup (xfree, buffer);
   store_signed_integer (buffer, word_size, value);
   /* Write it down to memory */
   write_memory (addr, buffer, word_size);
+  /* Free the buffer.  */
+  do_cleanups (old_chain);
 
   return MI_CMD_DONE;
 }


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