Skip to content

Commit 81b096f

Browse files
committed
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 438f3e7 commit 81b096f

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
@@ -396,12 +396,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
396396
while (hint < STRBUF_MAXLINK) {
397397
int len;
398398

399-
strbuf_grow(sb, hint);
400-
len = readlink(path, sb->buf, hint);
399+
strbuf_grow(sb, hint + 1);
400+
len = readlink(path, sb->buf, hint + 1);
401401
if (len < 0) {
402402
if (errno != ERANGE)
403403
break;
404-
} else if (len < hint) {
404+
} else if (len <= hint) {
405405
strbuf_setlen(sb, len);
406406
return 0;
407407
}

0 commit comments

Comments
 (0)