[PATCH STABLE] parsers.c: fix two compiler warnings

Matt Mackall mpm at selenic.com
Sat Feb 13 17:38:20 CST 2010


On Sat, 2010-02-13 at 16:07 -0600, Steve Borho wrote:
> # HG changeset patch
> # User Steve Borho <steve at borho.org>
> # Date 1266098056 21600
> # Node ID 648d858088d467770c8aa969b1780af076bc7197
> # Parent  956498af9812f5d4e81d6e8f44d62c3d3c556b1f
> parsers.c: fix two compiler warnings
> 
> MSVC 2008 warns:
> 
> mercurial/parsers.c(197) : warning C4018: '>' : signed/unsigned mismatch
> mercurial/parsers.c(335) : warning C4018: '<' : signed/unsigned mismatch

> -		if (flen > end - cur) {
> +		if (flen > (unsigned int)(end - cur)) {

This looks a little iffy, here what I came up with:

diff -r 6e5a47398fc5 mercurial/parsers.c
--- a/mercurial/parsers.c	Sat Feb 13 22:10:31 2010 +0100
+++ b/mercurial/parsers.c	Sat Feb 13 17:30:10 2010 -0600
@@ -194,7 +194,7 @@
 		mtime = ntohl(*(uint32_t *)(decode + 8));
 		flen = ntohl(*(uint32_t *)(decode + 12));
 		cur += 17;
-		if (flen > end - cur) {
+		if (cur + flen > end || cur + flen < cur) {
 			PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
 			goto quit;
 		}
@@ -332,7 +332,7 @@
 
 		n++;
 		step = 64 + (inlined ? comp_len : 0);
-		if (end - data < step)
+		if (data + step > end || data + step < data)
 			break;
 		data += step;
 	}

Step one is to fix unnecessary subtraction by adding the subtracted item
to both sides. Then we add a second clause to detect integer wraparound.

-- 
http://selenic.com : development and support for Mercurial and Linux




More information about the Mercurial-devel mailing list