Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date: catch invalid date arguments #908

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions bin/date
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ License: artistic2

use POSIX;

my $VERSION = '1.0.4';
my $VERSION = '1.0.5';

# set this if we do anything to select a timezone abbreviation, then
# prefer this if it is defined.
Expand Down Expand Up @@ -80,8 +80,19 @@ sub run {
}

sub select_format {
my( $supplied ) = grep { /\A\+/ } @_;
return $1 if $supplied =~ /\A\+(.+)/;
my $supplied;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up backing out this change. I should have paid attention to the test failing, but there was another test failing so I just ignored both. That's dumb.

A few things to note here:

  • don't exit from utility subs. In the fix I die and wrap the sub in an eval. That gives the main code a chance to ignore it. Note a huge deal here because it's all private code, but in general it's about separation of concerns.
  • we can't stop if there is an option that doesn't start with + because there are other options possible, and maybe we need better argument processing so nothing else is left.

foreach (@_) {
unless (s/\A\+//) {
print STDERR "Invalid date: $_\n";
exit 1;
}
if (defined $supplied) {
print STDERR "Extra operand: '+$_'\n";
exit 1;
}
$supplied = $_;
}
return $supplied if defined $supplied;

my $formats = get_formats();

Expand Down
Loading