convert bare readlink to strbuf_readlink
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
May 25, 2009 at 06:46 UTC
3cd7388d57db4f4a29949e8de96493fb77059484
1 file changed
+4
-7
diff.c
+4
-7
@@ -2014,18 +2014,15 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
2014
die("stat(%s): %s", name, strerror(errno));
2015
}
2016
if (S_ISLNK(st.st_mode)) {
2017
- int ret;
2018
- char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
2019
- ret = readlink(name, buf, sizeof(buf));
2020
- if (ret < 0)
2017
+ struct strbuf sb = STRBUF_INIT;
2018
+ if (strbuf_readlink(&sb, name, st.st_size) < 0)
2019
die("readlink(%s)", name);
2022
- if (ret == sizeof(buf))
2023
- die("symlink too long: %s", name);
2024
- prep_temp_blob(name, temp, buf, ret,
2020
+ prep_temp_blob(name, temp, sb.buf, sb.len,
2021
(one->sha1_valid ?
2022
one->sha1 : null_sha1),
2023
(one->sha1_valid ?
2024
one->mode : S_IFLNK));
2025
+ strbuf_release(&sb);
2026
}
2027
else {
2028
/* we can borrow from the file in the work tree */