Commit 82160cf0 authored by YAMAMOTO Mitsuharu's avatar YAMAMOTO Mitsuharu

* src/process.c (connect_network_socket): Fix memory leak. (Bug#32604)

parent 6c616e46
...@@ -3321,11 +3321,9 @@ static void ...@@ -3321,11 +3321,9 @@ static void
connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
Lisp_Object use_external_socket_p) Lisp_Object use_external_socket_p)
{ {
ptrdiff_t count = SPECPDL_INDEX ();
int s = -1, outch, inch; int s = -1, outch, inch;
int xerrno = 0; int xerrno = 0;
int family; int family;
struct sockaddr *sa = NULL;
int ret; int ret;
ptrdiff_t addrlen; ptrdiff_t addrlen;
struct Lisp_Process *p = XPROCESS (proc); struct Lisp_Process *p = XPROCESS (proc);
...@@ -3344,6 +3342,11 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3344,6 +3342,11 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
/* Do this in case we never enter the while-loop below. */ /* Do this in case we never enter the while-loop below. */
s = -1; s = -1;
struct sockaddr *sa = NULL;
ptrdiff_t count = SPECPDL_INDEX ();
record_unwind_protect_nothing ();
ptrdiff_t count1 = SPECPDL_INDEX ();
while (!NILP (addrinfos)) while (!NILP (addrinfos))
{ {
Lisp_Object addrinfo = XCAR (addrinfos); Lisp_Object addrinfo = XCAR (addrinfos);
...@@ -3356,9 +3359,8 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3356,9 +3359,8 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
#endif #endif
addrlen = get_lisp_to_sockaddr_size (ip_address, &family); addrlen = get_lisp_to_sockaddr_size (ip_address, &family);
if (sa) sa = xrealloc (sa, addrlen);
free (sa); set_unwind_protect_ptr (count, xfree, sa);
sa = xmalloc (addrlen);
conv_lisp_to_sockaddr (family, ip_address, sa, addrlen); conv_lisp_to_sockaddr (family, ip_address, sa, addrlen);
s = socket_to_use; s = socket_to_use;
...@@ -3520,7 +3522,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3520,7 +3522,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
#endif /* !WINDOWSNT */ #endif /* !WINDOWSNT */
/* Discard the unwind protect closing S. */ /* Discard the unwind protect closing S. */
specpdl_ptr = specpdl + count; specpdl_ptr = specpdl + count1;
emacs_close (s); emacs_close (s);
s = -1; s = -1;
if (0 <= socket_to_use) if (0 <= socket_to_use)
...@@ -3591,6 +3593,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3591,6 +3593,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
Lisp_Object data = get_file_errno_data (err, contact, xerrno); Lisp_Object data = get_file_errno_data (err, contact, xerrno);
pset_status (p, list2 (Fcar (data), Fcdr (data))); pset_status (p, list2 (Fcar (data), Fcdr (data)));
unbind_to (count, Qnil);
return; return;
} }
...@@ -3610,7 +3613,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3610,7 +3613,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
p->outfd = outch; p->outfd = outch;
/* Discard the unwind protect for closing S, if any. */ /* 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) if (p->is_server && p->socktype != SOCK_DGRAM)
pset_status (p, Qlisten); pset_status (p, Qlisten);
...@@ -3671,6 +3674,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos, ...@@ -3671,6 +3674,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
} }
#endif #endif
unbind_to (count, Qnil);
} }
/* Create a network stream/datagram client/server process. Treated /* Create a network stream/datagram client/server process. Treated
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment