From: YAMAMOTO Mitsuharu Date: Thu, 6 Sep 2018 23:35:36 +0000 (+0900) Subject: * src/process.c (connect_network_socket): Fix memory leak. (Bug#32604) X-Git-Tag: emacs-26.1.90~168 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=82160cf0c16e22bddfd90254e4a4e03ed383c9ae;p=emacs.git * src/process.c (connect_network_socket): Fix memory leak. (Bug#32604) --- diff --git a/src/process.c b/src/process.c index 676f38446e4..b0a327229c6 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