parsers: check for memory allocation overflows more carefully
authorBryan O'Sullivan <bryano@fb.com>
Mon, 06 Apr 2015 08:23:27 -0700
changeset 24623 2262d7bc469e
parent 24622 1e05f11619bb
child 24624 6f0e6fa9fdd7
parsers: check for memory allocation overflows more carefully
mercurial/parsers.c
--- a/mercurial/parsers.c	Sat Apr 04 11:27:15 2015 +0200
+++ b/mercurial/parsers.c	Mon Apr 06 08:23:27 2015 -0700
@@ -1319,6 +1319,11 @@
 static int nt_new(indexObject *self)
 {
 	if (self->ntlength == self->ntcapacity) {
+		if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) {
+			PyErr_SetString(PyExc_MemoryError,
+					"overflow in nt_new");
+			return -1;
+		}
 		self->ntcapacity *= 2;
 		self->nt = realloc(self->nt,
 				   self->ntcapacity * sizeof(nodetree));
@@ -1380,7 +1385,7 @@
 static int nt_init(indexObject *self)
 {
 	if (self->nt == NULL) {
-		if (self->raw_length > INT_MAX) {
+		if (self->raw_length > INT_MAX / sizeof(nodetree)) {
 			PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
 			return -1;
 		}