Skip to content

Commit 04bd9e6

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 a0ef3c5 commit 04bd9e6

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
@@ -407,12 +407,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
407407
while (hint < STRBUF_MAXLINK) {
408408
int len;
409409

410-
strbuf_grow(sb, hint);
411-
len = readlink(path, sb->buf, hint);
410+
strbuf_grow(sb, hint + 1);
411+
len = readlink(path, sb->buf, hint + 1);
412412
if (len < 0) {
413413
if (errno != ERANGE)
414414
break;
415-
} else if (len < hint) {
415+
} else if (len <= hint) {
416416
strbuf_setlen(sb, len);
417417
return 0;
418418
}

0 commit comments

Comments
 (0)