diff --git a/fs/xfs/xfs_bmap.c b/fs/xfs/xfs_bmap.c
index 130ec4fa5eaf8218533bce6fe22151e9db385964..af655c1d460dcd7ff11f040d1078b9d4a039f742 100644
--- a/fs/xfs/xfs_bmap.c
+++ b/fs/xfs/xfs_bmap.c
@@ -4825,12 +4825,13 @@ xfs_bmapi(
 		/*
 		 * Else go on to the next record.
 		 */
-		ep = xfs_iext_get_ext(ifp, ++lastx);
 		prev = got;
-		if (lastx >= nextents)
-			eof = 1;
-		else
+		if (++lastx < nextents) {
+			ep = xfs_iext_get_ext(ifp, lastx);
 			xfs_bmbt_get_all(ep, &got);
+		} else {
+			eof = 1;
+		}
 	}
 	*nmap = n;
 	/*