Skip to content

Commit c67fc0d

Browse files
authored
ls: avoid invalid closedir() (#801)
* When temporarily enabling warnings.pm I observed a warning in DirEntries(): closedir() attempted on invalid dirhandle $dh * The way the code was written, opendir() was always called even if the file was not a directory * Delay the opendir() call until we know that we don't want to list a regular file * opendir() should not happen if running "ls -d", or if the argument to DirEntries() is not a directory
1 parent 0c0305a commit c67fc0d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

bin/ls

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ sub DirEntries {
150150
my @Entries = (); # entries in original order
151151
my $Name = ""; # entry name
152152

153-
if (!opendir($dh, $_[0]) || exists($Options{'d'})) {
153+
if (exists $Options{'d'} || ! -d $_[0]) {
154154
if (-e $_[0]) {
155-
closedir($dh) if (defined($dh));
156155
push(@Entries, $_[0]);
157156
$Attributes{$_[0]} = stat($_[0]);
158157
push(@Entries, \%Attributes);
@@ -161,6 +160,10 @@ sub DirEntries {
161160
warn "$Program: can't access '$_[0]': $!\n";
162161
return ();
163162
}
163+
unless (opendir $dh, $_[0]) {
164+
warn "$Program: failed to open directory '$_[0]': $!\n";
165+
return ();
166+
}
164167
while ($Name = readdir($dh)) {
165168
next if (!exists($Options->{'a'}) &&
166169
$Name =~ m/^\./o);

0 commit comments

Comments
 (0)