On Fri, 2021-11-12 at 12:59 -0800, Bruce Korb wrote:
> 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:

It's free (buf), not free (cwd).  buf won't point to a static string.

buf may be NULL though, but free (NULL) is legal (no-op).


> > if (VERY_UNLIKELY (access (pz_curr_file, R_OK) != 0)) abort()

Perhaps just 

if (access (pz_curr_file, R_OK) != 0))
  {
    /* Some really inexplicable error happens. */
    fprintf (stderr, "Cannot access %s: %s",
             pz_curr_file, xstrerror (errno));
    abort();
  }

It will show which file can't be accessed so it's possible to diagnose.
And the working directory will be outputed by "make" when the fixincl
command fails anyway, so we don't need to really care it.

> 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;
> >      }
> >  
>  

-- 
Xi Ruoyao <xry...@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University

Reply via email to