Skip to content

Commit

Permalink
strbuf_readlink: don't call readlink twice if hint is the exact link …
Browse files Browse the repository at this point in the history
…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 <blees@dcon.de>
  • Loading branch information
kblees authored and dscho committed Sep 24, 2024
1 parent a77944a commit f2c265b
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
while (hint < STRBUF_MAXLINK) {
ssize_t len;

strbuf_grow(sb, hint);
len = readlink(path, sb->buf, hint);
strbuf_grow(sb, hint + 1);
len = readlink(path, sb->buf, hint + 1);
if (len < 0) {
if (errno != ERANGE)
break;
} else if (len < hint) {
} else if (len <= hint) {
strbuf_setlen(sb, len);
return 0;
}
Expand Down

0 comments on commit f2c265b

Please sign in to comment.