From patchwork Fri Sep 14 08:06:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyrill Tkachov X-Patchwork-Id: 969644 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-485637-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=foss.arm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="YDLUfrwA"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42BSlR2wzqz9s1c for ; Fri, 14 Sep 2018 18:06:25 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; q= dns; s=default; b=VC/px84O9BqyOwE4FaVf03iegKlA3uUcJXy05nJ3iRwuJu pIn7mv6FK3aFvXAtgbR+rx0E91qHq08wOJhsQIJ2et39QsAtGvqOO8OzsIzCDVu7 FWtIvKX0FsjoPWainrCGh8PQFtH7E4qpSgFJ8jXFbcsAUIUyfUi8Gj1GszOkw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; s= default; bh=+5plv92edYBlITIqEtLAsn+e+KY=; b=YDLUfrwA1/cbMIxPmpE4 at53rYGQatiSTr5m8iA5uoBVs6wBa95qi5rPMGF+IbTx7h/LZU24bccJr+dBE1wB NgxDPfP6m53pFXtHJwWgr10F8ua42eQSXTapVugOVPGuXhKQae01l0L9zEPbliDt 9XOHzJ/7oeAcvpQbipbhJ/8= Received: (qmail 91861 invoked by alias); 14 Sep 2018 08:06:13 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 91160 invoked by uid 89); 14 Sep 2018 08:06:12 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY autolearn=ham version=3.3.2 spammy=disappears, H*UA:31.0, o_rdonly, O_RDONLY X-HELO: foss.arm.com Received: from usa-sjc-mx-foss1.foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 14 Sep 2018 08:06:11 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 27C757A9; Fri, 14 Sep 2018 01:06:10 -0700 (PDT) Received: from [10.2.207.77] (e100706-lin.cambridge.arm.com [10.2.207.77]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A68133F557; Fri, 14 Sep 2018 01:06:09 -0700 (PDT) Message-ID: <5B9B6BF0.5010108@foss.arm.com> Date: Fri, 14 Sep 2018 09:06:08 +0100 From: Kyrill Tkachov User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" , "fortran@gcc.gnu.org" Subject: [PATCH][libgfortran] Fix uninitialized variable use in fallback_access Hi all, I've been tracking down a bug in a Fortran program on a newlib target and it boils down to fallback_access doing something bad. The unconditional calls to close cause havoc when open doesn't get called due to the short-circuiting in the if-statement above because the fd is uninitialised. In my environment GCC ends up calling close on file descriptor 0, thus trying to close stdin. This patch tightens up the calling so that close is called only when the corresponding open call succeeded. With this my runtime failure disappears. Bootstrapped and tested on aarch64-none-linux-gnu. Though that doesn't exercise this call I hope it's an obviously correct change. Ok for trunk and the branches? Thanks, Kyrill 2018-09-14 Kyrylo Tkachov * io/unix.c (fallback_access): Avoid calling close on uninitialized file descriptor. diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index 61e9f7997b25819514af546b50aa1d00b1d116f9..8081d6f96b2f6cd2489e876c8921cfb3510287ca 100644 --- a/libgfortran/io/unix.c +++ b/libgfortran/io/unix.c @@ -149,13 +149,21 @@ fallback_access (const char *path, int mode) { int fd; - if ((mode & R_OK) && (fd = open (path, O_RDONLY)) < 0) - return -1; - close (fd); + if (mode & R_OK) + { + if ((fd = open (path, O_RDONLY)) < 0) + return -1; + else + close (fd); + } - if ((mode & W_OK) && (fd = open (path, O_WRONLY)) < 0) - return -1; - close (fd); + if (mode & W_OK) + { + if ((fd = open (path, O_WRONLY)) < 0) + return -1; + else + close (fd); + } if (mode == F_OK) {