diff --git a/mercurial/cext/manifest.c b/mercurial/cext/manifest.c
index f27f8e9ef1e731723146b670a1a9a86e1a088069_bWVyY3VyaWFsL2NleHQvbWFuaWZlc3QuYw==..da4478ca0e3242d3a74eb0e0038ff83be30691f4_bWVyY3VyaWFsL2NleHQvbWFuaWZlc3QuYw== 100644
--- a/mercurial/cext/manifest.c
+++ b/mercurial/cext/manifest.c
@@ -137,10 +137,7 @@
 			return MANIFEST_TOO_SHORT_LINE;
 		}
 		next++; /* advance past newline */
-		if (!realloc_if_full(self)) {
-			return MANIFEST_OOM; /* no memory */
-		}
 		if (prev && strcmp(prev, data) > -1) {
 			/* This data isn't sorted, so we have to abort. */
 			return MANIFEST_NOT_SORTED;
 		}
@@ -143,7 +140,10 @@
 		if (prev && strcmp(prev, data) > -1) {
 			/* This data isn't sorted, so we have to abort. */
 			return MANIFEST_NOT_SORTED;
 		}
+		if (!realloc_if_full(self)) {
+			return MANIFEST_OOM; /* no memory */
+		}
 		l = self->lines + ((self->numlines)++);
 		l->start = data;
 		l->len = next - data;