@@ -103,7 +103,8 @@ static void t_read_file(void)
103103static int write_test_ref (struct reftable_writer * wr , void * arg )
104104{
105105 struct reftable_ref_record * ref = arg ;
106- reftable_writer_set_limits (wr , ref -> update_index , ref -> update_index );
106+ check (!reftable_writer_set_limits (wr , ref -> update_index ,
107+ ref -> update_index ));
107108 return reftable_writer_add_ref (wr , ref );
108109}
109110
@@ -143,7 +144,8 @@ static int write_test_log(struct reftable_writer *wr, void *arg)
143144{
144145 struct write_log_arg * wla = arg ;
145146
146- reftable_writer_set_limits (wr , wla -> update_index , wla -> update_index );
147+ check (!reftable_writer_set_limits (wr , wla -> update_index ,
148+ wla -> update_index ));
147149 return reftable_writer_add_log (wr , wla -> log );
148150}
149151
@@ -961,7 +963,7 @@ static void t_reflog_expire(void)
961963
962964static int write_nothing (struct reftable_writer * wr , void * arg UNUSED )
963965{
964- reftable_writer_set_limits (wr , 1 , 1 );
966+ check (! reftable_writer_set_limits (wr , 1 , 1 ) );
965967 return 0 ;
966968}
967969
@@ -1369,11 +1371,57 @@ static void t_reftable_stack_reload_with_missing_table(void)
13691371 clear_dir (dir );
13701372}
13711373
1374+ static int write_limits_after_ref (struct reftable_writer * wr , void * arg )
1375+ {
1376+ struct reftable_ref_record * ref = arg ;
1377+ check (!reftable_writer_set_limits (wr , ref -> update_index , ref -> update_index ));
1378+ check (!reftable_writer_add_ref (wr , ref ));
1379+ return reftable_writer_set_limits (wr , ref -> update_index , ref -> update_index );
1380+ }
1381+
1382+ static void t_reftable_invalid_limit_updates (void )
1383+ {
1384+ struct reftable_ref_record ref = {
1385+ .refname = (char * ) "HEAD" ,
1386+ .update_index = 1 ,
1387+ .value_type = REFTABLE_REF_SYMREF ,
1388+ .value .symref = (char * ) "master" ,
1389+ };
1390+ struct reftable_write_options opts = {
1391+ .default_permissions = 0660 ,
1392+ };
1393+ struct reftable_addition * add = NULL ;
1394+ char * dir = get_tmp_dir (__LINE__ );
1395+ struct reftable_stack * st = NULL ;
1396+ int err ;
1397+
1398+ err = reftable_new_stack (& st , dir , & opts );
1399+ check (!err );
1400+
1401+ reftable_addition_destroy (add );
1402+
1403+ err = reftable_stack_new_addition (& add , st , 0 );
1404+ check (!err );
1405+
1406+ /*
1407+ * write_limits_after_ref also updates the update indexes after adding
1408+ * the record. This should cause an err to be returned, since the limits
1409+ * must be set at the start.
1410+ */
1411+ err = reftable_addition_add (add , write_limits_after_ref , & ref );
1412+ check_int (err , = = , REFTABLE_API_ERROR );
1413+
1414+ reftable_addition_destroy (add );
1415+ reftable_stack_destroy (st );
1416+ clear_dir (dir );
1417+ }
1418+
13721419int cmd_main (int argc UNUSED , const char * argv [] UNUSED )
13731420{
13741421 TEST (t_empty_add (), "empty addition to stack" );
13751422 TEST (t_read_file (), "read_lines works" );
13761423 TEST (t_reflog_expire (), "expire reflog entries" );
1424+ TEST (t_reftable_invalid_limit_updates (), "prevent limit updates after adding records" );
13771425 TEST (t_reftable_stack_add (), "add multiple refs and logs to stack" );
13781426 TEST (t_reftable_stack_add_one (), "add a single ref record to stack" );
13791427 TEST (t_reftable_stack_add_performs_auto_compaction (), "addition to stack triggers auto-compaction" );
0 commit comments