git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zejun Zhao <jelly.zhao.42@gmail.com>
To: jelly.zhao.42@gmail.com
Cc: git@vger.kernel.org, gitster@pobox.com, newren@gmail.com, ps@pks.im
Subject: [GSOC][PATCH v2 4/6] apply: cast some ptrdiff_t's to size_t's
Date: Sun,  9 Feb 2025 08:12:14 +0000	[thread overview]
Message-ID: <20250209081216.241350-5-jelly.zhao.42@gmail.com> (raw)
In-Reply-To: <20250209081216.241350-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 605a0aa2e3..72464fb6c2 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


  parent reply	other threads:[~2025-02-09  8:13 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
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   ` Zejun Zhao [this message]
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=20250209081216.241350-5-jelly.zhao.42@gmail.com \
    --to=jelly.zhao.42@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=ps@pks.im \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).