Skip to content

Commit 0ec5b3b

Browse files
author
Kent Overstreet
committed
bcachefs: Fix shift-by-64 in bformat_needs_redo()
Ancient versions of bcachefs produced packed formats that could represent keys that our in memory format cannot represent; bformat_needs_redo() has some tricky shifts to check for this sort of overflow. Reported-by: [email protected] Signed-off-by: Kent Overstreet <[email protected]>
1 parent 2bb9600 commit 0ec5b3b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

fs/bcachefs/move.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -968,24 +968,30 @@ static bool migrate_btree_pred(struct bch_fs *c, void *arg,
968968
return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
969969
}
970970

971+
/*
972+
* Ancient versions of bcachefs produced packed formats which could represent
973+
* keys that the in memory format cannot represent; this checks for those
974+
* formats so we can get rid of them.
975+
*/
971976
static bool bformat_needs_redo(struct bkey_format *f)
972977
{
973-
unsigned i;
974-
975-
for (i = 0; i < f->nr_fields; i++) {
978+
for (unsigned i = 0; i < f->nr_fields; i++) {
979+
unsigned f_bits = f->bits_per_field[i];
976980
unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
977981
u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
978982
u64 field_offset = le64_to_cpu(f->field_offset[i]);
979983

980-
if (f->bits_per_field[i] > unpacked_bits)
984+
if (f_bits > unpacked_bits)
981985
return true;
982986

983-
if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
987+
if ((f_bits == unpacked_bits) && field_offset)
984988
return true;
985989

986-
if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
987-
unpacked_mask) <
988-
field_offset)
990+
u64 f_mask = f_bits
991+
? ~((~0ULL << (f_bits - 1)) << 1)
992+
: 0;
993+
994+
if (((field_offset + f_mask) & unpacked_mask) < field_offset)
989995
return true;
990996
}
991997

0 commit comments

Comments
 (0)