Skip to content

Commit 04a0e00

Browse files
committed
Merge branch 'kn/reftable-writer-log-write-verify' into jch
* kn/reftable-writer-log-write-verify: reftable/writer: ensure valid range for log's update_index
2 parents bf3d276 + 49c6b91 commit 04a0e00

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

reftable/writer.c

Lines changed: 12 additions & 0 deletions
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

t/unit-tests/t-reftable-readwrite.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void t_log_buffer_size(void)
9393
int i;
9494
struct reftable_log_record
9595
log = { .refname = (char *) "refs/heads/master",
96-
.update_index = 0xa,
96+
.update_index = update_index,
9797
.value_type = REFTABLE_LOG_UPDATE,
9898
.value = { .update = {
9999
.name = (char *) "Han-Wen Nienhuys",
@@ -130,7 +130,7 @@ static void t_log_overflow(void)
130130
int err;
131131
struct reftable_log_record log = {
132132
.refname = (char *) "refs/heads/master",
133-
.update_index = 0xa,
133+
.update_index = update_index,
134134
.value_type = REFTABLE_LOG_UPDATE,
135135
.value = {
136136
.update = {
@@ -154,6 +154,48 @@ static void t_log_overflow(void)
154154
reftable_buf_release(&buf);
155155
}
156156

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

t/unit-tests/t-reftable-stack.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,12 @@ static void t_reftable_stack_tombstone(void)
775775
}
776776

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

0 commit comments

Comments
 (0)