Skip to content

Commit a64c53b

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 044c84a commit a64c53b

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
@@ -413,12 +413,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
413413
while (hint < STRBUF_MAXLINK) {
414414
int len;
415415

416-
strbuf_grow(sb, hint);
417-
len = readlink(path, sb->buf, hint);
416+
strbuf_grow(sb, hint + 1);
417+
len = readlink(path, sb->buf, hint + 1);
418418
if (len < 0) {
419419
if (errno != ERANGE)
420420
break;
421-
} else if (len < hint) {
421+
} else if (len <= hint) {
422422
strbuf_setlen(sb, len);
423423
return 0;
424424
}

0 commit comments

Comments
 (0)