From: Xi Ruoyao <xry111@mengyan1223.wang>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Cc: Bruce Korb <bkorb@gnu.org>, Eric Gallager <egall@gwmail.gwu.edu>
Subject: [PATCH] fixincludes: fix portability issues about getcwd() [PR21283, PR80047]
Date: Fri, 12 Nov 2021 00:33:32 +0800 [thread overview]
Message-ID: <14343662168642b2d975044fccf5e235695bedc7.camel@mengyan1223.wang> (raw)
In-Reply-To: <CAMfHzOsApOACvVJmXwUkTjtaXAO3h+v=-ekkX294zKc5p9ETaA@mail.gmail.com>
[Revised to handle PR 21283.]
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.
And, MAXPATHLEN may be not unavailable on certain platform. PATH_MAX is
POSIX, but getcwd() may produce a path with length larger than it. So it's
suggested by POSIX [1] to call getcwd() with progressively larger buffers
until it does not give an [ERANGE] error.
[1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
fixincludes/ChangeLog:
PR other/21823
PR bootstrap/80047
* fixincl.c (process): Allocate and deallocate the buffer for
getcwd() progressively.
---
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;
}
--
2.33.1
next prev parent reply other threads:[~2021-11-11 16:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Xi Ruoyao [this message]
2021-11-12 20:59 ` [PATCH] fixincludes: fix portability issues about getcwd() [PR21283, PR80047] 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=14343662168642b2d975044fccf5e235695bedc7.camel@mengyan1223.wang \
--to=xry111@mengyan1223.wang \
--cc=bkorb@gnu.org \
--cc=egall@gwmail.gwu.edu \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).