Project

General

Profile

Bug #3250

getcwd() succeeds even if the current directory no longer exists

Added by tonyc about 1 month ago. Updated 29 days ago.

Status:
Resolved
Priority:
Normal
Assignee:
-
Category:
Kernel
Target version:
Start date:
10/22/2020
Due date:
11/01/2020
% Done:

100%

Estimated time:

Description

The Dragonfly BSD getcwd(3) man page claims:

ERRORS
The getcwd() function will fail if:

...
[ENOENT] A component of the pathname no longer exists.

but this does not occur:

$ cat cwd_noent.c
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main(void) {
char path[255];

if (mkdir("testdir", 0700) < 0) {
perror("NA: mkdir");
exit(1);
}
if (chdir("testdir") < 0) {
perror("NA: chdir");
exit(1);
}
if (rmdir("../testdir") < 0) {
perror("NA: testdir");
exit(1);
}
/* getcwd() succeeds despite the directory no longer existing */
if (getcwd(path, sizeof(path))) {
struct stat st;
int rc = stat(path, &st);

printf("# %s\n", path);
if (rc < 0) {
perror("FAIL: stat failed for path returned by getcwd");
exit(1);
}
else {
printf("NA: directory I removed still exists\n");
}
}
else {
int err = errno;
printf("OK: getcwd failed\n");
if (err == ENOENT) {
printf("OK: ENOENT correctly set\n");
}
else {
printf("FAIL: bad errno %d\n", err);
exit(1);
}
}
return 0;
}
$ cc -ocwd_noent cwd_noent.c
$ ./cwd_noent
# /home/tony/dev/perl/git/dflybugs/testdir
FAIL: stat failed for path returned by getcwd: No such file or directory
$ uname -a
DragonFly 5.8-RELEASE DragonFly v5.8.3-RELEASE #10: Thu Sep 24 19:19:45 EDT 2020 :/usr/obj/home/justin/release/5_8/sys/X86_64_GENERIC x86_64

This came up from a failure in the perl test suite, our bug for this can be found at https://github.com/Perl/perl5/issues/16525

History

#1

Updated by deef 29 days ago

  • Due date set to 11/01/2020
  • Category changed from Userland to Kernel
  • Status changed from New to Resolved
  • % Done changed from 0 to 100

Thank you for detailed bug report. Fixed in commit d6853f97+ and MFCed to RELEASE_5_8 in commit 6e9a8bad+.

Also available in: Atom PDF