From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from fencepost.gnu.org (fencepost.gnu.org [IPv6:2001:470:142:3::e]) by sourceware.org (Postfix) with ESMTPS id D6CEB3858413 for ; Fri, 12 Nov 2021 20:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D6CEB3858413 Received: from c-71-198-222-86.hsd1.ca.comcast.net ([71.198.222.86]:41118 helo=[172.16.16.102]) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mlddk-0001hr-NE; Fri, 12 Nov 2021 15:59:16 -0500 Subject: Re: [PATCH] fixincludes: fix portability issues about getcwd() [PR21283, PR80047] To: Xi Ruoyao , gcc-patches References: <109aefbeac593ab5660a71df38f1727002c19e39.camel@mengyan1223.wang> <14343662168642b2d975044fccf5e235695bedc7.camel@mengyan1223.wang> From: Bruce Korb Message-ID: <3053902e-315b-fc4f-f2f1-fea78a630947@gnu.org> Date: Fri, 12 Nov 2021 12:59:14 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <14343662168642b2d975044fccf5e235695bedc7.camel@mengyan1223.wang> Content-Language: en-US X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, NICE_REPLY_A, SPF_HELO_NONE, 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 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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-List-Received-Date: Fri, 12 Nov 2021 20:59:22 -0000 If you are going to be excruciatingly, painfully correct, free() is going to be unhappy about freeing a static string in the event getcwd() fails for some inexplicable reason. I'd replace the free() + return with a call to exit. Maybe even: if (VERY_UNLIKELY (access (pz_curr_file, R_OK) != 0)) abort() On 11/11/21 8:33 AM, Xi Ruoyao wrote: > --- > fixincludes/fixincl.c | 13 +++++++++++-- > 1 file changed, 11 insertions(+), 2 deletions(-) > > diff --git a/fixincludes/fixincl.c b/fixincludes/fixincl.c > index 6dba2f6e830..1580c67efec 100644 > --- a/fixincludes/fixincl.c > +++ b/fixincludes/fixincl.c > @@ -1353,9 +1353,18 @@ process (void) > if (access (pz_curr_file, R_OK) != 0) > { > int erno = errno; > + char *buf = NULL; > + const char *cwd = NULL; > + for (size_t size = 256; !cwd; size += size) > + { > + buf = xrealloc (buf, size); > + cwd = getcwd (buf, size); > + if (!cwd && errno != ERANGE) > + cwd = "the working directory"; > + } > fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n", > - pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN), > - erno, xstrerror (erno)); > + pz_curr_file, cwd, erno, xstrerror (erno)); > + free (buf); return; > } >