Skip to content

Commit af6f9d8

Browse files
authored
patch: improve validation of ed diffs
* Make more of an effort to reject patches where line addresses are not valid * Reading over the ed command parser in Patch::Ed::apply(), the regex assignment was not always correct * test1: "19,0d" command was incorrectly interpreted as "19,19d" thanks to the '||' operator * test2: "19,18d" command was accepted, but the range is backwards * test3: "0,19d" command was accepted, but address 0 is not allowed for "d" * test4: "0,19c" command was accepted, but address 0 is not allowed for "c" * I tested this by skipping to PLAN_J, using a modified diff with an update on line1 (ed command 0a is valid and means append after line 0) * Modifying elements of the diff input, I could trigger the new validation errors %perl diff -e env env2 | perl tee env.diff 19,20d 5,8c META DATA AND MORE . 0a #NEW LINE 1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .
1 parent f2d8543 commit af6f9d8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

bin/patch

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,14 @@ sub apply {
988988
shift(@hunk) =~ m!^(\d+)(?:,(\d+))?([acds])!
989989
or $self->throw('Unable to parse ed script');
990990

991-
my ($start, $end, $cmd) = ($1, $2 || $1, $3);
991+
my ($start, $end, $cmd) = ($1, $2, $3);
992+
$end = $start unless defined $end; # 0 can appear
993+
if ($start > $end) {
994+
$self->throw("Invalid address range: $start,$end\n");
995+
}
996+
if ($start == 0 && $cmd ne 'a') {
997+
$self->throw("Address zero invalid for command: $start,$end$cmd\n");
998+
}
992999

9931000
# We don't parse substitution commands and assume they all mean
9941001
# s/\.\././ even if they really mean s/\s+// or such. And we

0 commit comments

Comments
 (0)