Skip to content

Commit

Permalink
Signal readdir() errors by clearing/setting errno
Browse files Browse the repository at this point in the history
At least for Linux, distinguishing an error from the end of the
directory listing is done by setting errno to 0 and then calling
readdir(). Errno will then be set in the case of an error.

https://linux.die.net/man/3/readdir

This adresses

#17907

but unfortunately, there are no tests to (re)produce the behaviour at
all.

$! mirrors errno, so there also is added code in the example in
perlfunc.pod how to check for an error condition.
  • Loading branch information
Max Maischein committed Jun 28, 2020
1 parent 0382c61 commit 7683e28
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pod/perlfunc.pod
Expand Up @@ -6295,6 +6295,8 @@ value, not for its regular truth value.
while (readdir $dh) {
print "$some_dir/$_\n";
}
die "Got error while reading '$some_dir': $!"
if $!;
closedir $dh;

To avoid confusing would-be users of your code who are running earlier
Expand All @@ -6304,6 +6306,8 @@ recent vintage:

use 5.012; # so readdir assigns to $_ in a lone while test

In the case of an error, C<undef> will be returned and $! will be non-zero.

=item readline EXPR

=item readline
Expand Down
1 change: 1 addition & 0 deletions pp_sys.c
Expand Up @@ -4041,6 +4041,7 @@ PP(pp_readdir)
}

do {
SETERRNO(0,0);
dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
if (!dp)
break;
Expand Down

0 comments on commit 7683e28

Please sign in to comment.