From a5ca3850aa1283df530b10f9be63c033488706e4 Mon Sep 17 00:00:00 2001 From: TCY16 Date: Fri, 17 Jun 2022 15:52:45 +0200 Subject: [PATCH] fix memory alloc bug for the testcase and c89 style --- services/cache/infra.c | 4 ++-- testcode/testpkts.c | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/services/cache/infra.c b/services/cache/infra.c index 02ac7228f..beabca6a7 100644 --- a/services/cache/infra.c +++ b/services/cache/infra.c @@ -389,6 +389,7 @@ data_entry_init(struct infra_cache* infra, struct lruhash_entry* e, time_t timenow) { int i; + struct infra_data* data; uint8_t cookie[8] = {0,0,0,0,0,0,0,0}; for (i = 0; i < 8; i++) { @@ -396,8 +397,7 @@ data_entry_init(struct infra_cache* infra, struct lruhash_entry* e, 255); // @TODO is 255 correct? macro-ify it? } - - struct infra_data* data = (struct infra_data*)e->data; + data = (struct infra_data*)e->data; data->ttl = timenow + infra->host_ttl; rtt_init(&data->rtt); data->edns_version = 0; diff --git a/testcode/testpkts.c b/testcode/testpkts.c index 5b233e83f..a50bf3aee 100644 --- a/testcode/testpkts.c +++ b/testcode/testpkts.c @@ -1778,22 +1778,32 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len, if(match->server_cookie) { /** Find the cookie option and add the server cookie if * the client cookie is present and not already there */ + res = realloc(res, reslen + 28); uint8_t* walk_query = query_pkt; size_t walk_query_len = query_len; uint8_t* walk_response = res; - size_t walk_response_len = reslen; + size_t walk_response_len = reslen+28; uint8_t* rdlen_ptr_query; uint8_t* rdlen_ptr_response; + reslen += 28; + + if (!(walk_response)) { + log_err("testbound: out of memory; send without cookie"); + return; + } + /* verify that we have a EDNS record */ if(!pkt_find_edns_opt(&walk_query, &walk_query_len)) { walk_query_len = 0; - log_err("testbound: no EDNS in the query packet when trying to attach a EDNS cookie"); + log_err("testbound: no EDNS in the query packet when " + "trying to attach a EDNS cookie"); } if(!pkt_find_edns_opt(&walk_response, &walk_response_len)) { walk_response_len = 0; - log_err("testbound: no EDNS in the response packet when trying to attach a EDNS cookie"); + log_err("testbound: no EDNS in the response packet when" + "trying to attach a EDNS cookie"); } /* verify that we have a EDNS option */ @@ -1832,11 +1842,15 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len, log_err("testbound: invalid EDNS cookie in the query packet"); } - if (walk_query_len > 0 && walk_response_len == 0) { + if (walk_query_len > 0 && walk_response_len > 0) { + /* create space for the cookie, as the length of the + * rest of the response is 0 */ + /* depending on the incoming cookie, add the server cookie * or copy the complete cookie to the response */ if (sldns_read_uint16(walk_query+2) == 8) { - /* copy the EDNS client cookie from the query packet to the response */ + /* copy the EDNS client cookie from the query + * packet to the response */ memcpy(walk_response, walk_query, 12); /* add the server cookie to the client cookie to make it @@ -1859,15 +1873,12 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len, memcpy(rdlen_ptr_response, rdlen_ptr_query, 12); memcpy(walk_response+12, rdlen_ptr_query+12+8, 8); memcpy(walk_response+12+8, rdlen_ptr_query+12, 8); - - reslen = origlen + 28; } else { memcpy(rdlen_ptr_response, rdlen_ptr_query, 28); - - reslen = origlen + 28; } } else { log_err("testbound: the incoming EDNS cookie has the wrong length"); + } } else { log_err("testbound: an error has occured while parsing the EDNS cookie");