diff --git a/mercurial/bdiff.c b/mercurial/bdiff.c
index c8fa7ad1ff908fe6a1c829f03ddce676f9a9bd05_bWVyY3VyaWFsL2JkaWZmLmM=..d500ddae7494772e5eb867fccc6876f5f0c21dac_bWVyY3VyaWFsL2JkaWZmLmM= 100644
--- a/mercurial/bdiff.c
+++ b/mercurial/bdiff.c
@@ -31,6 +31,6 @@
 
 	/* count the lines */
 	i = 1; /* extra line for sentinel */
-	for (p = a; p < a + len; p++)
-		if (*p == '\n' || p == plast)
+	for (p = a; p < plast; p++)
+		if (*p == '\n')
 			i++;
@@ -36,4 +36,6 @@
 			i++;
+	if (p == plast)
+		i++;
 
 	*lr = l = (struct bdiff_line *)malloc(sizeof(struct bdiff_line) * i);
 	if (!l)