public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "Karsten Blees via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Karsten Blees <karsten.blees@gmail.com>
Subject: [PATCH v2 3/5] strbuf_readlink(): avoid calling `readlink()` twice in corner-cases
Date: Fri, 09 Jan 2026 20:05:07 +0000	[thread overview]
Message-ID: <7fe463d68aa58fd563053ee1cb87b2a8c1152957.1767989109.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2017.v2.git.1767989109.gitgitgadget@gmail.com>

From: Karsten Blees <karsten.blees@gmail.com>

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>
---
 strbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 6c3851a7f8..44a8f6a554 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -578,12 +578,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;
 		}
-- 
gitgitgadget


  parent reply	other threads:[~2026-01-09 20:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-16 15:33 [PATCH 0/5] Last preparations before upstreaming Git for Windows' symlink support Johannes Schindelin via GitGitGadget
2025-12-16 15:33 ` [PATCH 1/5] mingw: do resolve symlinks in `getcwd()` Johannes Schindelin via GitGitGadget
2025-12-17 14:44   ` Patrick Steinhardt
2025-12-16 15:33 ` [PATCH 2/5] init: do parse _all_ core.* settings early Johannes Schindelin via GitGitGadget
2025-12-17 14:44   ` Patrick Steinhardt
2025-12-16 15:33 ` [PATCH 3/5] strbuf_readlink(): avoid calling `readlink()` twice in corner-cases Karsten Blees via GitGitGadget
2025-12-16 15:33 ` [PATCH 4/5] strbuf_readlink(): support link targets that exceed PATH_MAX Karsten Blees via GitGitGadget
2025-12-17 14:44   ` Patrick Steinhardt
2025-12-19  8:50     ` Johannes Schindelin
2025-12-19 11:51       ` Patrick Steinhardt
2025-12-30  5:00         ` Junio C Hamano
2025-12-17 23:39   ` Junio C Hamano
2025-12-16 15:33 ` [PATCH 5/5] trim_last_path_component(): avoid hard-coding the directory separator Karsten Blees via GitGitGadget
2026-01-09 20:05 ` [PATCH v2 0/5] Last preparations before upstreaming Git for Windows' symlink support Johannes Schindelin via GitGitGadget
2026-01-09 20:05   ` [PATCH v2 1/5] mingw: do resolve symlinks in `getcwd()` Johannes Schindelin via GitGitGadget
2026-01-09 20:05   ` [PATCH v2 2/5] init: do parse _all_ core.* settings early Johannes Schindelin via GitGitGadget
2026-01-09 20:05   ` Karsten Blees via GitGitGadget [this message]
2026-01-09 20:05   ` [PATCH v2 4/5] strbuf_readlink(): support link targets that exceed 2*PATH_MAX Johannes Schindelin via GitGitGadget
2026-01-09 20:05   ` [PATCH v2 5/5] trim_last_path_component(): avoid hard-coding the directory separator Karsten Blees via GitGitGadget
2026-01-11  4:04   ` [PATCH v2 0/5] Last preparations before upstreaming Git for Windows' symlink support Junio C Hamano
2026-01-12  8:35   ` Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7fe463d68aa58fd563053ee1cb87b2a8c1152957.1767989109.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=karsten.blees@gmail.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox