Skip to content

Commit af1cd45

Browse files
kbleesdscho
authored andcommitted
strbuf_readlink: don't call readlink twice if hint is the exact link size
strbuf_readlink() calls readlink() twice if the hint argument specifies the exact size of the link target (e.g. by passing stat.st_size as returned by lstat()). This is necessary because 'readlink(..., hint) == hint' could mean that the buffer was too small. Use hint + 1 as buffer size to prevent this. Signed-off-by: Karsten Blees <[email protected]>
1 parent 7f91760 commit af1cd45

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

strbuf.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
576576
while (hint < STRBUF_MAXLINK) {
577577
ssize_t len;
578578

579-
strbuf_grow(sb, hint);
580-
len = readlink(path, sb->buf, hint);
579+
strbuf_grow(sb, hint + 1);
580+
len = readlink(path, sb->buf, hint + 1);
581581
if (len < 0) {
582582
if (errno != ERANGE)
583583
break;
584-
} else if (len < hint) {
584+
} else if (len <= hint) {
585585
strbuf_setlen(sb, len);
586586
return 0;
587587
}

0 commit comments

Comments
 (0)