From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: YAMAMOTO Mitsuharu Newsgroups: gmane.emacs.bugs Subject: bug#32604: 26.1.50; memory leak in connect_network_socket Date: Thu, 06 Sep 2018 08:50:33 +0900 Organization: Faculty of Science, Chiba University Message-ID: References: <87k1o3rdgd.fsf@gmail.com> <0aad35d46cfe7a15d67484205d19340c.squirrel@weber.math.s.chiba-u.ac.jp> <87muswhmuj.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Trace: blaine.gmane.org 1536191347 1632 195.159.176.226 (5 Sep 2018 23:49:07 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 5 Sep 2018 23:49:07 +0000 (UTC) User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM/1.14.9 (=?UTF-8?Q?Goj=C5=8D?=) APEL/10.8 EasyPG/1.0.0 Emacs/25.3 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Cc: 32604@debbugs.gnu.org To: Noam Postavsky Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Sep 06 01:49:03 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fxhXi-0000JA-Nl for geb-bug-gnu-emacs@m.gmane.org; Thu, 06 Sep 2018 01:49:02 +0200 Original-Received: from localhost ([::1]:58605 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fxhZo-0008EC-VC for geb-bug-gnu-emacs@m.gmane.org; Wed, 05 Sep 2018 19:51:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:32797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fxhZi-0008Cw-D3 for bug-gnu-emacs@gnu.org; Wed, 05 Sep 2018 19:51:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fxhZe-0006Ev-SX for bug-gnu-emacs@gnu.org; Wed, 05 Sep 2018 19:51:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:42487) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fxhZe-0006Cw-Im for bug-gnu-emacs@gnu.org; Wed, 05 Sep 2018 19:51:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fxhZe-0004hA-Cr for bug-gnu-emacs@gnu.org; Wed, 05 Sep 2018 19:51:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: YAMAMOTO Mitsuharu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 05 Sep 2018 23:51:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 32604 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 32604-submit@debbugs.gnu.org id=B32604.153619143818019 (code B ref 32604); Wed, 05 Sep 2018 23:51:02 +0000 Original-Received: (at 32604) by debbugs.gnu.org; 5 Sep 2018 23:50:38 +0000 Original-Received: from localhost ([127.0.0.1]:47505 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fxhZG-0004gZ-2m for submit@debbugs.gnu.org; Wed, 05 Sep 2018 19:50:38 -0400 Original-Received: from mathmail.math.s.chiba-u.ac.jp ([133.82.132.2]:54056) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fxhZE-0004gO-1B for 32604@debbugs.gnu.org; Wed, 05 Sep 2018 19:50:37 -0400 Original-Received: from mathent.math.s.chiba-u.ac.jp (mathent [192.168.32.5]) by mathmail.math.s.chiba-u.ac.jp (Postfix) with ESMTP id 721D3F08F8; Thu, 6 Sep 2018 08:50:33 +0900 (JST) (envelope-from mituharu@math.s.chiba-u.ac.jp) In-Reply-To: <87muswhmuj.fsf@gmail.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:150064 Archived-At: 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