Real path of soft links with missing target

Brian Burkhalter brian.burkhalter at oracle.com
Wed Jul 20 17:41:51 UTC 2022


On Jul 20, 2022, at 10:16 AM, maxxedev at gmail.com<mailto:maxxedev at gmail.com> wrote:

   #include <limits.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <errno.h>

   int main(int argc, char** argv) {
       char* path = "foo-source";
       char resolved[PATH_MAX + 1] = {0};
       char* result = realpath(path, resolved);
       printf("%d\n", result == NULL);  // 1
       printf("%d\n", errno == ENOENT); // 1
       printf("%s\n", resolved);   // "foo-target"
       return 0;
   }

When I run the above code in /tmp/ on Ubuntu 22.04 as

$ ln -s foo-target foo-source
$ ./resolve

it prints

1
1
/tmp/foo-target

As realpath(3)<https://www.man7.org/linux/man-pages/man3/realpath.3.html> states

“[If there is an error, ] it returns NULL, the contents of the array resolved_path are undefined, and errno<https://www.man7.org/linux/man-pages/man3/errno.3.html> is set to indicate the error.”

I don’t think one can rely on resolved_path or the return value when there is an error so I don’t know what we could do here. That resolved_path ends up being the link seems like an artifact that one cannot count on.

Brian



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/nio-dev/attachments/20220720/f8f2e7ea/attachment-0001.htm>


More information about the nio-dev mailing list