Skip to content

Commit 98cbb8f

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 3bed3e1 commit 98cbb8f

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

strbuf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
578578
while (hint < STRBUF_MAXLINK) {
579579
ssize_t len;
580580

581-
strbuf_grow(sb, hint);
582-
len = readlink(path, sb->buf, hint);
581+
strbuf_grow(sb, hint + 1);
582+
len = readlink(path, sb->buf, hint + 1);
583583
if (len < 0) {
584584
if (errno != ERANGE)
585585
break;
586-
} else if (len < hint) {
586+
} else if (len <= hint) {
587587
strbuf_setlen(sb, len);
588588
return 0;
589589
}

0 commit comments

Comments
 (0)