Skip to content

Commit bc67b4a

Browse files
KarthikNayakgitster
authored andcommitted
reftable: write correct max_update_index to header
In 297c09e (refs: allow multiple reflog entries for the same refname, 2024-12-16), the reftable backend learned to handle multiple reflog entries within the same transaction. This was done modifying the `update_index` for reflogs with multiple indices. During writing the logs, the `max_update_index` of the writer was modified to ensure the limits were raised to the modified `update_index`s. However, since ref entries are written before the modification to the `max_update_index`, if there are multiple blocks to be written, the reftable backend writes the header with the old `max_update_index`. When all logs are finally written, the footer will be written with the new `min_update_index`. This causes a mismatch between the header and the footer and causes the reftable file to be corrupted. The existing tests only spawn a single block and since headers are lazily written with the first block, the tests didn't capture this bug. To fix the issue, the appropriate `max_update_index` limit must be set even before the first block is written. Add a `max_index` field to the transaction which holds the `max_index` within all its updates, then propagate this value to the reftable backend, wherein this is used to the set the `max_update_index` correctly. Add a test which creates a few thousand reference updates with multiple reflog entries, which should trigger the bug. Reported-by: brian m. carlson <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ddcdc1 commit bc67b4a

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

refs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,13 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
12971297
update->flags &= ~REF_HAVE_OLD;
12981298
update->index = index;
12991299

1300+
/*
1301+
* Reference backends may need to know the max index to optimize
1302+
* their writes. So we store the max_index on the transaction level.
1303+
*/
1304+
if (index > transaction->max_index)
1305+
transaction->max_index = index;
1306+
13001307
return 0;
13011308
}
13021309

refs/refs-internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ struct ref_transaction {
203203
enum ref_transaction_state state;
204204
void *backend_data;
205205
unsigned int flags;
206+
unsigned int max_index;
206207
};
207208

208209
/*

refs/reftable-backend.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ struct write_transaction_table_arg {
852852
size_t updates_nr;
853853
size_t updates_alloc;
854854
size_t updates_expected;
855+
unsigned int max_index;
855856
};
856857

857858
struct reftable_transaction_data {
@@ -1302,7 +1303,6 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
13021303
struct reftable_log_record *logs = NULL;
13031304
struct ident_split committer_ident = {0};
13041305
size_t logs_nr = 0, logs_alloc = 0, i;
1305-
uint64_t max_update_index = ts;
13061306
const char *committer_info;
13071307
int ret = 0;
13081308

@@ -1312,7 +1312,12 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
13121312

13131313
QSORT(arg->updates, arg->updates_nr, transaction_update_cmp);
13141314

1315-
reftable_writer_set_limits(writer, ts, ts);
1315+
/*
1316+
* During reflog migration, we add indexes for a single reflog with
1317+
* multiple entries. Each entry will contain a different update_index,
1318+
* so set the limits accordingly.
1319+
*/
1320+
reftable_writer_set_limits(writer, ts, ts + arg->max_index);
13161321

13171322
for (i = 0; i < arg->updates_nr; i++) {
13181323
struct reftable_transaction_update *tx_update = &arg->updates[i];
@@ -1414,12 +1419,6 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
14141419
*/
14151420
log->update_index = ts + u->index;
14161421

1417-
/*
1418-
* Note the max update_index so the limit can be set later on.
1419-
*/
1420-
if (log->update_index > max_update_index)
1421-
max_update_index = log->update_index;
1422-
14231422
log->refname = xstrdup(u->refname);
14241423
memcpy(log->value.update.new_hash,
14251424
u->new_oid.hash, GIT_MAX_RAWSZ);
@@ -1483,8 +1482,6 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
14831482
* and log blocks.
14841483
*/
14851484
if (logs) {
1486-
reftable_writer_set_limits(writer, ts, max_update_index);
1487-
14881485
ret = reftable_writer_add_logs(writer, logs, logs_nr);
14891486
if (ret < 0)
14901487
goto done;
@@ -1505,6 +1502,9 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,
15051502
struct reftable_transaction_data *tx_data = transaction->backend_data;
15061503
int ret = 0;
15071504

1505+
if (tx_data->args)
1506+
tx_data->args->max_index = transaction->max_index;
1507+
15081508
for (size_t i = 0; i < tx_data->args_nr; i++) {
15091509
ret = reftable_addition_add(tx_data->args[i].addition,
15101510
write_transaction_table, &tx_data->args[i]);

t/t1460-refs-migrate.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ do
227227
done
228228
done
229229

230+
test_expect_success 'multiple reftable blocks with multiple entries' '
231+
test_when_finished "rm -rf repo" &&
232+
git init --ref-format=files repo &&
233+
test_commit -C repo first &&
234+
printf "create refs/heads/ref-%d HEAD\n" $(test_seq 5000) >stdin &&
235+
git -C repo update-ref --stdin <stdin &&
236+
test_commit -C repo second &&
237+
printf "update refs/heads/ref-%d HEAD\n" $(test_seq 3000) >stdin &&
238+
git -C repo update-ref --stdin <stdin &&
239+
test_migration repo reftable
240+
'
241+
230242
test_expect_success 'migrating from files format deletes backend files' '
231243
test_when_finished "rm -rf repo" &&
232244
git init --ref-format=files repo &&

0 commit comments

Comments
 (0)