From patchwork Wed Aug 14 16:48:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 34107 Received: (qmail 116965 invoked by alias); 14 Aug 2019 16:48:38 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 116787 invoked by uid 89); 14 Aug 2019 16:48:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HContent-Transfer-Encoding:8bit X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 14 Aug 2019 16:48:25 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 10B235607A; Wed, 14 Aug 2019 12:48:17 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id qESKtF4m7eKq; Wed, 14 Aug 2019 12:48:17 -0400 (EDT) Received: from murgatroyd.Home (97-122-178-82.hlrn.qwest.net [97.122.178.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id B04A35606F; Wed, 14 Aug 2019 12:48:16 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 1/2] Simplify write_inferior_memory Date: Wed, 14 Aug 2019 10:48:13 -0600 Message-Id: <20190814164814.29367-2-tromey@adacore.com> In-Reply-To: <20190814164814.29367-1-tromey@adacore.com> References: <20190814164814.29367-1-tromey@adacore.com> MIME-Version: 1.0 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-14 Tom Tromey * target.c (write_inferior_memory): Use std::vector. --- gdb/gdbserver/ChangeLog | 4 ++++ gdb/gdbserver/target.c | 22 +++++----------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c index 25f91eeba73..2587d8aa550 100644 --- a/gdb/gdbserver/target.c +++ b/gdb/gdbserver/target.c @@ -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 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. */