1From 7464397a0c5df0416a7ef3436747045b36fb7882 Mon Sep 17 00:00:00 2001
2From: Theodore Ts'o <tytso@mit.edu>
3Date: Tue, 9 Aug 2022 10:52:57 -0400
4Subject: e2fsck: fix potential fencepost error in
5 e2fsck_should_rebuild_extents()
6
7The ext2_extent_info.max_depth is zero-based (e.g., it is zero when
8the entire extent tree fits in the inode).  Hence, if it is equal to
9MAX_EXTENT_DEPTH_COUNT we should always rebuild the extent tree to
10shorten it.
11
12Also, for 1k block file systems, it's possible for the worst-case
13extent tree in its most compact form to have a maximum depth of 6, not
145.  So set MAX_EXTENT_DEPTH_COUNT to 8 just to be sure we have plenty
15of headroom.  (The kernel supports an extent depth up to 2**16, but
16e2fsck only keeps statistics up to MAX_EXTENT_DEPTH_COUNT, and if it's
17deeper than that, we know that it will be profitable to rebuild the
18extent tree in any case.)
19
20Addresses-Coverity-Bug: 1507761
21Signed-off-by: Theodore Ts'o <tytso@mit.edu>
22---
23 e2fsck/e2fsck.h  | 2 +-
24 e2fsck/extents.c | 2 +-
25 2 files changed, 2 insertions(+), 2 deletions(-)
26
27diff --git a/e2fsck/e2fsck.h b/e2fsck/e2fsck.h
28index 00b20919..75baf2cd 100644
29--- a/e2fsck/e2fsck.h
30+++ b/e2fsck/e2fsck.h
31@@ -233,7 +233,7 @@ typedef struct ea_refcount *ext2_refcount_t;
32  */
33 typedef struct e2fsck_struct *e2fsck_t;
34 
35-#define MAX_EXTENT_DEPTH_COUNT 5
36+#define MAX_EXTENT_DEPTH_COUNT 8
37 
38 /*
39  * This strucutre is used to manage the list of extents in a file. Placing
40diff --git a/e2fsck/extents.c b/e2fsck/extents.c
41index 86fe00e7..70798f34 100644
42--- a/e2fsck/extents.c
43+++ b/e2fsck/extents.c
44@@ -583,7 +583,7 @@ errcode_t e2fsck_should_rebuild_extents(e2fsck_t ctx,
45 			    sizeof(struct ext3_extent);
46 
47 	/* If the extent tree is too deep, then rebuild it. */
48-	if (info->max_depth > MAX_EXTENT_DEPTH_COUNT) {
49+	if (info->max_depth > MAX_EXTENT_DEPTH_COUNT-1) {
50 		pctx->blk = info->max_depth;
51 		op = PR_1E_CAN_COLLAPSE_EXTENT_TREE;
52 		goto rebuild;
53-- 
54cgit 
55
56