]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Simplify write_inferior_memory
authorTom Tromey <tromey@adacore.com>
Wed, 14 Aug 2019 15:11:28 +0000 (09:11 -0600)
committerTom Tromey <tromey@adacore.com>
Thu, 15 Aug 2019 13:42:21 +0000 (07:42 -0600)
gdbserver's write_inferior_memory uses a static variable to avoid
memory leaks, and has a comment referring to the lack of cleanups.
This patch removes this comment and the code in favor of a
straightforward use of std::vector.

gdb/gdbserver/ChangeLog
2019-08-15  Tom Tromey  <tromey@adacore.com>

* target.c (write_inferior_memory): Use std::vector.

gdb/gdbserver/ChangeLog
gdb/gdbserver/target.c

index 2dda3521ec9fbd9bc01bab4f5dd804ecd4cabf10..e4ad220a6aa4f7ee621984397124eb46877ee676 100644 (file)
@@ -1,3 +1,7 @@
+2019-08-15  Tom Tromey  <tromey@adacore.com>
+
+       * target.c (write_inferior_memory): Use std::vector.
+
 2019-08-06  Frank Ch. Eigler  <fche@redhat.com>
 
        PR build/24886
index 25f91eeba739ce0a4cce3bd8300e4fba97811b87..2587d8aa55040e66546ded8f053e9ade89ad22bb 100644 (file)
@@ -150,23 +150,11 @@ int
 write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
                       int len)
 {
-  /* Lacking cleanups, there is some potential for a memory leak if the
-     write fails and we go through error().  Make sure that no more than
-     one buffer is ever pending by making BUFFER static.  */
-  static unsigned char *buffer = 0;
-  int res;
-
-  if (buffer != NULL)
-    free (buffer);
-
-  buffer = (unsigned char *) xmalloc (len);
-  memcpy (buffer, myaddr, len);
-  check_mem_write (memaddr, buffer, myaddr, len);
-  res = (*the_target->write_memory) (memaddr, buffer, len);
-  free (buffer);
-  buffer = NULL;
-
-  return res;
+  /* Make a copy of the data because check_mem_write may need to
+     update it.  */
+  std::vector<unsigned char> buffer (myaddr, myaddr + len);
+  check_mem_write (memaddr, buffer.data (), myaddr, len);
+  return (*the_target->write_memory) (memaddr, buffer.data (), len);
 }
 
 /* See target/target.h.  */