[PATCH 1/2] wrapper.c: add xgetcwd()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



getcwd() requires the input buffer must be large enough to contain
current working directory, or it will return ERANGE. We currently
treat ERANGE critical and die out.

xgetcwd() handles ERANGE and grows the buffer if necessary. Like other
functions in x* family, it will die() if fails.

Signed-off-by: Nguyán ThÃi Ngác Duy <pclouds@xxxxxxxxx>
---
 git-compat-util.h |    2 ++
 wrapper.c         |   12 ++++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 49b50ee..e8b1398 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -433,6 +433,8 @@ extern int xmkstemp(char *template);
 extern int xmkstemp_mode(char *template, int mode);
 extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
 extern int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1);
+struct strbuf;
+extern void xgetcwd(struct strbuf *);
 
 static inline size_t xsize_t(off_t len)
 {
diff --git a/wrapper.c b/wrapper.c
index 2829000..5c8cbdf 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -217,6 +217,20 @@ int xmkstemp(char *template)
 	return fd;
 }
 
+void xgetcwd(struct strbuf *sb)
+{
+	const char *ret;
+	if (sb->alloc - sb->len < PATH_MAX)
+		strbuf_grow(sb, PATH_MAX);
+	while ((ret = getcwd(sb->buf + sb->len,
+			     sb->alloc - sb->len - 1)) == NULL &&
+	       errno == ERANGE)
+		strbuf_grow(sb, PATH_MAX);
+	if (!ret)
+		die_errno("Unable to read current working directory");
+	sb->len += strlen(sb->buf + sb->len);
+}
+
 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
 int git_mkstemp(char *path, size_t len, const char *template)
 {
-- 
1.7.4.74.g639db

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]