From: Junio C Hamano <gitster@pobox.com>
To: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2 2/3] path: use size_t for dir_prefix length
Date: Wed, 04 Mar 2026 09:15:03 -0800 [thread overview]
Message-ID: <xmqqwlzr8qjs.fsf@gitster.g> (raw)
In-Reply-To: <20260304130502.8475-3-jayatheerthkulkarni2005@gmail.com> (K. Jayatheerth's message of "Wed, 4 Mar 2026 18:35:01 +0530")
K Jayatheerth <jayatheerthkulkarni2005@gmail.com> writes:
> The strlen() function returns a size_t. Storing this in a standard
> signed int is a bad practice that invites overflow vulnerabilities if
> paths get absurdly long.
"a standard signed int" -> "an int variable". There is no
"nonstandard signed int" anyway ;-)
If we were doing malloc(len) using length truncated due to integer
wraparound and then strcpy() the whole string, it would make us
write beyond the end of the allocation, but in this case, the worst
thing that can happen is that we stop comparing prematurely, which
may make us declare that buf is a path inside the directory dir when
it isn't. The two callers of this function do not use this
miscalculated len to carry out what they do, so there is no other
damage. It indeed would be computing a wrong result, but "overflow
vulnerabilities" is a slight exaggeration in the context of this
patch, I think.
"overflow vulnerabilities" -> "bugs due to integer wraparound".
> Switch the variable to size_t. This is safe to do because 'len' is
> strictly used as an argument to strncmp() (which expects size_t) and
> as a positive array index, involving no signed arithmetic that could
> rely on negative values.
>
> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
> ---
> path.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/path.c b/path.c
> index f613d8bbd1..56be5e1726 100644
> --- a/path.c
> +++ b/path.c
> @@ -58,7 +58,7 @@ static void strbuf_cleanup_path(struct strbuf *sb)
>
> static int dir_prefix(const char *buf, const char *dir)
> {
> - int len = strlen(dir);
> + size_t len = strlen(dir);
> return !strncmp(buf, dir, len) &&
> (is_dir_sep(buf[len]) || buf[len] == '\0');
> }
next prev parent reply other threads:[~2026-03-04 17:15 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-02 14:21 [PATCH 0/3] path: clean up few things K Jayatheerth
2026-03-02 14:21 ` [PATCH 1/3] path: remove unused header K Jayatheerth
2026-03-02 14:21 ` [PATCH 2/3] path: use the right datatype K Jayatheerth
2026-03-03 13:42 ` Patrick Steinhardt
2026-03-03 16:21 ` Junio C Hamano
2026-03-02 14:21 ` [PATCH 3/3] path: remove redundant function calls K Jayatheerth
2026-03-03 13:42 ` Patrick Steinhardt
2026-03-03 16:39 ` Junio C Hamano
2026-03-04 13:04 ` [PATCH v2 0/3] clean up a few things K Jayatheerth
2026-03-04 13:05 ` [PATCH v2 1/3] path: remove unused header K Jayatheerth
2026-03-04 13:05 ` [PATCH v2 2/3] path: use size_t for dir_prefix length K Jayatheerth
2026-03-04 17:15 ` Junio C Hamano [this message]
2026-03-04 13:05 ` [PATCH v2 3/3] path: remove redundant function calls K Jayatheerth
2026-03-05 12:53 ` [PATCH v3 0/3] clean up a few things K Jayatheerth
2026-03-05 12:53 ` [PATCH v3 1/3] path: remove unused header K Jayatheerth
2026-03-05 12:53 ` [PATCH v3 2/3] path: use size_t for dir_prefix length K Jayatheerth
2026-03-05 12:53 ` [PATCH v3 3/3] path: remove redundant function calls K Jayatheerth
2026-03-05 19:36 ` [PATCH v3 0/3] clean up a few things Junio C Hamano
2026-03-06 1:47 ` K Jayatheerth
2026-03-06 1:59 ` K Jayatheerth
2026-03-06 21:58 ` Junio C Hamano
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=xmqqwlzr8qjs.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=jayatheerthkulkarni2005@gmail.com \
/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