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);
/* 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);
#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;
#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)
Lisp_Object data = get_file_errno_data (err, contact, xerrno);
pset_status (p, list2 (Fcar (data), Fcdr (data)));
+ unbind_to (count, Qnil);
return;
}
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);
}
#endif
+ unbind_to (count, Qnil);
}
/* Create a network stream/datagram client/server process. Treated