strbuf_readlink(): avoid calling `readlink()` twice in corner-cases

The `strbuf_readlink()` function 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 <karsten.blees@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jan 9, 2026 at 20:05 UTC 0fcbb57f970f318df422ca756a269651885576b9
1 file changed +3 -3
strbuf.c
+3 -3
@@ -578,12 +578,12 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
578 while (hint < STRBUF_MAXLINK) {
579 ssize_t len;
580
581 - strbuf_grow(sb, hint);
582 - len = readlink(path, sb->buf, hint);
581 + strbuf_grow(sb, hint + 1);
582 + len = readlink(path, sb->buf, hint + 1);
583 if (len < 0) {
584 if (errno != ERANGE)
585 break;
586 - } else if (len < hint) {
586 + } else if (len <= hint) {
587 strbuf_setlen(sb, len);
588 return 0;
589 }