Skip to content

Commit 765b496

Browse files
peffgitster
authored andcommitted
pass st.st_size as hint for strbuf_readlink()
When we initially added the strbuf_readlink() function in b11b7e1 (Add generic 'strbuf_readlink()' helper function, 2008-12-17), the point was that we generally have a _guess_ as to the correct size based on the stat information, but we can't necessarily trust it. Over the years, a few callers have grown up that simply pass in 0, even though they have the stat information. Let's have them pass in their hint for consistency (and in theory efficiency, since it may avoid an extra resize/syscall loop, but neither location is probably performance critical). Note that st.st_size is actually an off_t, so in theory we need xsize_t() here. But none of the other callsites use it, and since this is just a hint, it doesn't matter either way (if we wrap we'll simply start with a too-small hint and then eventually complain when we cannot allocate the memory). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f3e76ed commit 765b496

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

builtin/init-db.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
7373
continue;
7474
else if (S_ISLNK(st_template.st_mode)) {
7575
struct strbuf lnk = STRBUF_INIT;
76-
if (strbuf_readlink(&lnk, template_path->buf, 0) < 0)
76+
if (strbuf_readlink(&lnk, template_path->buf,
77+
st_template.st_size) < 0)
7778
die_errno(_("cannot readlink '%s'"), template_path->buf);
7879
if (symlink(lnk.buf, path->buf))
7980
die_errno(_("cannot symlink '%s' '%s'"),

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
363363
/* Follow "normalized" - ie "refs/.." symlinks by hand */
364364
if (S_ISLNK(st.st_mode)) {
365365
strbuf_reset(&sb_contents);
366-
if (strbuf_readlink(&sb_contents, path, 0) < 0) {
366+
if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
367367
if (errno == ENOENT || errno == EINVAL)
368368
/* inconsistent with lstat; retry */
369369
goto stat_ref;

0 commit comments

Comments
 (0)