public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] fixincludes: don't assume getcwd() can handle NULL argument
@ 2021-11-09 13:49 Xi Ruoyao
  2021-11-10  0:02 ` Joseph Myers
  2021-11-11 13:04 ` Eric Gallager
  0 siblings, 2 replies; 12+ messages in thread
From: Xi Ruoyao @ 2021-11-09 13:49 UTC (permalink / raw)
  To: gcc-patches; +Cc: Bruce Korb

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]

And, at least we've been leaking memory even if getcwd() supports this
non-standard extension.

fixincludes/ChangeLog:

	* fixincl.c (process): Allocate and deallocate the buffer for
	  getcwd() explicitly.
---
 fixincludes/fixincl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fixincludes/fixincl.c b/fixincludes/fixincl.c
index 6dba2f6e830..b4b1e38ede7 100644
--- a/fixincludes/fixincl.c
+++ b/fixincludes/fixincl.c
@@ -1353,9 +1353,11 @@ process (void)
   if (access (pz_curr_file, R_OK) != 0)
     {
       int erno = errno;
+      char *buf = xmalloc (MAXPATHLEN);
       fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n",
-               pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN),
+               pz_curr_file, getcwd (buf, MAXPATHLEN),
                erno, xstrerror (erno));
+      free (buf);
       return;
     }
 
-- 
2.33.1



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-11-13 18:37 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-09 13:49 [PATCH] fixincludes: don't assume getcwd() can handle NULL argument Xi Ruoyao
2021-11-10  0:02 ` Joseph Myers
2021-11-10 12:22   ` Xi Ruoyao
2021-11-11  0:51     ` Bruce Korb
2021-11-11 13:04 ` Eric Gallager
2021-11-11 16:33   ` [PATCH] fixincludes: fix portability issues about getcwd() [PR21283, PR80047] Xi Ruoyao
2021-11-12 20:59     ` Bruce Korb
2021-11-12 21:08       ` Xi Ruoyao
2021-11-12 21:58         ` [PATCH] fixincludes: simplify handling for access() failure " Xi Ruoyao
2021-11-13 16:13           ` Bruce Korb
2021-11-13 18:37             ` committed: " Xi Ruoyao
2021-11-11 16:40   ` [PATCH] fixincludes: don't assume getcwd() can handle NULL argument Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).