Skip to content

Commit 65c4ec8

Browse files
authored
ed: consistent filename validation
* Extend the filename validation from the f command to commands e and r, which also take a filename argument * Now the code can fail slightly earlier, before open_file_ro() is called
1 parent 27025b0 commit 65c4ec8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

bin/ed

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,7 @@ sub edFilename {
601601
return E_ADDREXT;
602602
}
603603
if (defined($args[0])) {
604-
return E_FNAME if $args[0] =~ m/\A\!/;
605-
return E_FNAME if $args[0] =~ m/\/\Z/;
606-
return E_FNAME if ($args[0] eq '.' || $args[0] eq '..');
604+
return E_FNAME if illegal_file($args[0]);
607605
$RememberedFilename = $args[0];
608606
}
609607
if (defined($RememberedFilename)) {
@@ -615,6 +613,15 @@ sub edFilename {
615613
return;
616614
}
617615

616+
sub illegal_file {
617+
my $name = shift;
618+
return 1 if length($name) == 0;
619+
return 1 if $name eq '.' or $name eq '..';
620+
return 1 if $name =~ m/\A\!/;
621+
return 1 if $name =~ m/\/\Z/;
622+
return 0;
623+
}
624+
618625
#
619626
# Write requested lines
620627
#
@@ -694,6 +701,7 @@ sub edRead {
694701
}
695702

696703
unless ($do_pipe) {
704+
return E_FNAME if illegal_file($filename);
697705
$fh = open_file_ro($filename);
698706
return E_OPEN unless $fh;
699707
}
@@ -752,6 +760,7 @@ sub edEdit {
752760
}
753761

754762
unless ($do_pipe) {
763+
return E_FNAME if illegal_file($filename);
755764
$fh = open_file_ro($filename);
756765
return E_OPEN unless $fh;
757766
}

0 commit comments

Comments
 (0)