Fix double init of protoctx, memory leak

Free vars where they are allocated, always
pull/13/head
Soner Tari 6 years ago
parent 83468afb1f
commit d0687b3398

@ -679,7 +679,6 @@ protoautossl_setup(pxy_conn_ctx_t *ctx)
ctx->protoctx->arg = malloc(sizeof(protoautossl_ctx_t));
if (!ctx->protoctx->arg) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->protoctx->arg, 0, sizeof(protoautossl_ctx_t));
@ -689,7 +688,6 @@ protoautossl_setup(pxy_conn_ctx_t *ctx)
ctx->sslctx = malloc(sizeof(ssl_ctx_t));
if (!ctx->sslctx) {
free(ctx->protoctx->arg);
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->sslctx, 0, sizeof(ssl_ctx_t));

@ -831,7 +831,6 @@ protohttp_setup(pxy_conn_ctx_t *ctx)
ctx->protoctx->arg = malloc(sizeof(protohttp_ctx_t));
if (!ctx->protoctx->arg) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->protoctx->arg, 0, sizeof(protohttp_ctx_t));
@ -853,7 +852,6 @@ protohttps_setup(pxy_conn_ctx_t *ctx)
ctx->protoctx->arg = malloc(sizeof(protohttp_ctx_t));
if (!ctx->protoctx->arg) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->protoctx->arg, 0, sizeof(protohttp_ctx_t));
@ -861,7 +859,6 @@ protohttps_setup(pxy_conn_ctx_t *ctx)
ctx->sslctx = malloc(sizeof(ssl_ctx_t));
if (!ctx->sslctx) {
free(ctx->protoctx->arg);
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->sslctx, 0, sizeof(ssl_ctx_t));
@ -880,7 +877,6 @@ protohttp_setup_child(pxy_conn_child_ctx_t *ctx)
ctx->protoctx->arg = malloc(sizeof(protohttp_ctx_t));
if (!ctx->protoctx->arg) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->protoctx->arg, 0, sizeof(protohttp_ctx_t));
@ -901,7 +897,6 @@ protohttps_setup_child(pxy_conn_child_ctx_t *ctx)
ctx->protoctx->arg = malloc(sizeof(protohttp_ctx_t));
if (!ctx->protoctx->arg) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->protoctx->arg, 0, sizeof(protohttp_ctx_t));

@ -1426,7 +1426,6 @@ protossl_setup(pxy_conn_ctx_t *ctx)
ctx->sslctx = malloc(sizeof(ssl_ctx_t));
if (!ctx->sslctx) {
free(ctx->protoctx);
return PROTO_ERROR;
}
memset(ctx->sslctx, 0, sizeof(ssl_ctx_t));

@ -87,31 +87,37 @@ pxy_setup_proto(pxy_conn_ctx_t *ctx)
// Default to tcp
prototcp_setup(ctx);
protocol_t rv;
if (ctx->spec->upgrade) {
return protoautossl_setup(ctx);
rv = protoautossl_setup(ctx);
} else if (ctx->spec->http) {
if (ctx->spec->ssl) {
return protohttps_setup(ctx);
rv = protohttps_setup(ctx);
} else {
return protohttp_setup(ctx);
rv = protohttp_setup(ctx);
}
} else if (ctx->spec->pop3) {
if (ctx->spec->ssl) {
return (protossl_setup(ctx) != PROTO_ERROR) ? PROTO_POP3S : PROTO_ERROR;
rv = (protossl_setup(ctx) != PROTO_ERROR) ? PROTO_POP3S : PROTO_ERROR;
} else {
return PROTO_POP3;
rv = PROTO_POP3;
}
} else if (ctx->spec->smtp) {
if (ctx->spec->ssl) {
return (protossl_setup(ctx) != PROTO_ERROR) ? PROTO_SMTPS : PROTO_ERROR;
rv = (protossl_setup(ctx) != PROTO_ERROR) ? PROTO_SMTPS : PROTO_ERROR;
} else {
return PROTO_SMTP;
rv = PROTO_SMTP;
}
} else if (ctx->spec->ssl) {
return protossl_setup(ctx);
rv = protossl_setup(ctx);
} else {
return PROTO_TCP;
rv = PROTO_TCP;
}
if (rv == PROTO_ERROR) {
free(ctx->protoctx);
}
return rv;
}
static protocol_t NONNULL(1)
@ -126,31 +132,37 @@ pxy_setup_proto_child(pxy_conn_child_ctx_t *ctx)
// Default to tcp
prototcp_setup_child(ctx);
protocol_t rv;
if (ctx->conn->spec->upgrade) {
return protoautossl_setup_child(ctx);
rv = protoautossl_setup_child(ctx);
} else if (ctx->conn->spec->http) {
if (ctx->conn->spec->ssl) {
return protohttps_setup_child(ctx);
rv = protohttps_setup_child(ctx);
} else {
return protohttp_setup_child(ctx);
rv = protohttp_setup_child(ctx);
}
} else if (ctx->conn->spec->pop3) {
if (ctx->conn->spec->ssl) {
return (protossl_setup_child(ctx) != PROTO_ERROR) ? PROTO_POP3S : PROTO_ERROR;
rv = (protossl_setup_child(ctx) != PROTO_ERROR) ? PROTO_POP3S : PROTO_ERROR;
} else {
return PROTO_POP3;
rv = PROTO_POP3;
}
} else if (ctx->conn->spec->smtp) {
if (ctx->conn->spec->ssl) {
return (protossl_setup_child(ctx) != PROTO_ERROR) ? PROTO_SMTPS : PROTO_ERROR;
rv = (protossl_setup_child(ctx) != PROTO_ERROR) ? PROTO_SMTPS : PROTO_ERROR;
} else {
return PROTO_SMTP;
rv = PROTO_SMTP;
}
} else if (ctx->conn->spec->ssl) {
return protossl_setup_child(ctx);
rv = protossl_setup_child(ctx);
} else {
return PROTO_TCP;
rv = PROTO_TCP;
}
if (rv == PROTO_ERROR) {
free(ctx->protoctx);
}
return rv;
}
static pxy_conn_ctx_t * MALLOC NONNULL(2,3,4)
@ -183,20 +195,10 @@ pxy_conn_ctx_new(evutil_socket_t fd,
ctx->thrmgr = thrmgr;
ctx->spec = spec;
ctx->protoctx = malloc(sizeof(proto_ctx_t));
if (!ctx->protoctx) {
log_err_level_printf(LOG_CRIT, "Error allocating memory\n");
evutil_closesocket(fd);
free(ctx);
return NULL;
}
memset(ctx->protoctx, 0, sizeof(proto_ctx_t));
ctx->proto = pxy_setup_proto(ctx);
if (ctx->proto == PROTO_ERROR) {
log_err_level_printf(LOG_CRIT, "Error allocating memory\n");
evutil_closesocket(fd);
free(ctx->protoctx);
free(ctx);
return NULL;
}
@ -236,20 +238,10 @@ pxy_conn_ctx_new_child(evutil_socket_t fd, pxy_conn_ctx_t *conn)
ctx->fd = fd;
ctx->conn = conn;
ctx->protoctx = malloc(sizeof(proto_ctx_t));
if (!ctx->protoctx) {
log_err_level_printf(LOG_CRIT, "Error allocating memory\n");
evutil_closesocket(fd);
free(ctx);
return NULL;
}
memset(ctx->protoctx, 0, sizeof(proto_ctx_t));
ctx->proto = pxy_setup_proto_child(ctx);
if (ctx->proto == PROTO_ERROR) {
log_err_level_printf(LOG_CRIT, "Error allocating memory\n");
evutil_closesocket(fd);
free(ctx->protoctx);
free(ctx);
return NULL;
}

Loading…
Cancel
Save