From patchwork Fri Nov 12 21:58:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 47579 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 95F103858430 for ; Fri, 12 Nov 2021 21:59:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 95F103858430 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636754341; bh=Xaibo7tsQogDl+910vJfhXmuZ0NKDvTfVlwr0/hM968=; h=Subject:To:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=DsdNU8gctNCnSjn680siPn9DZk9cEKgY8+x77uM92uQ5IwicZyedOxzrdNGzJRnFv C4Y4QcT3W3SnF0BAr99UvjUnYUio2O+7retGCwSk9LgOnwVuN3G/ypKoPn+GkgKRnZ rxs5ZCmtRyhiTwPAGSod/v6NnrR3Q8T0VR4OyEp4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mengyan1223.wang (mengyan1223.wang [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 5C3B83858012 for ; Fri, 12 Nov 2021 21:58:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5C3B83858012 Received: from [IPv6:240e:35a:103f:9400:dc73:854d:832e:2] (unknown [IPv6:240e:35a:103f:9400:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384)) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id 3D3B46603E; Fri, 12 Nov 2021 16:58:12 -0500 (EST) Message-ID: <5304be9808e0346f6f8e2bc76279451dd17d82be.camel@mengyan1223.wang> Subject: [PATCH] fixincludes: simplify handling for access() failure [PR21283, PR80047] To: Bruce Korb , gcc-patches Date: Sat, 13 Nov 2021 05:58:07 +0800 In-Reply-To: References: <109aefbeac593ab5660a71df38f1727002c19e39.camel@mengyan1223.wang> <14343662168642b2d975044fccf5e235695bedc7.camel@mengyan1223.wang> <3053902e-315b-fc4f-f2f1-fea78a630947@gnu.org> User-Agent: Evolution 3.42.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3038.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" POSIX says: On some implementations, if buf is a null pointer, getcwd() may obtain size bytes of memory using malloc(). In this case, the pointer returned by getcwd() may be used as the argument in a subsequent call to free(). Invoking getcwd() with buf as a null pointer is not recommended in conforming applications. This produces an error building GCC with --enable-werror-always: ../../../fixincludes/fixincl.c: In function ‘process’: ../../../fixincludes/fixincl.c:1356:7: error: argument 1 is null but the corresponding size argument 2 value is 4096 [-Werror=nonnull] It's suggested by POSIX to call getcwd() with progressively larger buffers until it does not give an [ERANGE] error. However, it's highly unlikely that this error-handling route is ever used. So we can simplify it instead of writting too much code. We give up to use getcwd(), because `make` will output a `Leaving directory ...` message containing the path to cwd when we call abort(). fixincludes/ChangeLog: PR other/21823 PR bootstrap/80047 * fixincl.c (process): Simplify the handling for highly unlikely access() failure, to avoid using non-standard extensions. --- fixincludes/fixincl.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fixincludes/fixincl.c b/fixincludes/fixincl.c index 6dba2f6e830..ee57fbf61b4 100644 --- a/fixincludes/fixincl.c +++ b/fixincludes/fixincl.c @@ -1352,11 +1352,10 @@ process (void) if (access (pz_curr_file, R_OK) != 0) { - int erno = errno; - fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n", - pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN), - erno, xstrerror (erno)); - return; + /* Some really strange error happened. */ + fprintf (stderr, "Cannot access %s: %s\n", pz_curr_file, + xstrerror (errno)); + abort(); } pz_curr_data = load_file (pz_curr_file);