unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Philipp Stephani <p.stephani2@gmail.com>, 27871@debbugs.gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: bug#27871: [PATCH] Treat unreachable current directory as error
Date: Sat, 7 Oct 2017 23:04:54 -0700	[thread overview]
Message-ID: <9b7b9905-dc97-feb4-9569-27372b1c6b5d@cs.ucla.edu> (raw)
In-Reply-To: <CAArVCkSN_ZSZog8KKEx=kMZD+V-vncpaztacHEaaarwUvXDx3Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 530 bytes --]

Philipp Stephani wrote:
> All of the current directory functions exhibit this behavior, including
> getwd and getcwd, so you need to make sure they are also covered.

Thanks for letting us know. I installed the 2nd attached patch, which addresses 
this by making the patch behave more like what you originally proposed, while 
still avoiding the need to use file_name_absolute_p (which is about Emacs file 
names, not OS names). Also, I noticed a related memory leak and fixed that by 
installing the 1st attached patch.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-src-xsmfns.c-x_session_initialize-Fix-memory-leak.patch --]
[-- Type: text/x-patch; name="0001-src-xsmfns.c-x_session_initialize-Fix-memory-leak.patch", Size: 933 bytes --]

From 2202952b8307f3a6407820280e94e4d979b7a122 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 7 Oct 2017 22:48:49 -0700
Subject: [PATCH 1/2] * src/xsmfns.c (x_session_initialize): Fix memory leak.

---
 src/xsmfns.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/xsmfns.c b/src/xsmfns.c
index 2cb4f3e..fb0d01b 100644
--- a/src/xsmfns.c
+++ b/src/xsmfns.c
@@ -401,12 +401,14 @@ x_session_initialize (struct x_display_info *dpyinfo)
   ptrdiff_t name_len = 0;
 
   /* libSM seems to crash if pwd is missing - see bug#18851.  */
-  if (! emacs_get_current_dir_name ())
+  char *pwd = emacs_get_current_dir_name ();
+  if (!pwd)
     {
       fprintf (stderr, "Disabling session management due to pwd error: %s\n",
                emacs_strerror (errno));
       return;
     }
+  xfree (pwd);
 
   ice_fd = -1;
   doing_interact = false;
-- 
2.7.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Improve-test-for-unreachable-dirs.patch --]
[-- Type: text/x-patch; name="0002-Improve-test-for-unreachable-dirs.patch", Size: 2460 bytes --]

From 7c2c117c91eeef5e7bd70c98cc7e201007016b1e Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 7 Oct 2017 22:56:29 -0700
Subject: [PATCH 2/2] Improve test for unreachable dirs

* src/sysdep.c (get_current_dir_name_or_unreachable):
New function, with most of the old contents of
emacs_get_current_dir_name.
(emacs_get_current_dir_name): Use it.  Use a simpler
test for unreachable directory strings, and also apply
it to getcwd etc. (Bug#27871)
---
 src/sysdep.c | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/sysdep.c b/src/sysdep.c
index 8291a60..c348492 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -221,9 +221,12 @@ init_standard_fds (void)
 }
 
 /* Return the current working directory.  The result should be freed
-   with 'free'.  Return NULL on errors.  */
-char *
-emacs_get_current_dir_name (void)
+   with 'free'.  Return NULL (setting errno) on errors.  If the
+   current directory is unreachable, return either NULL or a string
+   beginning with '('.  */
+
+static char *
+get_current_dir_name_or_unreachable (void)
 {
 # if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME
 #  ifdef HYBRID_MALLOC
@@ -233,16 +236,9 @@ emacs_get_current_dir_name (void)
 #  endif
   if (use_libc)
     {
-      /* GNU/Linux get_current_dir_name can return a string starting
-	 with "(unreachable)" (Bug#27871).  */
-      char *wd = get_current_dir_name ();
-      if (wd && ! (IS_DIRECTORY_SEP (*wd) || (*wd && IS_DEVICE_SEP (wd[1]))))
-	{
-	  free (wd);
-	  errno = ENOENT;
-	  return NULL;
-	}
-      return wd;
+      /* For an unreachable directory, this returns a string that starts
+	 with "(unreachable)"; see Bug#27871.  */
+      return get_current_dir_name ();
     }
 # endif
 
@@ -294,6 +290,23 @@ emacs_get_current_dir_name (void)
   return buf;
 }
 
+/* Return the current working directory.  The result should be freed
+   with 'free'.  Return NULL (setting errno) on errors; an unreachable
+   directory (e.g., its name starts with '(') counts as an error.  */
+
+char *
+emacs_get_current_dir_name (void)
+{
+  char *dir = get_current_dir_name_or_unreachable ();
+  if (dir && *dir == '(')
+    {
+      free (dir);
+      errno = ENOENT;
+      return NULL;
+    }
+  return dir;
+}
+
 \f
 /* Discard pending input on all input descriptors.  */
 
-- 
2.7.4


  reply	other threads:[~2017-10-08  6:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-29 21:06 bug#27871: 26.0.50; Bad handling of unmounted directory Philipp
2017-09-23 10:19 ` Philipp Stephani
2017-09-23 10:39   ` Eli Zaretskii
2017-09-23 11:30     ` Andreas Schwab
2017-09-23 11:33       ` Philipp Stephani
2017-09-23 11:38         ` Eli Zaretskii
2017-09-23 11:41           ` Eli Zaretskii
2017-09-30 18:49             ` Philipp Stephani
2017-09-30 18:50               ` bug#27871: [PATCH] Treat unreachable current directory as error Philipp Stephani
     [not found]               ` <20170930185006.54096-1-phst@google.com>
2017-10-01  0:00                 ` Paul Eggert
2017-10-05 10:13                   ` Eli Zaretskii
2017-10-05 23:06                     ` Paul Eggert
2017-10-07  8:49                   ` Philipp Stephani
2017-10-08  6:04                     ` Paul Eggert [this message]
2017-10-08 14:56                       ` Philipp Stephani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9b7b9905-dc97-feb4-9569-27372b1c6b5d@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=27871@debbugs.gnu.org \
    --cc=p.stephani2@gmail.com \
    --cc=phst@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).