index: remove side-effect from failed nt_init()
authorMartin von Zweigbergk <martinvonz@google.com>
Mon, 06 Aug 2018 22:34:37 -0700
changeset 38975 f7d8fb2ed8a8
parent 38974 05c1f5f49ebb
child 38976 dcd395dc98d8
index: remove side-effect from failed nt_init() As pointed out by Yuya in the review of D4108, if we run into the "overflow in nt_init" case (which I think normally happens only in repos with at least 2^26=64Mi revisions), we would leave the node tree half-initialized. Differential Revision: https://phab.mercurial-scm.org/D4153
mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c	Mon Aug 06 22:24:34 2018 -0700
+++ b/mercurial/cext/revlog.c	Mon Aug 06 22:34:37 2018 -0700
@@ -1092,15 +1092,15 @@
 static int nt_init(indexObject *self)
 {
 	if (self->nt == NULL) {
+		if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
+			PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
+			return -1;
+		}
 		self->nt = PyMem_Malloc(sizeof(nodetree));
 		if (self->nt == NULL) {
 			PyErr_NoMemory();
 			return -1;
 		}
-		if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
-			PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
-			return -1;
-		}
 		self->nt->capacity = self->raw_length < 4
 			? 4 : (int)self->raw_length / 2;