Skip to content

Commit

Permalink
tail: check_number() too forgiving
Browse files Browse the repository at this point in the history
* Numbers can optionally be given with '-' or '+' prefix
* The number validation function allows numbers with leading garbage (regex match was only on end of string)
* These values were accepted: aaa1 +aaa1 -aaa1
* We can pass a helpful error message to usage() to indicate which argument was rejected
* Style: indent check_number()
* Tested this by passing numbers to -n option
  • Loading branch information
mknos authored Oct 9, 2023
1 parent 97b398d commit 10e79a2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bin/tail
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ my $block_size = 512;
sub check_number($)
{
my $opt = shift;
if ($opt =~ /\+(\d+)$/) {
return $1+0;
} elsif ($opt =~ /-?(\d+)$/) {
return -($1+0);
if ($opt =~ m/\A\+(\d+)\Z/) {
return $1+0;
} elsif ($opt =~ m/\A\-?(\d+)\Z/) {
return -($1+0);
} else {
usage(1);
usage(1, "invalid number '$opt'");
}
}

Expand Down

0 comments on commit 10e79a2

Please sign in to comment.