[DragonFlyBSD - Bug #3250] (Resolved) getcwd() succeeds even if the current directory no longer exists

bugtracker-admin at leaf.dragonflybsd.org bugtracker-admin at leaf.dragonflybsd.org
Sun Nov 1 10:01:22 PST 2020


Issue #3250 has been updated by deef.

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+.

----------------------------------------
Bug #3250: getcwd() succeeds even if the current directory no longer exists
http://bugs.dragonflybsd.org/issues/3250#change-13930

* Author: tonyc
* Status: Resolved
* Priority: Normal
* Assignee: 
* Category: Kernel
* Target version: Latest stable
----------------------------------------
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     root at www.shiningsilence.com:/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



-- 
You have received this notification because you have either subscribed to it, or are involved in it.
To change your notification preferences, please click here: http://bugs.dragonflybsd.org/my/account



More information about the Bugs mailing list