[PATCH] fixincludes: fix portability issues about getcwd() [PR21283, PR80047]
Bruce Korb
bkorb@gnu.org
Fri Nov 12 20:59:14 GMT 2021
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;
> }
>
More information about the Gcc-patches
mailing list