]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
modules/cookies/cookiemonster.c: replace asserts
authorTomas Krizek <tomas.krizek@nic.cz>
Wed, 24 Mar 2021 16:44:48 +0000 (17:44 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Tue, 25 May 2021 12:39:43 +0000 (14:39 +0200)
modules/cookies/cookiemonster.c

index fa1b869eb2668221c419c61118fdb344de54be3c..f443a4d2f0fd1d7a87ba99e83ce7a8a6cabd1b68 100644 (file)
@@ -2,7 +2,6 @@
  *  SPDX-License-Identifier: GPL-3.0-or-later
  */
 
-#include <assert.h>
 #include <ccan/json/json.h>
 #include <libknot/db/db_lmdb.h>
 #include <libknot/error.h>
@@ -57,13 +56,15 @@ static int srvr_sockaddr_cc_check(const struct sockaddr *srvr_sa,
                                   const uint8_t *cc, uint16_t cc_len,
                                   const struct kr_cookie_settings *clnt_sett)
 {
-       assert(cc && cc_len > 0 && clnt_sett);
+       if (!kr_assume(cc && cc_len > 0 && clnt_sett))
+               return -2;
 
        if (!srvr_sa) {
                return -2;
        }
 
-       assert(clnt_sett->current.secr);
+       if (!kr_assume(clnt_sett->current.secr))
+               return -2;
 
        /* The address must correspond with the client cookie. */
        struct knot_cc_input input = {
@@ -107,7 +108,8 @@ static int srvr_sockaddr_cc_check(const struct sockaddr *srvr_sa,
 static const uint8_t *get_cookie_opt(kr_cookie_lru_t *cache,
                                      const struct sockaddr *sa)
 {
-       assert(cache && sa);
+       if (!kr_assume(cache && sa))
+               return NULL;
 
        const uint8_t *cached_cookie_opt = kr_cookie_lru_get(cache, sa);
        if (!cached_cookie_opt) {
@@ -132,7 +134,8 @@ static const uint8_t *get_cookie_opt(kr_cookie_lru_t *cache,
 static bool is_cookie_cached(kr_cookie_lru_t *cache, const struct sockaddr *sa,
                              const uint8_t *cookie_opt)
 {
-       assert(cache && sa && cookie_opt);
+       if (!kr_assume(cache && sa && cookie_opt))
+               return false;
 
        const uint8_t *cached_opt = get_cookie_opt(cache, sa);
        if (!cached_opt) {
@@ -159,7 +162,8 @@ static bool check_cookie_content_and_cache(const struct kr_cookie_settings *clnt
                                            uint8_t *pkt_cookie_opt,
                                            kr_cookie_lru_t *cache)
 {
-       assert(clnt_sett && req && pkt_cookie_opt && cache);
+       if (!kr_assume(clnt_sett && req && pkt_cookie_opt && cache))
+               return false;
 
        const uint8_t *pkt_cookie_data = knot_edns_opt_get_data(pkt_cookie_opt);
        uint16_t pkt_cookie_len = knot_edns_opt_get_length(pkt_cookie_opt);
@@ -176,7 +180,8 @@ static bool check_cookie_content_and_cache(const struct kr_cookie_settings *clnt
                          "got malformed DNS cookie or server cookie missing");
                return false;
        }
-       assert(pkt_cc_len == KNOT_OPT_COOKIE_CLNT);
+       if (!kr_assume(pkt_cc_len == KNOT_OPT_COOKIE_CLNT))
+               return false;
 
        /* Check server address against received client cookie. */
        const struct sockaddr *srvr_sockaddr = passed_server_sockaddr(req);
@@ -186,7 +191,8 @@ static bool check_cookie_content_and_cache(const struct kr_cookie_settings *clnt
                VERBOSE_MSG(NULL, "%s\n", "could not match received cookie");
                return false;
        }
-       assert(srvr_sockaddr);
+       if (!kr_assume(srvr_sockaddr))
+               return false;
 
        /* Don't cache received cookies that don't match the current secret. */
        if ((ret == 1) &&
@@ -301,7 +307,8 @@ static inline uint8_t *req_cookie_option(struct kr_request *req)
 static int invalid_sc_status(int state, bool sc_present, bool ignore_badcookie,
                              const struct kr_request *req, knot_pkt_t *answer)
 {
-       assert(req && answer);
+       if (!kr_assume(req && answer))
+               return KR_STATE_FAIL;
 
        const knot_pkt_t *pkt = req->qsource.packet;