reftable: prevent 'update_index' changes after adding records

The function `reftable_writer_set_limits()` allows updating the
'min_update_index' and 'max_update_index' of a reftable writer. These
values are written to both the writer's header and footer.

Since the header is written during the first block write, any subsequent
changes to the update index would create a mismatch between the header
and footer values. The footer would contain the newer values while the
header retained the original ones.

To protect against this bug, prevent callers from updating these values
after any record is written. To do this, modify the function to return
an error whenever the limits are modified after any record adds. Check
for record adds within `reftable_writer_set_limits()` by checking the
`last_key` and `next` variable. The former is updated after each record
added, but is reset at certain points. The latter is set after writing
the first block.

Modify all callers of the function to anticipate a return type and
handle it accordingly. Add a unit test to also ensure the function
returns the error as expected.

Helped-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Karthik Nayak
2025-01-22 06:35:49 +01:00
committed by Junio C Hamano
parent e7c1b9f123
commit 017bd89239
6 changed files with 99 additions and 21 deletions

View File

@@ -179,11 +179,24 @@ int reftable_writer_new(struct reftable_writer **out,
return 0;
}
void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
int reftable_writer_set_limits(struct reftable_writer *w, uint64_t min,
uint64_t max)
{
/*
* Set the min/max update index limits for the reftable writer.
* This must be called before adding any records, since:
* - The 'next' field gets set after writing the first block.
* - The 'last_key' field updates with each new record (but resets
* after sections).
* Returns REFTABLE_API_ERROR if called after writing has begun.
*/
if (w->next || w->last_key.len)
return REFTABLE_API_ERROR;
w->min_update_index = min;
w->max_update_index = max;
return 0;
}
static void writer_release(struct reftable_writer *w)