Skip to content

Commit aeeff0e

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 82a64af commit aeeff0e

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
@@ -420,12 +420,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
420420
while (hint < STRBUF_MAXLINK) {
421421
int len;
422422

423-
strbuf_grow(sb, hint);
424-
len = readlink(path, sb->buf, hint);
423+
strbuf_grow(sb, hint + 1);
424+
len = readlink(path, sb->buf, hint + 1);
425425
if (len < 0) {
426426
if (errno != ERANGE)
427427
break;
428-
} else if (len < hint) {
428+
} else if (len <= hint) {
429429
strbuf_setlen(sb, len);
430430
return 0;
431431
}

0 commit comments

Comments
 (0)