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

[PATCH v3 4/6] apply: cast some ptrdiff_t's to size_t's

From
ZZZejun Zhao <jelly.zhao.42@gmail.com>
Date
Feb 16, 2025, 07:28 UTC
Message-ID
<20250216072843.72385-5-jelly.zhao.42@gmail.com>
In-Reply-To
<20250216072843.72385-1-jelly.zhao.42@gmail.com>

There are several -Wsign-comparison warnings in "apply.c", complaining about us comparing ptrdiff_t's with size_t's.

Fix these warnings by typecasting from ptrdiff_t to size_t. As to why the casts is safe,

  - in function `date_len`, `date` is the starting address of a date at
  the end of the `line` and is guaranteed to be larger than (or equal
  to) `line`
  - in function `git_header_name`, `cp` is guaranteed to be larger than
  (or equal to) `second`, so `line + len` is greater than (or equal to)
  `cp` since we already treat `line + len - second` as a size_t
  - in function `git_header_name`, we are iterating `name` using
  `second`, so `second` is guaranteed to be greater than (or equal to)
  `name`
Signed-off-by: Zejun Zhao <jelly.zhao.42@gmail.com>
---
 apply.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/apply.c b/apply.c
index ac3e599bdf..c554a52f28 100644
--- a/apply.c
+++ b/apply.c
@@ -540,7 +540,7 @@ static size_t date_len(const char *line, size_t len)
 	    !isdigit(*p++) || !isdigit(*p++))	/* Not a date. */
 		return 0;
 
-	if (date - line >= strlen("19") &&
+	if ((size_t) (date - line) >= strlen("19") &&
 	    isdigit(date[-1]) && isdigit(date[-2]))	/* 4-digit year */
 		date -= strlen("19");
 
@@ -1207,7 +1207,7 @@ static char *git_header_name(int p_value,
 		cp = skip_tree_prefix(p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
-		if (line + llen - cp != first.len ||
+		if ((size_t) (line + llen - cp) != first.len ||
 		    memcmp(first.buf, cp, first.len))
 			goto free_and_fail1;
 		return strbuf_detach(&first, NULL);
@@ -1240,7 +1240,7 @@ static char *git_header_name(int p_value,
 				goto free_and_fail2;
 
 			len = sp.buf + sp.len - np;
-			if (len < second - name &&
+			if (len < (size_t) (second - name) &&
 			    !strncmp(np, name, len) &&
 			    isspace(name[len])) {
 				/* Good */
-- 
2.43.0
Previous: Zejun ZhaoNext: Zejun Zhao
Message 29 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.