bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#32604: 26.1.50; memory leak in connect_network_socket


From: YAMAMOTO Mitsuharu
Subject: bug#32604: 26.1.50; memory leak in connect_network_socket
Date: Thu, 06 Sep 2018 08:50:33 +0900
User-agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM/1.14.9 (Gojō) APEL/10.8 EasyPG/1.0.0 Emacs/25.3 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO)

On Wed, 05 Sep 2018 08:19:48 +0900,
Noam Postavsky wrote:

> 
> Looks good to me; a couple of minor suggestions below.
> 
> mituharu@math.s.chiba-u.ac.jp writes:
> 
> > @@ -3322,6 +3322,7 @@ connect_network_socket (Lisp_Object proc,
> > Lisp_Object addrinfos,
> >                          Lisp_Object use_external_socket_p)
> >  {
> >    ptrdiff_t count = SPECPDL_INDEX ();
> > +  ptrdiff_t count1 UNINIT;
> >    int s = -1, outch, inch;
> >    int xerrno = 0;
> >    int family;
> > @@ -3344,6 +3345,9 @@ connect_network_socket (Lisp_Object proc,
> > Lisp_Object addrinfos,
> >    /* Do this in case we never enter the while-loop below.  */
> >    s = -1;
> >
> > +  record_unwind_protect_nothing ();
> > +  count1 = SPECPDL_INDEX ();
> 
> Since we assume a C99 compiler now, you could just do
> 
>     ptrdiff_t count1 = SPECPDL_INDEX ();
> 
> without having the UNINIT thing.  Also, since free is harmless on a NULL
> pointer, you could just record an unwind protect at the top once,
> without having the nothing state, I think.

Thanks for the comments.  With C99, I would probably gather related
things like:

  struct sockaddr *sa = NULL;
  ptrdiff_t count = SPECPDL_INDEX ();
  record_unwind_protect_nothing ();
        :
  while (!NILP (addrinfos))
    {
        :
      sa = xrealloc (sa, addrlen);
      set_unwind_protect_ptr (count, xfree, sa);
        :
    }
        :
  unbind_to (count, Qnil);

This looks much more idiomatic.  We need to update specbinding
according to (potential) change of the value of `sa' by xrealloc call.

                                     YAMAMOTO Mitsuharu
                                mituharu@math.s.chiba-u.ac.jp
   
diff --git a/src/process.c b/src/process.c
index 676f38446e..b0a327229c 100644
--- a/src/process.c
+++ b/src/process.c
@@ -3321,11 +3321,9 @@ static void
 connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
                         Lisp_Object use_external_socket_p)
 {
-  ptrdiff_t count = SPECPDL_INDEX ();
   int s = -1, outch, inch;
   int xerrno = 0;
   int family;
-  struct sockaddr *sa = NULL;
   int ret;
   ptrdiff_t addrlen;
   struct Lisp_Process *p = XPROCESS (proc);
@@ -3344,6 +3342,11 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
   /* Do this in case we never enter the while-loop below.  */
   s = -1;
 
+  struct sockaddr *sa = NULL;
+  ptrdiff_t count = SPECPDL_INDEX ();
+  record_unwind_protect_nothing ();
+  ptrdiff_t count1 = SPECPDL_INDEX ();
+
   while (!NILP (addrinfos))
     {
       Lisp_Object addrinfo = XCAR (addrinfos);
@@ -3356,9 +3359,8 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
 #endif
 
       addrlen = get_lisp_to_sockaddr_size (ip_address, &family);
-      if (sa)
-       free (sa);
-      sa = xmalloc (addrlen);
+      sa = xrealloc (sa, addrlen);
+      set_unwind_protect_ptr (count, xfree, sa);
       conv_lisp_to_sockaddr (family, ip_address, sa, addrlen);
 
       s = socket_to_use;
@@ -3520,7 +3522,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
 #endif /* !WINDOWSNT */
 
       /* Discard the unwind protect closing S.  */
-      specpdl_ptr = specpdl + count;
+      specpdl_ptr = specpdl + count1;
       emacs_close (s);
       s = -1;
       if (0 <= socket_to_use)
@@ -3591,6 +3593,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
          Lisp_Object data = get_file_errno_data (err, contact, xerrno);
 
          pset_status (p, list2 (Fcar (data), Fcdr (data)));
+         unbind_to (count, Qnil);
          return;
        }
 
@@ -3610,7 +3613,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
   p->outfd = outch;
 
   /* Discard the unwind protect for closing S, if any.  */
-  specpdl_ptr = specpdl + count;
+  specpdl_ptr = specpdl + count1;
 
   if (p->is_server && p->socktype != SOCK_DGRAM)
     pset_status (p, Qlisten);
@@ -3671,6 +3674,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object 
addrinfos,
     }
 #endif
 
+  unbind_to (count, Qnil);
 }
 
 /* Create a network stream/datagram client/server process.  Treated





reply via email to

[Prev in Thread] Current Thread [Next in Thread]