MEDIUM: connection: make sure all address producers allocate their address

This commit places calls to sockaddr_alloc() at the places where an address
is needed, and makes sure that the allocation is properly tested. This does
not add too many error paths since connection allocations are already in the
vicinity and share the same error paths. For the two cases where a
clear_addr() was called, instead the address was not allocated.
diff --git a/src/backend.c b/src/backend.c
index 18d2498..dcf3642 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -826,7 +826,8 @@
 
 	DPRINTF(stderr,"assign_server_address : s=%p\n",s);
 
-	/* FIXME WTA: an address allocation will soon be needed here */
+	if (!sockaddr_alloc(&srv_conn->dst))
+		return SRV_STATUS_INTERNAL;
 
 	if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
 		/* A server is necessarily known for this stream */
@@ -1039,7 +1040,8 @@
 	else
 		return;
 
-	/* FIXME WTA: an address allocation will soon be needed here for src */
+	if (!sockaddr_alloc(&srv_conn->src))
+		return;
 
 	switch (src->opts & CO_SRC_TPROXY_MASK) {
 	case CO_SRC_TPROXY_ADDR:
diff --git a/src/checks.c b/src/checks.c
index c2fc87c..61acb17 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -1614,7 +1614,9 @@
 	/* Maybe there were an older connection we were waiting on */
 	check->wait_list.events = 0;
 
-	/* FIXME WTA: we'll have to dynamically allocate the dst address here */
+	if (!sockaddr_alloc(&conn->dst))
+		return SF_ERR_RESOURCE;
+
 	if (is_addr(&check->addr)) {
 		/* we'll connect to the check addr specified on the server */
 		*conn->dst = check->addr;
@@ -1643,10 +1645,6 @@
 	}
 
 	/* no client address */
-	/* FIXME WTA: we'll have to dynamically allocate the src address here
-	 * before clearing it, or better release it and make it null.
-	 */
-	clear_addr(conn->src);
 
 	conn_prepare(conn, proto, check->xprt);
 	if (conn_install_mux(conn, &mux_pt_ops, cs, s->proxy, NULL) < 0)
@@ -2862,12 +2860,12 @@
 			conn->target = s ? &s->obj_type : &proxy->obj_type;
 
 			/* no client address */
-			/* FIXME WTA: we'll have to dynamically allocate the src address here
-			 * before clearing it, or better release it and make it null.
-			 */
-			clear_addr(conn->src);
+
+			if (!sockaddr_alloc(&conn->dst)) {
+				ret = SF_ERR_RESOURCE;
+				goto fail_check;
+			}
 
-			/* FIXME WTA: we'll have to dynamically allocate the dst address here */
 			if (is_addr(&check->addr)) {
 				/* we'll connect to the check addr specified on the server */
 				*conn->dst = check->addr;
diff --git a/src/connection.c b/src/connection.c
index 0277309..602fc79 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -402,6 +402,9 @@
 	if (!conn_ctrl_ready(conn))
 		goto fail;
 
+	if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
+		goto fail;
+
 	if (!fd_recv_ready(conn->handle.fd))
 		goto not_ready;
 
diff --git a/src/hlua.c b/src/hlua.c
index 150faec..49f056c 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -2461,7 +2461,11 @@
 		WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
 	}
 
-	/* FIXME WTA: dst address allocation needed here! */
+	if (!sockaddr_alloc(&conn->dst)) {
+		xref_unlock(&socket->xref, peer);
+		WILL_LJMP(luaL_error(L, "connect: internal error"));
+	}
+
 	memcpy(conn->dst, addr, sizeof(struct sockaddr_storage));
 
 	/* Set port. */
diff --git a/src/http_ana.c b/src/http_ana.c
index c5c928d..782c168 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -741,7 +741,8 @@
 		struct ist uri, path;
 
 		/* Note that for now we don't reuse existing proxy connections */
-		if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
+		if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL ||
+			     !sockaddr_alloc(&conn->dst))) {
 			txn->req.err_state = txn->req.msg_state;
 			txn->req.msg_state = HTTP_MSG_ERROR;
 			txn->status = 500;
@@ -759,7 +760,6 @@
 		uri = htx_sl_req_uri(sl);
 		path = http_get_path(uri);
 
-		/* FIXME WTA: below we'll need to dynamically allocate the dst address */
 		if (url2sa(uri.ptr, uri.len - path.len, conn->dst, NULL) == -1)
 			goto return_bad_req;
 
diff --git a/src/peers.c b/src/peers.c
index 193ef0e..8fa6a8e 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -2539,7 +2539,9 @@
 
 	conn->target = s->target = peer_session_target(peer, s);
 
-	/* FIXME WTA: a sockaddr allocation will be needed here */
+	if (!sockaddr_alloc(&conn->dst))
+		goto out_free_cs;
+
 	memcpy(conn->dst, &peer->addr, sizeof(*conn->dst));
 
 	conn_prepare(conn, peer->proto, peer_xprt(peer));
diff --git a/src/session.c b/src/session.c
index 782d488..7def387 100644
--- a/src/session.c
+++ b/src/session.c
@@ -155,8 +155,10 @@
 	if (unlikely((cli_conn = conn_new()) == NULL))
 		goto out_close;
 
+	if (!sockaddr_alloc(&cli_conn->src))
+		goto out_free_conn;
+
 	cli_conn->handle.fd = cfd;
-	/* FIXME WTA: an allocation will be needed here. Better steal the original address on success */
 	*cli_conn->src = *addr;
 	cli_conn->flags |= CO_FL_ADDR_FROM_SET;
 	cli_conn->target = &l->obj_type;