Subject: bug#27871: 26.0.50; Bad handling of unmounted directory

From: Eli Zaretskii <[email-masked]>
Date: Sat, 23 Sep 2017 13:39:29 +0300

> From: Philipp Stephani <[email-masked]>
> Date: Sat, 23 Sep 2017 10:19:19 +0000
> 
> Here's a patch. With the patch, the output is
> 
> Error getting directory: Transport endpoint is not connected
> Warning (initialization): Error setting default-directory
> Ignoring relative file name (a) due to nil default-directory

Thanks.

> +/* Return the current working directory.  The result should be freed
> +   with 'free'.  Return NULL on errors.  */
> +char *
> +emacs_get_current_dir_name (void)
> +{
> +  char *dir = emacs_get_current_dir_name_1 ();
> +  if (dir == NULL)
> +    return NULL;
> +  /* On Linux, getcwd and get_current_dir_name return a string
> +     starting with "(unreachable)" if the current directory doesn't
> +     exist, e.g. because it was unmounted.  Treat that as an error.
> +     See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27871.  */
> +  const char *prefix = "(unreachable)";
> +  size_t dir_len = strlen (dir);
> +  size_t prefix_len = strlen (prefix);
> +  if (dir_len >= prefix_len && strncmp (dir, prefix, prefix_len) == 0)
> +    {
> +      errno = ENOTCONN;
> +      return NULL;

What if there's a directory called literally "(unreachable)SOMETHING"?
This isn't disallowed on GNU/Linux, is it?  We should probably stat
the result if we see this prefix, before we declare it a failure, IMO.