BUG/MINOR: connections: Make sure we free the connection on failure.
In connect_server(), make sure we properly free a newly created connection
if we somehow fail, and it has not yet been attached to a conn_stream, or
it would lead to a memory leak.
This should appease coverity for backend.c, as reported in inssue #556.
This should be backported to 2.1, 2.0 and 1.9
(cherry picked from commit c0caac2cc8f51c5802f0128a4ceb0c73ff601ead)
[wt: minor ctx adjustment]
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit b82d6ef70bb0a757631549cdc889f9b138da5f09)
[wt: adjusted context, no address allocation in 2.0]
Signed-off-by: Willy Tarreau <w@1wt.eu>
diff --git a/src/backend.c b/src/backend.c
index 160a2aa..ae0943e 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -1477,13 +1477,18 @@
}
}
- if (!srv_conn)
+ if (!srv_conn) {
+ if (srv_conn)
+ conn_free(srv_conn);
return SF_ERR_RESOURCE;
+ }
if (!(s->flags & SF_ADDR_SET)) {
err = assign_server_address(s, srv_conn);
- if (err != SRV_STATUS_OK)
+ if (err != SRV_STATUS_OK) {
+ conn_free(srv_conn);
return SF_ERR_INTERNAL;
+ }
}
if (!conn_xprt_ready(srv_conn) && !srv_conn->mux) {
@@ -1493,11 +1498,15 @@
else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
/* proxies exclusively run on raw_sock right now */
conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), xprt_get(XPRT_RAW));
- if (!(srv_conn->ctrl))
+ if (!(srv_conn->ctrl)) {
+ conn_free(srv_conn);
return SF_ERR_INTERNAL;
+ }
}
- else
+ else {
+ conn_free(srv_conn);
return SF_ERR_INTERNAL; /* how did we get there ? */
+ }
#if defined(USE_OPENSSL) && defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
if (!srv ||