diff mbox series

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

Message ID 20250216072843.72385-5-jelly.zhao.42@gmail.com (mailing list archive)
State New
Headers show
Series apply: address -Wsign-comparison warnings | expand

Commit Message

Zejun Zhao Feb. 16, 2025, 7:28 a.m. UTC
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 mbox series

Patch

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 */