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
This commit is contained in:
Gleb Smirnoff 2011-07-14 18:38:10 +00:00
parent b8ea56d7e4
commit 0a7d45d349

View File

@ -525,32 +525,31 @@ ng_attach_cntl(struct socket *so)
{ {
struct ngsock *priv; struct ngsock *priv;
struct ngpcb *pcbp; struct ngpcb *pcbp;
node_p node;
int error; int error;
/* Setup protocol control block */
if ((error = ng_attach_common(so, NG_CONTROL)) != 0)
return (error);
pcbp = sotongpcb(so);
/* Make the generic node components */
if ((error = ng_make_node_common(&typestruct, &node)) != 0) {
ng_detach_common(pcbp, NG_CONTROL);
return (error);
}
/* Allocate node private info */ /* Allocate node private info */
priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO); priv = malloc(sizeof(*priv), M_NETGRAPH_SOCK, M_WAITOK | M_ZERO);
/* Setup protocol control block */ /* Initialize mutex. */
if ((error = ng_attach_common(so, NG_CONTROL)) != 0) { mtx_init(&priv->mtx, "ng_socket", NULL, MTX_DEF);
free(priv, M_NETGRAPH_SOCK);
return (error);
}
pcbp = sotongpcb(so);
/* Link the pcb the private data. */ /* Link the pcb the private data. */
priv->ctlsock = pcbp; priv->ctlsock = pcbp;
pcbp->sockdata = priv; pcbp->sockdata = priv;
priv->refs++; priv->refs++;
priv->node = node;
/* 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);
ng_detach_common(pcbp, NG_CONTROL);
return (error);
}
/* Store a hint for netstat(1). */ /* Store a hint for netstat(1). */
priv->node_id = priv->node->nd_ID; priv->node_id = priv->node->nd_ID;