Skip to content

Commit cae70ac

Browse files
derrickstoleegitster
authored andcommitted
fsmonitor: de-duplicate BUG()s around dirty bits
The index has an fsmonitor_dirty bitmap that records which index entries are "dirty" based on the response from the FSMonitor. If this bitmap ever grows larger than the index, then there was an error in how it was constructed, and it was probably a developer's bug. There are several BUG() statements that are very similar, so replace these uses with a simpler assert_index_minimum(). Since there is one caller that uses a custom 'pos' value instead of the bit_size member, we cannot simplify it too much. However, the error string is identical in each, so this simplifies things. Be sure to add one when checking if a position if valid, since the minimum is a bound on the expected size. The end result is that the code is simpler to read while also preserving these assertions for developers in the FSMonitor space. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c80dd39 commit cae70ac

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

fsmonitor.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313

1414
struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
1515

16+
static void assert_index_minimum(struct index_state *istate, size_t pos)
17+
{
18+
if (pos > istate->cache_nr)
19+
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
20+
(uintmax_t)pos, istate->cache_nr);
21+
}
22+
1623
static void fsmonitor_ewah_callback(size_t pos, void *is)
1724
{
1825
struct index_state *istate = (struct index_state *)is;
1926
struct cache_entry *ce;
2027

21-
if (pos >= istate->cache_nr)
22-
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
23-
(uintmax_t)pos, istate->cache_nr);
28+
assert_index_minimum(istate, pos + 1);
2429

2530
ce = istate->cache[pos];
2631
ce->ce_flags &= ~CE_FSMONITOR_VALID;
@@ -82,10 +87,8 @@ int read_fsmonitor_extension(struct index_state *istate, const void *data,
8287
}
8388
istate->fsmonitor_dirty = fsmonitor_dirty;
8489

85-
if (!istate->split_index &&
86-
istate->fsmonitor_dirty->bit_size > istate->cache_nr)
87-
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
88-
(uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
90+
if (!istate->split_index)
91+
assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
8992

9093
trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
9194
return 0;
@@ -110,10 +113,8 @@ void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
110113
uint32_t ewah_size = 0;
111114
int fixup = 0;
112115

113-
if (!istate->split_index &&
114-
istate->fsmonitor_dirty->bit_size > istate->cache_nr)
115-
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
116-
(uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
116+
if (!istate->split_index)
117+
assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
117118

118119
put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
119120
strbuf_add(sb, &hdr_version, sizeof(uint32_t));
@@ -335,9 +336,7 @@ void tweak_fsmonitor(struct index_state *istate)
335336
}
336337

337338
/* Mark all previously saved entries as dirty */
338-
if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
339-
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
340-
(uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
339+
assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
341340
ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
342341

343342
refresh_fsmonitor(istate);

0 commit comments

Comments
 (0)