Skip to content

Commit

Permalink
Merge pull request #308 from mknos/diff-contextlen
Browse files Browse the repository at this point in the history
diff: validation for -U and -C
  • Loading branch information
briandfoy authored Nov 2, 2023
2 parents 9347115 + 8c437ab commit 17f109a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions bin/diff
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ while ($ARGV[0] =~ /^-/) {
my $opt = shift;
last if $opt eq '--';
if ($opt =~ /^-C(.*)/) {
$Context_Lines = $1 || shift;
$Context_Lines = checklen($1 || shift);
$opt_c = 1;
$Diff_Type = "CONTEXT";
} elsif ($opt =~ /^-c$/) {
Expand All @@ -83,7 +83,7 @@ while ($ARGV[0] =~ /^-/) {
$opt_f = 1;
$Diff_Type = "REVERSE_ED";
} elsif ($opt =~ /^-U(.*)$/) {
$Context_Lines = $1 || shift;
$Context_Lines = checklen($1 || shift);
$opt_u = 1;
$Diff_Type = "UNIFIED";
} elsif ($opt =~ /^-u$/) {
Expand Down Expand Up @@ -193,6 +193,12 @@ sub bag {
exit 2;
}

sub checklen {
my $n = shift;
return int($n) if ($n =~ m/\A[0-9]+\Z/);
bag("Invalid context length '$n'\n$usage");
}

########
# Package Hunk. A Hunk is a group of Blocks which overlap because of the
# context surrounding each block. (So if we're not using context, every
Expand Down

0 comments on commit 17f109a

Please sign in to comment.