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

[PATCH v3 2/6] apply: change some variables from `int` to `size_t`

From
ZZZejun Zhao <jelly.zhao.42@gmail.com>
Date
Feb 16, 2025, 07:28 UTC
Message-ID
<20250216072843.72385-3-jelly.zhao.42@gmail.com>
In-Reply-To
<20250216072843.72385-1-jelly.zhao.42@gmail.com>
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 int`.
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 4380a83386..4aa47a22b9 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, '.');
@@ -2185,7 +2185,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 			};
 			int i;
 			for (i = 0; binhdr[i]; i++) {
-				int len = strlen(binhdr[i]);
+				size_t len = strlen(binhdr[i]);
 				if (len < size - hd &&
 				    !memcmp(binhdr[i], buffer + hd, len)) {
 					state->linenr++;
@@ -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;
 
 	/*
@@ -2418,7 +2419,7 @@ static int line_by_line_fuzzy_match(struct image *img,
 				    struct image *postimage,
 				    unsigned long current,
 				    int current_lno,
-				    int preimage_limit)
+				    size_t preimage_limit)
 {
 	int i;
 	size_t imgoff = 0;
@@ -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) {
@@ -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
@@ -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,
@@ -4293,14 +4294,14 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
 	if (lines > state->max_change)
 		state->max_change = lines;
 	if (patch->old_name) {
-		int len = quote_c_style(patch->old_name, NULL, NULL, 0);
+		size_t len = quote_c_style(patch->old_name, NULL, NULL, 0);
 		if (!len)
 			len = strlen(patch->old_name);
 		if (len > state->max_len)
 			state->max_len = len;
 	}
 	if (patch->new_name) {
-		int len = quote_c_style(patch->new_name, NULL, NULL, 0);
+		size_t len = quote_c_style(patch->new_name, NULL, NULL, 0);
 		if (!len)
 			len = strlen(patch->new_name);
 		if (len > state->max_len)
-- 
2.43.0
Previous: Zejun ZhaoNext: Zejun Zhao
Message 27 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.