All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Zejun Zhao <jelly.zhao.42@gmail.com>
Cc: git@vger.kernel.org, gitster@pobox.com, newren@gmail.com
Subject: Re: [GSOC][PATCH v2 2/6] apply: change some variables from `int` to `size_t`
Date: Thu, 13 Feb 2025 07:08:13 +0100	[thread overview]
Message-ID: <Z62MTZXQD3Wa47Jz@pks.im> (raw)
In-Reply-To: <20250209081216.241350-3-jelly.zhao.42@gmail.com>

On Sun, Feb 09, 2025 at 08:12:12AM +0000, Zejun Zhao wrote:
> Some assigned variables are mistyped as `int`, including
> 
>   - those whose values come from a system function returning `size_t`,
> 
>   - those that are used for array indexing,
> 
>   - those that represent length/size/distance,
> 
> some of which will trigger -Wsign-comparison warnings.
> 
> Change some of them to `size_t`/`unsigned`.
> 
> Signed-off-by: Zejun Zhao <jelly.zhao.42@gmail.com>
> ---
>  apply.c | 21 +++++++++++----------
>  1 file changed, 11 insertions(+), 10 deletions(-)
> 
> diff --git a/apply.c b/apply.c
> index 831b338155..b4ae74a5fb 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1087,7 +1087,7 @@ static int gitdiff_index(struct gitdiff_data *state,
>  	 * and optional space with octal mode.
>  	 */
>  	const char *ptr, *eol;
> -	int len;
> +	size_t len;
>  	const unsigned hexsz = the_hash_algo->hexsz;
>  
>  	ptr = strchr(line, '.');

This is storing the result of `ptr - line`, which will be a positive
integer. It's later passed to `memcpy()`, which expects a `size_t`.

> @@ -2320,7 +2320,8 @@ static void update_pre_post_images(struct image *preimage,
>  {
>  	struct image fixed_preimage = IMAGE_INIT;
>  	size_t insert_pos = 0;
> -	int i, ctx, reduced;
> +	int i, reduced;
> +	size_t ctx;
>  	const char *fixed;
>  
>  	/*

`ctx` is indexing an array and counts against `preimage->line_nr`, which
is a `size_t`, too.

> @@ -2492,7 +2493,7 @@ static int match_fragment(struct apply_state *state,
>  	struct strbuf fixed = STRBUF_INIT;
>  	char *fixed_buf;
>  	size_t fixed_len;
> -	int preimage_limit;
> +	size_t preimage_limit;
>  	int ret;
>  
>  	if (preimage->line_nr + current_lno <= img->line_nr) {

This one stores `struct image::line_nr`, which is a `size_t`.

> @@ -2706,7 +2707,7 @@ static int find_pos(struct apply_state *state,
>  {
>  	int i;
>  	unsigned long backwards, forwards, current;
> -	int backwards_lno, forwards_lno, current_lno;
> +	size_t backwards_lno, forwards_lno, current_lno;
>  
>  	/*
>  	 * When running with --allow-overlap, it is possible that a hunk is

These are a bit curious, as they store `line`, which is itself an `int`
parameter. As far as I understand, the only caller is also only ever
passing a positive integer here.

> @@ -2791,7 +2792,7 @@ static int find_pos(struct apply_state *state,
>   */
>  static void update_image(struct apply_state *state,
>  			 struct image *img,
> -			 int applied_pos,
> +			 size_t applied_pos,
>  			 struct image *preimage,
>  			 struct image *postimage)
>  {
> @@ -2803,7 +2804,7 @@ static void update_image(struct apply_state *state,
>  	size_t remove_count, insert_count, applied_at = 0;
>  	size_t result_alloc;
>  	char *result;
> -	int preimage_limit;
> +	size_t preimage_limit;
>  
>  	/*
>  	 * If we are removing blank lines at the end of img,

The caller makes sure that the function only gets called  when the
parameter is a positive integer.

> @@ -4288,19 +4289,19 @@ static void summary_patch_list(struct patch *patch)
>  
>  static void patch_stats(struct apply_state *state, struct patch *patch)
>  {
> -	int lines = patch->lines_added + patch->lines_deleted;
> +	unsigned lines = patch->lines_added + patch->lines_deleted;

This one is curious again, as the type of these variables is an `int`.
This should likely be adapted in tandem if they cannot be negative.

Patrick

  reply	other threads:[~2025-02-13  6:08 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-05  1:40 [GSOC][PATCH] apply: address -Wsign-comparison warnings Zejun Zhao
2025-02-05  7:47 ` Patrick Steinhardt
2025-02-05 15:42   ` Zejun Zhao
2025-02-05 12:58 ` Junio C Hamano
2025-02-05 16:49   ` Zejun Zhao
2025-02-09  8:12 ` [GSOC][PATCH v2 0/6] " Zejun Zhao
2025-02-09  8:12   ` [GSOC][PATCH v2 1/6] apply: change fields in `apply_state` to unsigned Zejun Zhao
2025-02-13  9:51     ` Karthik Nayak
2025-02-13 18:39       ` Junio C Hamano
2025-02-09  8:12   ` [GSOC][PATCH v2 2/6] apply: change some variables from `int` to `size_t` Zejun Zhao
2025-02-13  6:08     ` Patrick Steinhardt [this message]
2025-02-16  7:12       ` [GSOC][PATCH] apply: address -Wsign-comparison warnings Zejun Zhao
2025-02-18 17:49         ` Junio C Hamano
2025-02-21  6:37           ` Zejun Zhao
2025-02-21 17:11             ` Junio C Hamano
2025-02-23 17:36               ` Zejun Zhao
2025-02-24 14:27                 ` Junio C Hamano
2025-02-25  3:24                   ` Zejun Zhao
2025-02-25 12:53                     ` Junio C Hamano
2025-02-09  8:12   ` [GSOC][PATCH v2 3/6] apply: do a typecast to eliminate warnings Zejun Zhao
2025-02-09  8:12   ` [GSOC][PATCH v2 4/6] apply: cast some ptrdiff_t's to size_t's Zejun Zhao
2025-02-09  8:12   ` [GSOC][PATCH v2 5/6] apply: use `size_t` loop counters Zejun Zhao
2025-02-13  6:08     ` Patrick Steinhardt
2025-02-09  8:12   ` [GSOC][PATCH v2 6/6] apply: enable -Wsign-comparison checks Zejun Zhao
2025-02-16  7:28 ` [GSOC][PATCH v3 0/6] apply: address -Wsign-comparison warnings Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 1/6] apply: correct type misuse in `apply.h` Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 2/6] apply: change some variables from `int` to `size_t` Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 3/6] apply: do a typecast to eliminate warnings Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 4/6] apply: cast some ptrdiff_t's to size_t's Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 5/6] apply: use `size_t` loop counters Zejun Zhao
2025-02-16  7:28   ` [PATCH v3 6/6] apply: enable -Wsign-comparison checks Zejun Zhao

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=Z62MTZXQD3Wa47Jz@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jelly.zhao.42@gmail.com \
    --cc=newren@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.