[PATCH v2 4/5] strbuf_readlink(): support link targets that exceed 2*PATH_MAX
- From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Jan 9, 2026, 20:05 UTC
- Message-ID
- <accb6d5f0aef9ab5de6da8e9d08ad59a19ef5157.1767989109.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2017.v2.git.1767989109.gitgitgadget@gmail.com>
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
The `strbuf_readlink()` function refuses to read link targets that exceed 2*PATH_MAX (even if a sufficient size was specified by the caller).
The reason that that limit is 2*PATH_MAX instead of PATH_MAX is that the symlink targets do not need to be normalized. After running `ln -s a/../a/../a/../a/../b c`, the target of the symlink `c` will not be normalized to `b` but instead be much longer. As such, symlink targets' lengths can far exceed PATH_MAX.
They are frequently much longer than 2*PATH_MAX on Windows, which actually supports paths up to 32,767 characters, but sets PATH_MAX to 260 for backwards compatibility. For full details, see https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
Let's just hard-code the limit used by `strbuf_readlink()` to 32,767 and make it independent of the current platform's PATH_MAX.
Based-on-a-patch-by: Karsten Blees <karsten.blees@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> --- strbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/strbuf.c b/strbuf.c index 44a8f6a554..ec2b7afbe6 100644 --- a/strbuf.c +++ b/strbuf.c @@ -566,7 +566,7 @@ ssize_t strbuf_write(struct strbuf *sb, FILE *f) return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0; } -#define STRBUF_MAXLINK (2*PATH_MAX) +#define STRBUF_MAXLINK (32767) int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) {
-- gitgitgadget