Skip to content

Commit f7c607f

Browse files
committed
Merge branch 'kn/reftable-writer-log-write-verify'
Reftable backend adds check for upper limit of log's update_index. * kn/reftable-writer-log-write-verify: reftable/writer: ensure valid range for log's update_index
2 parents 19fbad7 + 49c6b91 commit f7c607f

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Diff for: reftable/writer.c

+12
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ int reftable_writer_add_log(struct reftable_writer *w,
425425
if (log->value_type == REFTABLE_LOG_DELETION)
426426
return reftable_writer_add_log_verbatim(w, log);
427427

428+
/*
429+
* Verify only the upper limit of the update_index. Each reflog entry
430+
* is tied to a specific update_index. Entries in the reflog can be
431+
* replaced by adding a new entry with the same update_index,
432+
* effectively canceling the old one.
433+
*
434+
* Consequently, reflog updates may include update_index values lower
435+
* than the writer's min_update_index.
436+
*/
437+
if (log->update_index > w->max_update_index)
438+
return REFTABLE_API_ERROR;
439+
428440
if (!log->refname)
429441
return REFTABLE_API_ERROR;
430442

Diff for: t/unit-tests/t-reftable-readwrite.c

+45-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void t_log_buffer_size(void)
9191
int i;
9292
struct reftable_log_record
9393
log = { .refname = (char *) "refs/heads/master",
94-
.update_index = 0xa,
94+
.update_index = update_index,
9595
.value_type = REFTABLE_LOG_UPDATE,
9696
.value = { .update = {
9797
.name = (char *) "Han-Wen Nienhuys",
@@ -128,7 +128,7 @@ static void t_log_overflow(void)
128128
int err;
129129
struct reftable_log_record log = {
130130
.refname = (char *) "refs/heads/master",
131-
.update_index = 0xa,
131+
.update_index = update_index,
132132
.value_type = REFTABLE_LOG_UPDATE,
133133
.value = {
134134
.update = {
@@ -152,6 +152,48 @@ static void t_log_overflow(void)
152152
reftable_buf_release(&buf);
153153
}
154154

155+
static void t_log_write_limits(void)
156+
{
157+
struct reftable_write_options opts = { 0 };
158+
struct reftable_buf buf = REFTABLE_BUF_INIT;
159+
struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
160+
struct reftable_log_record log = {
161+
.refname = (char *)"refs/head/master",
162+
.update_index = 0,
163+
.value_type = REFTABLE_LOG_UPDATE,
164+
.value = {
165+
.update = {
166+
.old_hash = { 1 },
167+
.new_hash = { 2 },
168+
.name = (char *)"Han-Wen Nienhuys",
169+
.email = (char *)"[email protected]",
170+
.tz_offset = 100,
171+
.time = 0x5e430672,
172+
},
173+
},
174+
};
175+
int err;
176+
177+
reftable_writer_set_limits(w, 1, 1);
178+
179+
/* write with update_index (0) below set limits (1, 1) */
180+
err = reftable_writer_add_log(w, &log);
181+
check_int(err, ==, 0);
182+
183+
/* write with update_index (1) in the set limits (1, 1) */
184+
log.update_index = 1;
185+
err = reftable_writer_add_log(w, &log);
186+
check_int(err, ==, 0);
187+
188+
/* write with update_index (3) above set limits (1, 1) */
189+
log.update_index = 3;
190+
err = reftable_writer_add_log(w, &log);
191+
check_int(err, ==, REFTABLE_API_ERROR);
192+
193+
reftable_writer_free(w);
194+
reftable_buf_release(&buf);
195+
}
196+
155197
static void t_log_write_read(void)
156198
{
157199
struct reftable_write_options opts = {
@@ -918,6 +960,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
918960
TEST(t_corrupt_table_empty(), "read-write on an empty table");
919961
TEST(t_log_buffer_size(), "buffer extension for log compression");
920962
TEST(t_log_overflow(), "log overflow returns expected error");
963+
TEST(t_log_write_limits(), "writer limits for writing log records");
921964
TEST(t_log_write_read(), "read-write on log records");
922965
TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error");
923966
TEST(t_table_read_api(), "read on a table");

Diff for: t/unit-tests/t-reftable-stack.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,12 @@ static void t_reftable_stack_tombstone(void)
773773
}
774774

775775
logs[i].refname = xstrdup(buf);
776-
/* update_index is part of the key. */
777-
logs[i].update_index = 42;
776+
/*
777+
* update_index is part of the key so should be constant.
778+
* The value itself should be less than the writer's upper
779+
* limit.
780+
*/
781+
logs[i].update_index = 1;
778782
if (i % 2 == 0) {
779783
logs[i].value_type = REFTABLE_LOG_UPDATE;
780784
t_reftable_set_hash(logs[i].value.update.new_hash, i,

0 commit comments

Comments
 (0)