Subject: Re: [PATCH 2/3] path: use the right datatype

From: Junio C Hamano <[email-masked]>
Date: Tue, 03 Mar 2026 08:21:54 -0800

K Jayatheerth <[email-masked]> 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.
>
> Signed-off-by: K Jayatheerth <[email-masked]>
> ---
>  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');
>  }

Obviously correct.

We also could tell it to return "bool" without disrupting much else,
as this is a file-scope static function that are only used inside
"if (...)" conditions without its return value stored in any
variable, if we are interested in type kosherness.

I have to wonder if it is easier to read if we used our standard
helper functions, e.g.,

        const char *tail;

        return (skip_prefix(buf, dir, &tail) &&
		(!*tail || is_dir_sep(*tail)));

but probably not.

Thanks.