From 4a2bfd249d06e229b080cb6f50a68086a8aa1e52 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Sun, 11 Apr 2021 13:45:19 +1200 Subject: [PATCH] dns scavenging: avoid useless copy of msg Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- source4/dsdb/kcc/scavenge_dns_records.c | 33 ++++++------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/source4/dsdb/kcc/scavenge_dns_records.c b/source4/dsdb/kcc/scavenge_dns_records.c index 6084808ca34..db341364902 100644 --- a/source4/dsdb/kcc/scavenge_dns_records.c +++ b/source4/dsdb/kcc/scavenge_dns_records.c @@ -97,7 +97,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, struct ldb_message_element *tombstone_el = NULL; struct ldb_message_element *old_el = NULL; struct ldb_message *new_msg = NULL; - struct ldb_message *old_msg = NULL; enum ndr_err_code ndr_err; int ret; struct GUID guid; @@ -178,28 +177,21 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, * change. This prevents race conditions. */ for (i = 0; i < res->count; i++) { - old_msg = ldb_msg_copy(mem_ctx, res->msgs[i]); - if (old_msg == NULL) { - return NT_STATUS_INTERNAL_ERROR; - } - - old_el = ldb_msg_find_element(old_msg, "dnsRecord"); - if (old_el == NULL) { - TALLOC_FREE(old_msg); - return NT_STATUS_INTERNAL_ERROR; - } - - old_el->flags = LDB_FLAG_MOD_DELETE; - new_msg = ldb_msg_copy(mem_ctx, old_msg); + new_msg = ldb_msg_copy(mem_ctx, res->msgs[i]); if (new_msg == NULL) { - TALLOC_FREE(old_msg); return NT_STATUS_INTERNAL_ERROR; } + old_el = ldb_msg_find_element(new_msg, "dnsRecord"); + if (old_el == NULL) { + TALLOC_FREE(new_msg); + return NT_STATUS_INTERNAL_ERROR; + } + old_el->flags = LDB_FLAG_MOD_DELETE; + ret = ldb_msg_add_empty( new_msg, "dnsRecord", LDB_FLAG_MOD_ADD, &el); if (ret != LDB_SUCCESS) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); return NT_STATUS_INTERNAL_ERROR; } @@ -207,7 +199,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, status = copy_current_records(mem_ctx, old_el, el, dns_timestamp); if (!NT_STATUS_IS_OK(status)) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); return NT_STATUS_INTERNAL_ERROR; } @@ -215,7 +206,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, /* If nothing was expired, do nothing. */ if (el->num_values == old_el->num_values && el->num_values != 0) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); continue; } @@ -238,7 +228,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, struct ldb_val, 1); if (!vals) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); return NT_STATUS_INTERNAL_ERROR; } @@ -255,7 +244,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, &true_val, &tombstone_el); if (ret != LDB_SUCCESS) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); return NT_STATUS_INTERNAL_ERROR; } @@ -268,7 +256,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, struct ldb_val, 1); if (!vals) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); return NT_STATUS_INTERNAL_ERROR; } @@ -290,7 +277,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, /* Set DN to the GUID in case the object was moved. */ el = ldb_msg_find_element(new_msg, "objectGUID"); if (el == NULL) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); *error_string = talloc_asprintf(mem_ctx, @@ -302,7 +288,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, status = GUID_from_ndr_blob(el->values, &guid); if (!NT_STATUS_IS_OK(status)) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); *error_string = discard_const_p(char, "Error: Invalid GUID.\n"); @@ -318,7 +303,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, ret = ldb_modify(samdb, new_msg); if (ret != LDB_SUCCESS) { - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); *error_string = talloc_asprintf(mem_ctx, @@ -328,7 +312,6 @@ static NTSTATUS dns_tombstone_records_zone(TALLOC_CTX *mem_ctx, ldb_errstring(samdb)); return NT_STATUS_INTERNAL_ERROR; } - TALLOC_FREE(old_msg); TALLOC_FREE(new_msg); }