threads / patch / 62941

patch, 6 partsxdiff: move sign comparison warning guard into each file

Subject: [PATCH 1/6] xdiff: move sign comparison warning guard into each file

## tl;dr

6 messages between Feb 12, 2025 and Feb 12, 2025. Diffs are folded; open one to read it.

replies: 5people: 1as markdown or json

David Aguilar· Feb 12, 2025, 06:04 UTC · lore

Allow each file to fix the warnings guarded by the macro separately by moving the definition from the shared xinclude.h into each file that needs it.

xmerge.c and xprepare.c do not contain any signed vs. unsigned comparisons so the definition was not included in these files.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xemit.c      | 2 ++
 xdiff/xhistogram.c | 2 ++
 xdiff/xinclude.h   | 2 --
 xdiff/xpatience.c  | 3 +++
 xdiff/xutils.c     | 2 ++
 5 files changed, 9 insertions(+), 2 deletions(-)
Show changes to 5 files +9 −2

xdiff/xemit.c, xdiff/xhistogram.c, xdiff/xinclude.h, xdiff/xpatience.c, xdiff/xutils.c

diff --git a/xdiff/xemit.c b/xdiff/xemit.c
index 75f0fe4986..2b394a4806 100644
--- a/xdiff/xemit.c
+++ b/xdiff/xemit.c
@@ -20,6 +20,8 @@
  *
  */
 
+#define DISABLE_SIGN_COMPARE_WARNINGS
+
 #include "xinclude.h"
 
 static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {
diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
index 16a8fe2f3f..3d2b190fa6 100644
--- a/xdiff/xhistogram.c
+++ b/xdiff/xhistogram.c
@@ -41,6 +41,8 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define DISABLE_SIGN_COMPARE_WARNINGS
+
 #include "xinclude.h"
 
 #define MAX_PTR	UINT_MAX
diff --git a/xdiff/xinclude.h b/xdiff/xinclude.h
index 7e56542526..a4285ac0eb 100644
--- a/xdiff/xinclude.h
+++ b/xdiff/xinclude.h
@@ -23,8 +23,6 @@
 #if !defined(XINCLUDE_H)
 #define XINCLUDE_H
 
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
 #include "git-compat-util.h"
 #include "xmacros.h"
 #include "xdiff.h"
diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c
index a2d8955537..b0ba421b28 100644
--- a/xdiff/xpatience.c
+++ b/xdiff/xpatience.c
@@ -19,6 +19,9 @@
  *  Davide Libenzi <davidel@xmailserver.org>
  *
  */
+
+#define DISABLE_SIGN_COMPARE_WARNINGS
+
 #include "xinclude.h"
 
 /*
diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index 9e36f24875..169edff5ab 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -20,6 +20,8 @@
  *
  */
 
+#define DISABLE_SIGN_COMPARE_WARNINGS
+
 #include "xinclude.h"
 
 
-- 
2.48.1.643.g61982db19f
David Aguilar· Feb 12, 2025, 06:04 UTC · re: David Aguilar · lore

[PATCH 2/6] xdiff: avoid signed vs. unsigned comparisons in xdiffi.c

The loop iteration variable is non-negative and only used in comparisons against other size_t values.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xdiffi.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Show changes to xdiff/xdiffi.c +1 −2
diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c
index 4685ba6137..8889b8b62a 100644
--- a/xdiff/xdiffi.c
+++ b/xdiff/xdiffi.c
@@ -19,7 +19,6 @@
  *  Davide Libenzi <davidel@xmailserver.org>
  *
  */
-#define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "xinclude.h"
 
@@ -1014,7 +1013,7 @@ static void xdl_mark_ignorable_lines(xdchange_t *xscr, xdfenv_t *xe, long flags)
 
 static int record_matches_regex(xrecord_t *rec, xpparam_t const *xpp) {
 	regmatch_t regmatch;
-	int i;
+	size_t i;
 
 	for (i = 0; i < xpp->ignore_regex_nr; i++)
 		if (!regexec_buf(xpp->ignore_regex[i], rec->ptr, rec->size, 1,
-- 
2.48.1.643.g61982db19f
David Aguilar· Feb 12, 2025, 06:04 UTC · re: David Aguilar · lore

[PATCH 3/6] xdiff: avoid signed vs. unsigned comparisons in xemit.c

The unsigned `ignored` variable causes expressions to promote to unsigned. Use a signed value to make comparisons use the same types.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xemit.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Show changes to xdiff/xemit.c +1 −3
diff --git a/xdiff/xemit.c b/xdiff/xemit.c
index 2b394a4806..f8e3f25b03 100644
--- a/xdiff/xemit.c
+++ b/xdiff/xemit.c
@@ -20,8 +20,6 @@
  *
  */
 
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
 #include "xinclude.h"
 
 static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {
@@ -56,7 +54,7 @@ xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg)
 	xdchange_t *xch, *xchp, *lxch;
 	long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen;
 	long max_ignorable = xecfg->ctxlen;
-	unsigned long ignored = 0; /* number of ignored blank lines */
+	long ignored = 0; /* number of ignored blank lines */
 
 	/* remove ignorable changes that are too far before other changes */
 	for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) {
-- 
2.48.1.643.g61982db19f
David Aguilar· Feb 12, 2025, 06:04 UTC · re: David Aguilar · lore

[PATCH 4/6] xdiff: avoid signed vs. unsigned comparisons in xhistogram.c

The comparisons all involve unsigned variables. Cast the comparison to unsigned to eliminate the mismatch.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xhistogram.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
Show changes to xdiff/xhistogram.c +4 −6
diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
index 3d2b190fa6..040d81e0bc 100644
--- a/xdiff/xhistogram.c
+++ b/xdiff/xhistogram.c
@@ -41,8 +41,6 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
 #include "xinclude.h"
 
 #define MAX_PTR	UINT_MAX
@@ -108,7 +106,7 @@ static int scanA(struct histindex *index, int line1, int count1)
 	unsigned int chain_len;
 	struct record **rec_chain, *rec;
 
-	for (ptr = LINE_END(1); line1 <= ptr; ptr--) {
+	for (ptr = LINE_END(1); (unsigned int)line1 <= ptr; ptr--) {
 		tbl_idx = TABLE_HASH(index, 1, ptr);
 		rec_chain = index->records + tbl_idx;
 		rec = *rec_chain;
@@ -183,14 +181,14 @@ static int try_lcs(struct histindex *index, struct region *lcs, int b_ptr,
 			be = bs;
 			rc = rec->cnt;
 
-			while (line1 < as && line2 < bs
+			while ((unsigned int)line1 < as && (unsigned int)line2 < bs
 				&& CMP(index, 1, as - 1, 2, bs - 1)) {
 				as--;
 				bs--;
 				if (1 < rc)
 					rc = XDL_MIN(rc, CNT(index, as));
 			}
-			while (ae < LINE_END(1) && be < LINE_END(2)
+			while (ae < (unsigned int)LINE_END(1) && be < (unsigned int)LINE_END(2)
 				&& CMP(index, 1, ae + 1, 2, be + 1)) {
 				ae++;
 				be++;
@@ -315,7 +313,7 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
 	if (count1 <= 0 && count2 <= 0)
 		return 0;
 
-	if (LINE_END(1) >= MAX_PTR)
+	if ((unsigned int)LINE_END(1) >= MAX_PTR)
 		return -1;
 
 	if (!count1) {
-- 
2.48.1.643.g61982db19f
David Aguilar· Feb 12, 2025, 06:04 UTC · re: David Aguilar · lore

[PATCH 5/6] xdiff: avoid signed vs. unsigned comparisons in xpatience.c

The loop iteration variable is non-negative and used in comparisons against a size_t value. Use size_t to eliminate the mismatch.

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xpatience.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Show changes to xdiff/xpatience.c +1 −3
diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c
index b0ba421b28..82f663004e 100644
--- a/xdiff/xpatience.c
+++ b/xdiff/xpatience.c
@@ -20,8 +20,6 @@
  *
  */
 
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
 #include "xinclude.h"
 
 /*
@@ -78,7 +76,7 @@ struct hashmap {
 
 static int is_anchor(xpparam_t const *xpp, const char *line)
 {
-	int i;
+	size_t i;
 	for (i = 0; i < xpp->anchors_nr; i++) {
 		if (!strncmp(line, xpp->anchors[i], strlen(xpp->anchors[i])))
 			return 1;
-- 
2.48.1.643.g61982db19f
David Aguilar· Feb 12, 2025, 06:04 UTC · re: David Aguilar · lore

[PATCH 6/6] xdiff: avoid signed vs. unsigned comparisons in xutils.c

The comparisons all involve comparisons against unsigned values.
Signed-off-by: David Aguilar <davvid@gmail.com>
---
 xdiff/xutils.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
Show changes to xdiff/xutils.c +2 −4
diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index 169edff5ab..444a108f87 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -20,8 +20,6 @@
  *
  */
 
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
 #include "xinclude.h"
 
 
@@ -377,7 +375,7 @@ static int xdl_format_hunk_hdr(long s1, long c1, long s2, long c2,
 	nb += 3;
 	if (func && funclen) {
 		buf[nb++] = ' ';
-		if (funclen > sizeof(buf) - nb - 1)
+		if ((size_t)funclen > sizeof(buf) - nb - 1)
 			funclen = sizeof(buf) - nb - 1;
 		memcpy(buf + nb, func, funclen);
 		nb += funclen;
@@ -439,7 +437,7 @@ void* xdl_alloc_grow_helper(void *p, long nr, long *alloc, size_t size)
 {
 	void *tmp = NULL;
 	size_t n = ((LONG_MAX - 16) / 2 >= *alloc) ? 2 * *alloc + 16 : LONG_MAX;
-	if (nr > n)
+	if ((size_t)nr > n)
 		n = nr;
 	if (SIZE_MAX / size >= n)
 		tmp = xdl_realloc(p, n * size);
-- 
2.48.1.643.g61982db19f

← back to recent threads