From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31283 invoked by alias); 8 Apr 2013 17:57:26 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 31274 invoked by uid 89); 8 Apr 2013 17:57:26 -0000 X-Spam-SWARE-Status: No, score=-8.9 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Mon, 08 Apr 2013 17:57:26 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r38HvOx3023208 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 8 Apr 2013 13:57:25 -0400 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r38HvNKn021347; Mon, 8 Apr 2013 13:57:23 -0400 Message-ID: <51630502.3050109@redhat.com> Date: Tue, 09 Apr 2013 07:15:00 -0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130311 Thunderbird/17.0.4 MIME-Version: 1.0 To: gdb-patches@sourceware.org CC: Jan Kratochvil Subject: [PATCH] Avoid potencially-stale errno usage (was: Re: Include putpkt in TRY_CATCH. PR gdb/15275) References: <20130325195832.GA15218@host2.jankratochvil.net> <515478BE.3030801@redhat.com> In-Reply-To: <515478BE.3030801@redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-SW-Source: 2013-04/txt/msg00200.txt.bz2 On 03/28/2013 05:07 PM, Pedro Alves wrote: > ( > There's a latent bug in the patch that that throw_perror_with_name > call assumes error is still set to the right error, although > remote_unpush_target is called in between. I'm copying this > from preexisting code, and we can fix both at once.) > > if (serial_write (remote_desc, str, len)) > { > remote_unpush_target (); > throw_perror_with_name (TARGET_CLOSE_ERROR, > ) Fixing this before it ends up forgotten and bites us at some point. Comments? ------------ Avoid potencially-stale errno usage. The current throw_perror_with_name/TARGET_CLOSE_ERROR calls assume errno is still set to the right error, although remote_unpush_target is called in between, which may well change errno. Tested on x86_64 Fedora 17 w/ gdbserver. gdb/ 2013-04-08 Pedro Alves * remote.c (unpush_and_perror): New function. (readchar, remote_serial_write): Use it. --- gdb/remote.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index 740324b..e60ff78 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -7033,6 +7033,23 @@ remote_files_info (struct target_ops *ignore) /* Stuff for dealing with the packets which are part of this protocol. See comment at top of file for details. */ +/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR + error to higher layers. Called when a serial error is detected. + The exception message is STRING, followed by a colon and a blank, + then the system error message for errno at function entry. */ + +static void +unpush_and_perror (const char *string) +{ + char *errstr; + + errstr = xstrprintf ("%s: %s", string, safe_strerror (errno)); + make_cleanup (xfree, errstr); + + remote_unpush_target (); + throw_error (TARGET_CLOSE_ERROR, "%s", errstr); +} + /* Read a single character from the remote end. */ static int @@ -7048,14 +7065,11 @@ readchar (int timeout) switch ((enum serial_rc) ch) { case SERIAL_EOF: - remote_unpush_target (); - throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed")); + unpush_and_perror (_("Remote connection closed")); /* no return */ case SERIAL_ERROR: - remote_unpush_target (); - throw_perror_with_name (TARGET_CLOSE_ERROR, - _("Remote communication error. " - "Target disconnected.")); + unpush_and_perror (_("Remote communication error. " + "Target disconnected.")); /* no return */ case SERIAL_TIMEOUT: break; @@ -7071,10 +7085,8 @@ remote_serial_write (const char *str, int len) { if (serial_write (remote_desc, str, len)) { - remote_unpush_target (); - throw_perror_with_name (TARGET_CLOSE_ERROR, - _("Remote communication error. " - "Target disconnected.")); + unpush_and_perror (_("Remote communication error. " + "Target disconnected.")); } }