Don't accept ranges for add commands in consdiff

Fixes ticket #21963
This commit is contained in:
Sebastian Hahn 2017-04-17 06:46:23 +02:00
parent b081a7ed21
commit 459643502b
2 changed files with 18 additions and 0 deletions

View File

@ -795,6 +795,7 @@ apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
diff_line[diff_cdline->len] = 0; diff_line[diff_cdline->len] = 0;
const char *ptr = diff_line; const char *ptr = diff_line;
int start = 0, end = 0; int start = 0, end = 0;
int had_range = 0;
if (get_linenum(&ptr, &start) < 0) { if (get_linenum(&ptr, &start) < 0) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because " log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"an ed command was missing a line number."); "an ed command was missing a line number.");
@ -802,6 +803,7 @@ apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
} }
if (*ptr == ',') { if (*ptr == ',') {
/* Two-item range */ /* Two-item range */
had_range = 1;
++ptr; ++ptr;
if (get_linenum(&ptr, &end) < 0) { if (get_linenum(&ptr, &end) < 0) {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because " log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
@ -850,6 +852,13 @@ apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
goto error_cleanup; goto error_cleanup;
} }
/* 'a' commands are not allowed to have ranges. */
if (had_range && action == 'a') {
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
"it wanted to add lines after a range.");
goto error_cleanup;
}
/* Add unchanged lines. */ /* Add unchanged lines. */
for (; j && j > end; --j) { for (; j && j > end; --j) {
cdline_t *cons_line = smartlist_get(cons1, j-1); cdline_t *cons_line = smartlist_get(cons1, j-1);

View File

@ -687,6 +687,15 @@ test_consdiff_apply_ed_diff(void *arg)
smartlist_clear(diff); smartlist_clear(diff);
/* Unexpected range for add command. */
smartlist_add_linecpy(diff, area, "1,2a");
mock_clean_saved_logs();
cons2 = apply_ed_diff(cons1, diff, 0);
tt_ptr_op(NULL, OP_EQ, cons2);
expect_single_log_msg_containing("add lines after a range");
smartlist_clear(diff);
/* Script is not in reverse order. */ /* Script is not in reverse order. */
smartlist_add_linecpy(diff, area, "1d"); smartlist_add_linecpy(diff, area, "1d");
smartlist_add_linecpy(diff, area, "3d"); smartlist_add_linecpy(diff, area, "3d");