From 9de59c7e3f01c578831a8e352ff8e9ee2312c77f Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 7 Aug 2015 08:27:19 +0200 Subject: [PATCH] dns_server: Consolidate talloc_realloc This puts the talloc_realloc into add_response_rr instead of before create_response_rr. It is a bit less efficient, but as we do not expect hundreds of answers, I think this code is a bit easier to understand. Signed-off-by: Volker Lendecke Reviewed-by: Kai Blin --- source4/dns_server/dns_query.c | 41 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/source4/dns_server/dns_query.c b/source4/dns_server/dns_query.c index 956898e580f..fb29e8fca68 100644 --- a/source4/dns_server/dns_query.c +++ b/source4/dns_server/dns_query.c @@ -40,15 +40,24 @@ #undef DBGC_CLASS #define DBGC_CLASS DBGC_DNS -static WERROR create_response_rr(const char *name, - const struct dnsp_DnssrvRpcRecord *rec, - struct dns_res_rec **answers, uint16_t *ancount) +static WERROR add_response_rr(TALLOC_CTX *mem_ctx, const char *name, + const struct dnsp_DnssrvRpcRecord *rec, + struct dns_res_rec **answers, uint16_t *ancount) { struct dns_res_rec *ans = *answers; uint16_t ai = *ancount; char *tmp; uint32_t i; + if (ai == UINT16_MAX) { + return WERR_BUFFER_OVERFLOW; + } + + ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec, ai+1); + if (ans == NULL) { + return WERR_NOMEM; + } + ZERO_STRUCT(ans[ai]); switch (rec->wType) { @@ -281,13 +290,10 @@ static WERROR add_zone_authority_record(struct dns_server *dns, return werror; } - ns = talloc_realloc(mem_ctx, ns, struct dns_res_rec, rec_count + ni); - if (ns == NULL) { - return WERR_NOMEM; - } for (ri = 0; ri < rec_count; ri++) { if (recs[ri].wType == DNS_TYPE_SOA) { - werror = create_response_rr(zone, &recs[ri], &ns, &ni); + werror = add_response_rr(mem_ctx, zone, &recs[ri], + &ns, &ni); if (!W_ERROR_IS_OK(werror)) { return werror; } @@ -326,11 +332,6 @@ static WERROR handle_question(struct dns_server *dns, goto done; } - ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec, rec_count + ai); - if (ans == NULL) { - return WERR_NOMEM; - } - /* Set up for an NXDOMAIN reply if no match is found */ werror_return = DNS_ERR(NAME_ERROR); @@ -345,16 +346,9 @@ static WERROR handle_question(struct dns_server *dns, return WERR_NOMEM; } - /* We reply with one more record, so grow the array */ - ans = talloc_realloc(mem_ctx, ans, struct dns_res_rec, - rec_count + 1); - if (ans == NULL) { - TALLOC_FREE(new_q); - return WERR_NOMEM; - } - /* First put in the CNAME record */ - werror = create_response_rr(question->name, &recs[ri], &ans, &ai); + werror = add_response_rr(mem_ctx, question->name, + &recs[ri], &ans, &ai); if (!W_ERROR_IS_OK(werror)) { TALLOC_FREE(new_q); return werror; @@ -385,7 +379,8 @@ static WERROR handle_question(struct dns_server *dns, werror_return = WERR_OK; continue; } - werror = create_response_rr(question->name, &recs[ri], &ans, &ai); + werror = add_response_rr(mem_ctx, question->name, &recs[ri], + &ans, &ai); if (!W_ERROR_IS_OK(werror)) { return werror; }