Subject: Re: bug#26628: [PATCH] Fix memory leak of cwd string in emacsclient

From: Paul Eggert <[email-masked]>
Date: Wed, 31 May 2017 23:30:53 -0700

Generally speaking we don't bother freeing storage just before program exit 
merely to pacify AddressSanitizer, as that makes the program less efficient and 
is contrary to the goal of leak checking which is to increase efficiency. Here, 
though, we can free storage earlier, and this might have a point since the 
storage can get reused. So I installed the attached further patch.

> I'm aware that I can set ASAN_OPTIONS=detect_leaks=0 in the environment

It might not hurt to do that, if only to prevent our hassling with false alarms.
From 877e808440d4bc2e62d6fb509defee91a3fdc895 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 31 May 2017 22:38:04 -0700
Subject: [PATCH] Free cwd when no longer needed
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib-src/emacsclient.c (main): Don’t dally when freeing cwd.
---
 lib-src/emacsclient.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 3a0715f..8828b76 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -208,7 +208,7 @@ char *get_current_dir_name (void);
 /* Return the current working directory.  Returns NULL on errors.
    Any other returned value must be freed with free.  This is used
    only when get_current_dir_name is not defined on the system.  */
-char*
+char *
 get_current_dir_name (void)
 {
   char *buf;
@@ -1702,6 +1702,7 @@ main (int argc, char **argv)
   if (tramp_prefix)
     quote_argument (emacs_socket, tramp_prefix);
   quote_argument (emacs_socket, cwd);
+  free (cwd);
   send_to_emacs (emacs_socket, "/");
   send_to_emacs (emacs_socket, " ");
 
@@ -1945,8 +1946,6 @@ main (int argc, char **argv)
   if (rl < 0)
     exit_status = EXIT_FAILURE;
 
-  free (cwd);                   /* Keep leak checkers happy.  */
-
   CLOSE_SOCKET (emacs_socket);
   return exit_status;
 }
-- 
2.7.4