dns_server: Consolidate talloc_realloc
authorVolker Lendecke <vl@samba.org>
Fri, 7 Aug 2015 06:27:19 +0000 (08:27 +0200)
committerKai Blin <kai@samba.org>
Tue, 15 Dec 2015 13:43:09 +0000 (14:43 +0100)
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 <vl@samba.org>
Reviewed-by: Kai Blin <kai@samba.org>
source4/dns_server/dns_query.c

index 956898e580fb56043ca58252f87721cace3dec55..fb29e8fca68b6586c26aa3d58d92b6a5585e05bf 100644 (file)
 #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;
                }