From: Stephan Bosch Date: Thu, 1 Nov 2018 00:25:10 +0000 (+0100) Subject: lib-smtp: server: recipient: Add reference counting. X-Git-Tag: 2.3.5~55 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ce9373c8dca5b265fb030ff08328f5fb17cda9d9;p=thirdparty%2Fdovecot%2Fcore.git lib-smtp: server: recipient: Add reference counting. Unlike the server command, this is not strictly necessary for the recipient object, but we add this anyway to prevent future problems when the recipient implementation becomes more complex. --- diff --git a/src/lib-smtp/smtp-server-private.h b/src/lib-smtp/smtp-server-private.h index cc75320d4e..8181a41fb8 100644 --- a/src/lib-smtp/smtp-server-private.h +++ b/src/lib-smtp/smtp-server-private.h @@ -113,6 +113,7 @@ struct smtp_server_command { struct smtp_server_recipient_private { struct smtp_server_recipient rcpt; + int refcount; struct smtp_server_recipient_hook *hooks_head, *hooks_tail; }; @@ -374,6 +375,8 @@ void smtp_server_connection_set_proxy_data(struct smtp_server_connection *conn, struct smtp_server_recipient * smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd, const struct smtp_address *rcpt_to); +void smtp_server_recipient_ref(struct smtp_server_recipient *rcpt); +bool smtp_server_recipient_unref(struct smtp_server_recipient **_rcpt); void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt); void smtp_server_recipient_approved(struct smtp_server_recipient *rcpt); diff --git a/src/lib-smtp/smtp-server-recipient.c b/src/lib-smtp/smtp-server-recipient.c index 6d99e603ec..76080149d3 100644 --- a/src/lib-smtp/smtp-server-recipient.c +++ b/src/lib-smtp/smtp-server-recipient.c @@ -19,6 +19,7 @@ smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd, pool = pool_alloconly_create("smtp server recipient", 512); prcpt = p_new(pool, struct smtp_server_recipient_private, 1); + prcpt->refcount = 1; prcpt->rcpt.pool = pool; prcpt->rcpt.conn = cmd->conn; prcpt->rcpt.cmd = cmd; @@ -27,19 +28,40 @@ smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd, return &prcpt->rcpt; } -void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt) +void smtp_server_recipient_ref(struct smtp_server_recipient *rcpt) +{ + struct smtp_server_recipient_private *prcpt = + (struct smtp_server_recipient_private *)rcpt; + + i_assert(prcpt->refcount > 0); + prcpt->refcount++; +} + +bool smtp_server_recipient_unref(struct smtp_server_recipient **_rcpt) { struct smtp_server_recipient *rcpt = *_rcpt; + struct smtp_server_recipient_private *prcpt = + (struct smtp_server_recipient_private *)rcpt; *_rcpt = NULL; if (rcpt == NULL) - return; + return FALSE; + + i_assert(prcpt->refcount > 0); + if (--prcpt->refcount > 0) + return TRUE; smtp_server_recipient_call_hooks( rcpt, SMTP_SERVER_RECIPIENT_HOOK_DESTROY); pool_unref(&rcpt->pool); + return FALSE; +} + +void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt) +{ + smtp_server_recipient_unref(_rcpt); } void smtp_server_recipient_approved(struct smtp_server_recipient *rcpt)