From owner-svn-src-all@FreeBSD.ORG Thu Jul 14 18:38:11 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 35BA3106566C; Thu, 14 Jul 2011 18:38:11 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 261A48FC19; Thu, 14 Jul 2011 18:38:11 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p6EIcBX6041020; Thu, 14 Jul 2011 18:38:11 GMT (envelope-from glebius@svn.freebsd.org) Received: (from glebius@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p6EIcBtJ041018; Thu, 14 Jul 2011 18:38:11 GMT (envelope-from glebius@svn.freebsd.org) Message-Id: <201107141838.p6EIcBtJ041018@svn.freebsd.org> From: Gleb Smirnoff Date: Thu, 14 Jul 2011 18:38:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r224031 - head/sys/netgraph X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Jul 2011 18:38:11 -0000 Author: glebius Date: Thu Jul 14 18:38:10 2011 New Revision: 224031 URL: http://svn.freebsd.org/changeset/base/224031 Log: In ng_attach_cntl() first allocate things that may fail, and then do the rest of initialization. This simplifies code and fixes a double free in failure scenario. Reviewed by: bz Modified: head/sys/netgraph/ng_socket.c Modified: head/sys/netgraph/ng_socket.c ============================================================================== --- head/sys/netgraph/ng_socket.c Thu Jul 14 18:37:10 2011 (r224030) +++ head/sys/netgraph/ng_socket.c Thu Jul 14 18:38:10 2011 (r224031) @@ -525,33 +525,32 @@ ng_attach_cntl(struct socket *so) { struct ngsock *priv; struct ngpcb *pcbp; + node_p node; int error; - /* Allocate node private info */ - priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); - /* Setup protocol control block */ - if ((error = ng_attach_common(so, NG_CONTROL)) != 0) { - free(priv, M_NETGRAPH_SOCK); + if ((error = ng_attach_common(so, NG_CONTROL)) != 0) return (error); - } pcbp = sotongpcb(so); - /* Link the pcb the private data. */ - priv->ctlsock = pcbp; - pcbp->sockdata = priv; - priv->refs++; - - /* Initialize mutex. */ - mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF); - /* Make the generic node components */ - if ((error = ng_make_node_common(&typestruct, &priv->node)) != 0) { - free(priv, M_NETGRAPH_SOCK); + if ((error = ng_make_node_common(&typestruct, &node)) != 0) { ng_detach_common(pcbp, NG_CONTROL); return (error); } + /* Allocate node private info */ + priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); + + /* Initialize mutex. */ + mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF); + + /* Link the pcb the private data. */ + priv->ctlsock = pcbp; + pcbp->sockdata = priv; + priv->refs++; + priv->node = node; + /* Store a hint for netstat(1). */ priv->node_id = priv->node->nd_ID;