git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSOC][PATCH v2 1/6] apply: change fields in `apply_state` to unsigned

From
Karthik Nayak <karthik.188@gmail.com>
Date
Feb 13, 2025, 09:51 UTC
Message-ID
<CAOLa=ZQbVBmGnsvWUz0ByW8z5NMgLY2oZZ1RLJeoWJST9PGAvQ@mail.gmail.com>
In-Reply-To
<20250209081216.241350-2-jelly.zhao.42@gmail.com>
Zejun Zhao <jelly.zhao.42@gmail.com> writes:
Show 7 quoted lines
> `.max_change` and `.max_len` of `apply_state` are only used as unsigned
> integers. Misuse of `int` type would cause -Wsign-comparison warnings.
>
> Fix this by
>
>   - change `.max_change`'s type to `unsigned` since it's just a counter
>

Looking at `.max_change` it seems like this is only assigned in `patch_stats()` where we do

  int lines = patch->lines_added + patch->lines_deleted;
  if (lines > state->max_change)
     state->max_change = lines;

In this case shouldn't we first convert `.lines_added` `.lines_deleted` to also be 'unsigned int' in the first place?

>   - change `.max_len`'s type to `size_t` since it's a length
>

I see that this is assigned the return value of `strlen()` so this makes sense.

Show 25 quoted lines
>   - change the types of relevant variables in function `show_stats`
>
> Note that `printf`'s format string requires us to do some typecast
> (from `size_t` to `int`) on `max` in function `show_stats`. This is
> safe because we already set a upper bound of `50` for `max` before the
> cast.
>
> Signed-off-by: Zejun Zhao <jelly.zhao.42@gmail.com>
> ---
>  apply.c | 9 +++++----
>  apply.h | 4 ++--
>  2 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index 4a7b6120ac..831b338155 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -2238,7 +2238,8 @@ static void show_stats(struct apply_state *state, struct patch *patch)
>  {
>  	struct strbuf qname = STRBUF_INIT;
>  	char *cp = patch->new_name ? patch->new_name : patch->old_name;
> -	int max, add, del;
> +	size_t max;
> +	unsigned add, del;
>

Tangential to this patch, I don't think we have a guideline on using 'unsigned' vs 'unsigned int'. Probably we should.

Show 43 quoted lines
>  	quote_c_style(cp, &qname, NULL, 0);
>
> @@ -2257,12 +2258,12 @@ static void show_stats(struct apply_state *state, struct patch *patch)
>  	}
>
>  	if (patch->is_binary) {
> -		printf(" %-*s |  Bin\n", max, qname.buf);
> +		printf(" %-*s |  Bin\n", (int) max, qname.buf);
>  		strbuf_release(&qname);
>  		return;
>  	}
>
> -	printf(" %-*s |", max, qname.buf);
> +	printf(" %-*s |", (int) max, qname.buf);
>  	strbuf_release(&qname);
>
>  	/*
> @@ -2273,7 +2274,7 @@ static void show_stats(struct apply_state *state, struct patch *patch)
>  	del = patch->lines_deleted;
>
>  	if (state->max_change > 0) {
> -		int total = ((add + del) * max + state->max_change / 2) / state->max_change;
> +		unsigned total = ((add + del) * max + state->max_change / 2) / state->max_change;
>  		add = (add * max + state->max_change / 2) / state->max_change;
>  		del = total - add;
>  	}
> diff --git a/apply.h b/apply.h
> index 90e887ec0e..f7f369d44f 100644
> --- a/apply.h
> +++ b/apply.h
> @@ -90,8 +90,8 @@ struct apply_state {
>  	 * we've seen, and the longest filename. That allows us to do simple
>  	 * scaling.
>  	 */
> -	int max_change;
> -	int max_len;
> +	unsigned max_change;
> +	size_t max_len;
>
>  	/*
>  	 * Records filenames that have been touched, in order to handle
> --
> 2.43.0
The rest look good.
Previous: Zejun ZhaoNext: Junio C Hamano
Message 8 of 31 in “apply: address -Wsign-comparison warnings”
  1. Zejun ZhaoFeb 5, 2025
  2. Patrick SteinhardtFeb 5, 2025
  3. Zejun ZhaoFeb 5, 2025
  4. Junio C HamanoFeb 5, 2025
  5. Zejun ZhaoFeb 5, 2025
  6. [GSOC][PATCH v2 0/6] apply: address -Wsign-comparison warningsZejun Zhao, Feb 9, 2025
  7. [GSOC][PATCH v2 1/6] apply: change fields in `apply_state` to unsignedZejun Zhao, Feb 9, 2025
  8. Karthik NayakFeb 13, 2025
  9. Junio C HamanoFeb 13, 2025
  10. [GSOC][PATCH v2 2/6] apply: change some variables from `int` to `size_t`Zejun Zhao, Feb 9, 2025
  11. Patrick SteinhardtFeb 13, 2025
  12. Zejun ZhaoFeb 16, 2025
  13. Junio C HamanoFeb 18, 2025
  14. Zejun ZhaoFeb 21, 2025
  15. Junio C HamanoFeb 21, 2025
  16. Zejun ZhaoFeb 23, 2025
  17. Junio C HamanoFeb 24, 2025
  18. Zejun ZhaoFeb 25, 2025
  19. Junio C HamanoFeb 25, 2025
  20. [GSOC][PATCH v2 3/6] apply: do a typecast to eliminate warningsZejun Zhao, Feb 9, 2025
  21. [GSOC][PATCH v2 4/6] apply: cast some ptrdiff_t's to size_t'sZejun Zhao, Feb 9, 2025
  22. [GSOC][PATCH v2 5/6] apply: use `size_t` loop countersZejun Zhao, Feb 9, 2025
  23. Patrick SteinhardtFeb 13, 2025
  24. [GSOC][PATCH v2 6/6] apply: enable -Wsign-comparison checksZejun Zhao, Feb 9, 2025
  25. [GSOC][PATCH v3 0/6] apply: address -Wsign-comparison warningsZejun Zhao, Feb 16, 2025
  26. 1/6 apply: correct type misuse in `apply.h`Zejun Zhao, Feb 16, 2025
  27. 2/6 apply: change some variables from `int` to `size_t`Zejun Zhao, Feb 16, 2025
  28. 3/6 apply: do a typecast to eliminate warningsZejun Zhao, Feb 16, 2025
  29. 4/6 apply: cast some ptrdiff_t's to size_t'sZejun Zhao, Feb 16, 2025
  30. 5/6 apply: use `size_t` loop countersZejun Zhao, Feb 16, 2025
  31. 6/6 apply: enable -Wsign-comparison checksZejun Zhao, Feb 16, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.