From patchwork Tue Oct 27 13:11:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 9397 Received: (qmail 41214 invoked by alias); 27 Oct 2015 13:11:23 -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 41200 invoked by uid 89); 27 Oct 2015 13:11:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 27 Oct 2015 13:11:21 +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 (Postfix) with ESMTPS id 3AF3E6A; Tue, 27 Oct 2015 13:11:20 +0000 (UTC) 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 t9RDBIdY000739; Tue, 27 Oct 2015 09:11:19 -0400 Message-ID: <562F77F6.60503@redhat.com> Date: Tue, 27 Oct 2015 13:11:18 +0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Doug Evans CC: gdb-patches@sourceware.org Subject: Re: [PATCH] [PR symtab/17911] Recognize bad file types References: <560AA051.6060803@redhat.com> In-Reply-To: On 10/26/2015 11:44 PM, Doug Evans wrote: > Pedro Alves writes: >> Seems like there are function calls after these that may >> clobber errno. I think it'd be safer to do the usual >> save_errno = errno; / errno = save_errno; dance. > > Does what you're saying apply to the current source base? > [I think so, just checking. > And if so, then let's fix that first.] > Like so. From 0becb0a09f6a18e4edfe35bf088a0dd41e949796 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Tue, 27 Oct 2015 12:54:22 +0000 Subject: [PATCH] source.c:openp: save/restore errno openp's return is documented as: ~~~ If a file is found, return the descriptor. Otherwise, return -1, with errno set for the last name we tried to open. */ ~~~ By inspection, I noticed that there are function calls after the ones that first set errno, and those may clobber errno. It's safer to save errno when see an open fail, and restore it on exit. Tested on x86_64 Fedora 20. gdb/ChangeLog: 2015-10-27 Pedro Alves * source.c (openp): New local 'last_errno'. Use it to save/restore errno. --- gdb/source.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gdb/source.c b/gdb/source.c index 3e5e15c..194b044 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -746,6 +746,9 @@ openp (const char *path, int opts, const char *string, struct cleanup *back_to; int ix; char *dir; + /* The errno set for the last name we tried to open (and + failed). */ + int last_errno = 0; /* The open syscall MODE parameter is not specified. */ gdb_assert ((mode & O_CREAT) == 0); @@ -786,6 +789,7 @@ openp (const char *path, int opts, const char *string, filename = NULL; fd = -1; } + last_errno = errno; if (!(opts & OPF_SEARCH_IN_PATH)) for (i = 0; string[i]; i++) @@ -808,6 +812,7 @@ openp (const char *path, int opts, const char *string, alloclen = strlen (path) + strlen (string) + 2; filename = (char *) alloca (alloclen); fd = -1; + last_errno = ENOENT; dir_vec = dirnames_to_char_ptr_vec (path); back_to = make_cleanup_free_char_ptr_vec (dir_vec); @@ -878,6 +883,7 @@ openp (const char *path, int opts, const char *string, fd = gdb_open_cloexec (filename, mode, 0); if (fd >= 0) break; + last_errno = errno; } } @@ -895,6 +901,7 @@ done: *filename_opened = gdb_abspath (filename); } + errno = last_errno; return fd; }