fixincludes: simplify handling for access() failure [PR21283, PR80047]

Message ID 5304be9808e0346f6f8e2bc76279451dd17d82be.camel@mengyan1223.wang
State New
Headers
Series fixincludes: simplify handling for access() failure [PR21283, PR80047] |

Commit Message

Xi Ruoyao Nov. 12, 2021, 9:58 p.m. UTC
  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(-)
  

Comments

Bruce Korb Nov. 13, 2021, 4:13 p.m. UTC | #1
Perfect.

On 11/12/21 1:58 PM, Xi Ruoyao wrote:
> 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);
  
Xi Ruoyao Nov. 13, 2021, 6:37 p.m. UTC | #2
On Sat, 2021-11-13 at 08:13 -0800, Bruce Korb wrote:
> Perfect.

Committed at r12-5234 with minor format fix.

> On 11/12/21 1:58 PM, Xi Ruoyao wrote:
> > 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);
  

Patch

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