summaryrefslogtreecommitdiffstats
path: root/sys/netgraph
diff options
context:
space:
mode:
authorglebius <glebius@FreeBSD.org>2011-07-14 18:38:10 +0000
committerglebius <glebius@FreeBSD.org>2011-07-14 18:38:10 +0000
commit4055dcbe1cacf8618218d189236613d83963d5ee (patch)
treeb618d18ef0ae9cbf850622d012b2e65dfec5d7fd /sys/netgraph
parentea2a83a8daa00350e502581002584808808991f3 (diff)
downloadFreeBSD-src-4055dcbe1cacf8618218d189236613d83963d5ee.zip
FreeBSD-src-4055dcbe1cacf8618218d189236613d83963d5ee.tar.gz
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
Diffstat (limited to 'sys/netgraph')
-rw-r--r--sys/netgraph/ng_socket.c31
1 files changed, 15 insertions, 16 deletions
diff --git a/sys/netgraph/ng_socket.c b/sys/netgraph/ng_socket.c
index c5d887b..f72f822 100644
--- a/sys/netgraph/ng_socket.c
+++ b/sys/netgraph/ng_socket.c
@@ -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;
OpenPOWER on IntegriCloud