From bd7dcde38f157f91e8515c69df50ee8b4dda3d40 Mon Sep 17 00:00:00 2001 From: Daniel Salzman Date: Sat, 27 Jan 2018 15:07:44 +0100 Subject: [PATCH] pkt: remove useless pointer sanitization in knot_pkt_free --- src/knot/nameserver/update.c | 12 +++--- src/knot/query/requestor.c | 6 +-- src/knot/server/tcp-handler.c | 6 +-- src/knot/server/udp-handler.c | 6 +-- src/knot/zone/zone.c | 4 +- src/libknot/packet/pkt.c | 17 ++++---- src/libknot/packet/pkt.h | 2 +- src/utils/kdig/kdig_exec.c | 58 +++++++++++++------------- src/utils/knsupdate/knsupdate_params.c | 6 +-- tests-fuzz/fuzz_packet.c | 4 +- tests/libknot/test_pkt.c | 9 ++-- tests/modules/test_rrl.c | 6 +-- tests/test_process_query.c | 4 +- 13 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/knot/nameserver/update.c b/src/knot/nameserver/update.c index d6a28558c..5f499a4ca 100644 --- a/src/knot/nameserver/update.c +++ b/src/knot/nameserver/update.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -224,7 +224,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot knot_pkt_t *query = knot_pkt_new(NULL, request->query->max_size, NULL); int ret = knot_pkt_copy(query, request->query); if (ret != KNOT_EOK) { - knot_pkt_free(&query); + knot_pkt_free(query); return ret; } knot_wire_set_id(query->wire, dnssec_random_uint16_t()); @@ -240,7 +240,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot struct knot_requestor re; ret = knot_requestor_init(&re, capture, &capture_param, NULL); if (ret != KNOT_EOK) { - knot_pkt_free(&query); + knot_pkt_free(query); return ret; } @@ -250,7 +250,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot struct knot_request *req = knot_request_make(re.mm, dst, src, query, NULL, 0); if (req == NULL) { knot_requestor_clear(&re); - knot_pkt_free(&query); + knot_pkt_free(query); return KNOT_ENOMEM; } @@ -356,8 +356,8 @@ static void send_update_response(conf_t *conf, const zone_t *zone, struct knot_r static void free_request(struct knot_request *req) { close(req->fd); - knot_pkt_free(&req->query); - knot_pkt_free(&req->resp); + knot_pkt_free(req->query); + knot_pkt_free(req->resp); free(req); } diff --git a/src/knot/query/requestor.c b/src/knot/query/requestor.c index 46af6867e..d28b9c94a 100644 --- a/src/knot/query/requestor.c +++ b/src/knot/query/requestor.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -156,8 +156,8 @@ void knot_request_free(struct knot_request *request, knot_mm_t *mm) if (request->fd >= 0) { close(request->fd); } - knot_pkt_free(&request->query); - knot_pkt_free(&request->resp); + knot_pkt_free(request->query); + knot_pkt_free(request->resp); tsig_cleanup(&request->tsig); mm_free(mm, request); diff --git a/src/knot/server/tcp-handler.c b/src/knot/server/tcp-handler.c index 435f3907d..463544485 100644 --- a/src/knot/server/tcp-handler.c +++ b/src/knot/server/tcp-handler.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -168,8 +168,8 @@ static int tcp_handle(tcp_context_t *tcp, int fd, knot_layer_finish(&tcp->layer); /* Cleanup. */ - knot_pkt_free(&query); - knot_pkt_free(&ans); + knot_pkt_free(query); + knot_pkt_free(ans); return ret; } diff --git a/src/knot/server/udp-handler.c b/src/knot/server/udp-handler.c index 535e2e6e2..1241d982e 100644 --- a/src/knot/server/udp-handler.c +++ b/src/knot/server/udp-handler.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -100,8 +100,8 @@ static void udp_handle(udp_context_t *udp, int fd, struct sockaddr_storage *ss, knot_layer_finish(&udp->layer); /* Cleanup. */ - knot_pkt_free(&query); - knot_pkt_free(&ans); + knot_pkt_free(query); + knot_pkt_free(ans); } /*! \brief Pointer to selected UDP master implementation. */ diff --git a/src/knot/zone/zone.c b/src/knot/zone/zone.c index bfb74bf53..fbccd0b52 100644 --- a/src/knot/zone/zone.c +++ b/src/knot/zone/zone.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -555,7 +555,7 @@ int zone_update_enqueue(zone_t *zone, knot_pkt_t *pkt, knotd_qdata_params_t *par req->query = knot_pkt_new(NULL, pkt->max_size, NULL); int ret = knot_pkt_copy(req->query, pkt); if (ret != KNOT_EOK) { - knot_pkt_free(&req->query); + knot_pkt_free(req->query); free(req); return ret; } diff --git a/src/libknot/packet/pkt.c b/src/libknot/packet/pkt.c index 8ed1b712d..414bc14c2 100644 --- a/src/libknot/packet/pkt.c +++ b/src/libknot/packet/pkt.c @@ -378,26 +378,25 @@ void knot_pkt_clear(knot_pkt_t *pkt) } _public_ -void knot_pkt_free(knot_pkt_t **pkt) +void knot_pkt_free(knot_pkt_t *pkt) { - if (pkt == NULL || *pkt == NULL) { + if (pkt == NULL) { return; } /* Free temporary RRSets. */ - pkt_free_data(*pkt); + pkt_free_data(pkt); /* Free RR/RR info arrays. */ - mm_free(&(*pkt)->mm, (*pkt)->rr); - mm_free(&(*pkt)->mm, (*pkt)->rr_info); + mm_free(&pkt->mm, pkt->rr); + mm_free(&pkt->mm, pkt->rr_info); // free the space for wireformat - if ((*pkt)->flags & KNOT_PF_FREE) { - mm_free(&(*pkt)->mm, (*pkt)->wire); + if (pkt->flags & KNOT_PF_FREE) { + mm_free(&pkt->mm, pkt->wire); } - mm_free(&(*pkt)->mm, *pkt); - *pkt = NULL; + mm_free(&pkt->mm, pkt); } _public_ diff --git a/src/libknot/packet/pkt.h b/src/libknot/packet/pkt.h index 9889a787a..891e7513b 100644 --- a/src/libknot/packet/pkt.h +++ b/src/libknot/packet/pkt.h @@ -139,7 +139,7 @@ int knot_pkt_init_response(knot_pkt_t *pkt, const knot_pkt_t *query); void knot_pkt_clear(knot_pkt_t *pkt); /*! \brief Begone you foul creature of the underworld. */ -void knot_pkt_free(knot_pkt_t **pkt); +void knot_pkt_free(knot_pkt_t *pkt); /*! * \brief Reserve an arbitrary amount of space in the packet. diff --git a/src/utils/kdig/kdig_exec.c b/src/utils/kdig/kdig_exec.c index 3dad5dc0d..f668f80e5 100644 --- a/src/utils/kdig/kdig_exec.c +++ b/src/utils/kdig/kdig_exec.c @@ -235,7 +235,7 @@ static void process_dnstap(const query_t *query) ERR("can't print dnstap message\n"); } - knot_pkt_free(&pkt); + knot_pkt_free(pkt); dt_reader_free_frame(reader, &frame); } } @@ -412,7 +412,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) // Create QNAME from string. knot_dname_t *qname = knot_dname_from_str_alloc(query->owner); if (qname == NULL) { - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } @@ -421,7 +421,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) query->type_num); if (ret != KNOT_EOK) { knot_dname_free(&qname, NULL); - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } @@ -444,7 +444,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) &packet->mm); knot_dname_free(&qname, NULL); if (soa == NULL) { - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } @@ -452,7 +452,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) ret = knot_rrset_add_rdata(soa, wire, sizeof(wire), &packet->mm); if (ret != KNOT_EOK) { knot_rrset_free(&soa, &packet->mm); - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } @@ -462,7 +462,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) ret = knot_pkt_put(packet, KNOT_COMPR_HINT_NONE, soa, KNOT_PF_FREE); if (ret != KNOT_EOK) { knot_rrset_free(&soa, &packet->mm); - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } @@ -479,7 +479,7 @@ static knot_pkt_t *create_query_packet(const query_t *query) int ret = add_query_edns(packet, query, max_size); if (ret != KNOT_EOK) { ERR("can't set up EDNS section\n"); - knot_pkt_free(&packet); + knot_pkt_free(packet); return NULL; } } @@ -636,7 +636,7 @@ static int process_query_packet(const knot_pkt_t *query, } else { ERR("can't print query packet\n"); } - knot_pkt_free(&q); + knot_pkt_free(q); } else { ERR("can't print query packet\n"); } @@ -675,7 +675,7 @@ static int process_query_packet(const knot_pkt_t *query, // Parse reply to the packet structure. if (knot_pkt_parse(reply, KNOT_PF_NOCANON) != KNOT_EOK) { ERR("malformed reply packet from %s\n", net->remote_str); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -685,12 +685,12 @@ static int process_query_packet(const knot_pkt_t *query, break; // Check for timeout. } else if (time_diff_ms(&t_query, &t_end) > 1000 * net->wait) { - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return -1; } - knot_pkt_free(&reply); + knot_pkt_free(reply); } // Check for TC bit and repeat query with TCP if required. @@ -699,7 +699,7 @@ static int process_query_packet(const knot_pkt_t *query, printf("\n"); WARN("truncated reply from %s, retrying over TCP\n\n", net->remote_str); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); net->socktype = SOCK_STREAM; @@ -740,7 +740,7 @@ static int process_query_packet(const knot_pkt_t *query, uint8_t *opt = knot_edns_get_option(reply->opt_rr, KNOT_EDNS_OPTION_COOKIE); if (opt == NULL) { ERR("bad cookie, missing EDNS section\n"); - knot_pkt_free(&reply); + knot_pkt_free(reply); return -1; } @@ -749,11 +749,11 @@ static int process_query_packet(const knot_pkt_t *query, int ret = knot_edns_cookie_parse(&new_ctx.cc, &new_ctx.sc, data, data_len); if (ret != KNOT_EOK) { - knot_pkt_free(&reply); + knot_pkt_free(reply); ERR("bad cookie, missing EDNS cookie option\n"); return -1; } - knot_pkt_free(&reply); + knot_pkt_free(reply); // Restore the original client cookie. new_ctx.cc = query_ctx->cc; @@ -761,12 +761,12 @@ static int process_query_packet(const knot_pkt_t *query, knot_pkt_t *new_query = create_query_packet(&new_ctx); ret = process_query_packet(new_query, net, &new_ctx, ignore_tc, sign_ctx, style); - knot_pkt_free(&new_query); + knot_pkt_free(new_query); return ret; } - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; @@ -846,7 +846,7 @@ static void process_query(const query_t *query) if (ret == 0) { net_clean(&net); sign_context_deinit(&sign_ctx); - knot_pkt_free(&out_packet); + knot_pkt_free(out_packet); return; } @@ -870,7 +870,7 @@ static void process_query(const query_t *query) } sign_context_deinit(&sign_ctx); - knot_pkt_free(&out_packet); + knot_pkt_free(out_packet); } static int process_xfr_packet(const knot_pkt_t *query, @@ -930,7 +930,7 @@ static int process_xfr_packet(const knot_pkt_t *query, } else { ERR("can't print query packet\n"); } - knot_pkt_free(&q); + knot_pkt_free(q); } else { ERR("can't print query packet\n"); } @@ -969,7 +969,7 @@ static int process_xfr_packet(const knot_pkt_t *query, // Parse reply to the packet structure. if (knot_pkt_parse(reply, 0) != KNOT_EOK) { ERR("malformed reply packet from %s\n", net->remote_str); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -977,7 +977,7 @@ static int process_xfr_packet(const knot_pkt_t *query, // Compare reply header id. if (check_reply_id(reply, query) == false) { ERR("reply ID mismatch from %s\n", net->remote_str); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -991,7 +991,7 @@ static int process_xfr_packet(const knot_pkt_t *query, if (knot_pkt_ext_rcode(reply) != KNOT_RCODE_NOERROR) { ERR("server replied with error '%s'\n", knot_pkt_ext_rcode_name(reply)); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -1011,7 +1011,7 @@ static int process_xfr_packet(const knot_pkt_t *query, ERR("reply verification for %s (%s)\n", net->remote_str, knot_strerror(ret)); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -1023,7 +1023,7 @@ static int process_xfr_packet(const knot_pkt_t *query, if (serial < 0) { ERR("first answer record from %s isn't SOA\n", net->remote_str); - knot_pkt_free(&reply); + knot_pkt_free(reply); net_close(net); return 0; } @@ -1044,11 +1044,11 @@ static int process_xfr_packet(const knot_pkt_t *query, // Check for finished transfer. if (finished_xfr(serial, reply, msg_count, query_ctx->serial != -1)) { - knot_pkt_free(&reply); + knot_pkt_free(reply); break; } - knot_pkt_free(&reply); + knot_pkt_free(reply); } // Get stop reply time. @@ -1107,7 +1107,7 @@ static void process_xfr(const query_t *query) flags, &query->tls, &net); if (ret != KNOT_EOK) { sign_context_deinit(&sign_ctx); - knot_pkt_free(&out_packet); + knot_pkt_free(out_packet); return; } @@ -1133,7 +1133,7 @@ static void process_xfr(const query_t *query) net_clean(&net); sign_context_deinit(&sign_ctx); - knot_pkt_free(&out_packet); + knot_pkt_free(out_packet); } int kdig_exec(const kdig_params_t *params) diff --git a/src/utils/knsupdate/knsupdate_params.c b/src/utils/knsupdate/knsupdate_params.c index 19aa223a4..f1b067e36 100644 --- a/src/utils/knsupdate/knsupdate_params.c +++ b/src/utils/knsupdate/knsupdate_params.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -146,8 +146,8 @@ void knsupdate_clean(knsupdate_params_t *params) srv_info_free(params->srcif); free(params->zone); zs_deinit(¶ms->parser); - knot_pkt_free(¶ms->query); - knot_pkt_free(¶ms->answer); + knot_pkt_free(params->query); + knot_pkt_free(params->answer); knot_tsig_key_deinit(¶ms->tsig_key); /* Clean up the structure. */ diff --git a/tests-fuzz/fuzz_packet.c b/tests-fuzz/fuzz_packet.c index 0d8dae05c..ad9c086ab 100644 --- a/tests-fuzz/fuzz_packet.c +++ b/tests-fuzz/fuzz_packet.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,7 +27,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) knot_pkt_t *pkt = knot_pkt_new(copy, size, NULL); assert(pkt); knot_pkt_parse(pkt, 0); - knot_pkt_free(&pkt); + knot_pkt_free(pkt); return 0; } diff --git a/tests/libknot/test_pkt.c b/tests/libknot/test_pkt.c index 661a54e6e..e14361ba1 100644 --- a/tests/libknot/test_pkt.c +++ b/tests/libknot/test_pkt.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -188,10 +188,9 @@ int main(int argc, char *argv[]) packet_match(in, copy); /* Free packets. */ - knot_pkt_free(©); - knot_pkt_free(&out); - knot_pkt_free(&in); - ok(in == NULL && out == NULL && copy == NULL, "pkt: free"); + knot_pkt_free(copy); + knot_pkt_free(out); + knot_pkt_free(in); /* Free extra data. */ for (unsigned i = 0; i < NAMECOUNT; ++i) { diff --git a/tests/modules/test_rrl.c b/tests/modules/test_rrl.c index 24dcc7b1b..5d13c599f 100644 --- a/tests/modules/test_rrl.c +++ b/tests/modules/test_rrl.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) int ret = knot_pkt_put_question(query, qname, KNOT_CLASS_IN, KNOT_RRTYPE_A); knot_dname_free(&qname, NULL); if (ret != KNOT_EOK) { - knot_pkt_free(&query); + knot_pkt_free(query); return KNOT_ERROR; /* Fatal */ } @@ -160,7 +160,7 @@ int main(int argc, char *argv[]) #endif knot_dname_free(&zone, NULL); - knot_pkt_free(&query); + knot_pkt_free(query); rrl_destroy(rrl); dnssec_crypto_cleanup(); return 0; diff --git a/tests/test_process_query.c b/tests/test_process_query.c index b6c42d341..aa678773a 100644 --- a/tests/test_process_query.c +++ b/tests/test_process_query.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 CZ.NIC, z.s.p.o. +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -68,7 +68,7 @@ static void exec_query(knot_layer_t *layer, const char *name, /* Check answer. */ answer_sanity_check(query->wire, answer->wire, answer->size, expected_rcode, name); - knot_pkt_free(&answer); + knot_pkt_free(answer); } /* \internal Helpers */