[PATCH] convert bare readlink to strbuf_readlink

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Jeff King
Date: Monday, May 25, 2009 - 3:46 am

This particular readlink call never NUL-terminated its
result, making it a potential source of bugs (though there
is no bug now, as it currently always respects the length
field). Let's just switch it to strbuf_readlink which is
shorter and less error-prone.

Signed-off-by: Jeff King <peff@peff.net>
---
This is a re-post with the missing strbuf_release added.

I am not overly married to this patch, so if you think it is code churn,
please just say so and drop it. I am just clearing out my "clean up and
submit" queue. :)

 diff.c |   11 ++++-------
 1 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/diff.c b/diff.c
index f06876b..dcfbcb0 100644
--- a/diff.c
+++ b/diff.c
@@ -2014,18 +2014,15 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
 			die("stat(%s): %s", name, strerror(errno));
 		}
 		if (S_ISLNK(st.st_mode)) {
-			int ret;
-			char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
-			ret = readlink(name, buf, sizeof(buf));
-			if (ret < 0)
+			struct strbuf sb = STRBUF_INIT;
+			if (strbuf_readlink(&sb, name, st.st_size) < 0)
 				die("readlink(%s)", name);
-			if (ret == sizeof(buf))
-				die("symlink too long: %s", name);
-			prep_temp_blob(name, temp, buf, ret,
+			prep_temp_blob(name, temp, sb.buf, sb.len,
 				       (one->sha1_valid ?
 					one->sha1 : null_sha1),
 				       (one->sha1_valid ?
 					one->mode : S_IFLNK));
+			strbuf_release(&sb);
 		}
 		else {
 			/* we can borrow from the file in the work tree */
-- 
1.6.3.1.250.g01b8b.dirty
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] setup_revisions(): do not access outside argv, =?utf-8?q?Nguy=E1=BB ..., (Wed May 20, 1:08 am)
Re: [PATCH] setup_revisions(): do not access outside argv, Johannes Sixt, (Wed May 20, 1:15 am)
Re: [PATCH] setup_revisions(): do not access outside argv, Nguyen Thai Ngoc Duy, (Wed May 20, 1:23 am)
Re: [PATCH] setup_revisions(): do not access outside argv, Junio C Hamano, (Wed May 20, 6:58 pm)
Re: [PATCH] setup_revisions(): do not access outside argv, Nguyen Thai Ngoc Duy, (Wed May 20, 7:41 pm)
Re: [PATCH] setup_revisions(): do not access outside argv, Thomas Jarosch, (Thu May 21, 11:02 am)
Re: [PATCH] setup_revisions(): do not access outside argv, Thomas Jarosch, (Fri May 22, 7:23 am)
Re: [PATCH] setup_revisions(): do not access outside argv, Brandon Casey, (Fri May 22, 8:33 am)
[PATCH] convert bare readlink to strbuf_readlink, Jeff King, (Mon May 25, 3:46 am)
Re: [PATCH] convert bare readlink to strbuf_readlink, Junio C Hamano, (Mon May 25, 3:23 pm)