parsers: avoid signed integer overflow in calculation of leaf-node index stable
authorYuya Nishihara <yuya@tcha.org>
Wed, 29 Apr 2015 23:07:34 +0900
branchstable
changeset 24879 b3142ea2a0d4
parent 24878 e530cde6d115
child 24880 5e111acc1170
parsers: avoid signed integer overflow in calculation of leaf-node index If v = -INT_MAX - 1, -v would exceed INT_MAX. I don't think this would cause problems such as issue4627, but we can't blame it as a compiler bug because signed integer overflow is undefined in C.
mercurial/parsers.c
--- a/mercurial/parsers.c	Tue Apr 28 17:38:02 2015 -0700
+++ b/mercurial/parsers.c	Wed Apr 29 23:07:34 2015 +0900
@@ -1312,7 +1312,7 @@
 			const char *n;
 			Py_ssize_t i;
 
-			v = -v - 1;
+			v = -(v + 1);
 			n = index_node(self, v);
 			if (n == NULL)
 				return -2;
@@ -1368,7 +1368,7 @@
 			return 0;
 		}
 		if (v < 0) {
-			const char *oldnode = index_node(self, -v - 1);
+			const char *oldnode = index_node(self, -(v + 1));
 			int noff;
 
 			if (!oldnode || !memcmp(oldnode, node, 20)) {