Skip to content

Commit 3cd7388

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9619ff1 commit 3cd7388

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

diff.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,18 +2014,15 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
20142014
die("stat(%s): %s", name, strerror(errno));
20152015
}
20162016
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)
20212019
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,
20252021
(one->sha1_valid ?
20262022
one->sha1 : null_sha1),
20272023
(one->sha1_valid ?
20282024
one->mode : S_IFLNK));
2025+
strbuf_release(&sb);
20292026
}
20302027
else {
20312028
/* we can borrow from the file in the work tree */

0 commit comments

Comments
 (0)