]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/cache: get rid of void-pointer arithmetic
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 29 May 2018 11:11:40 +0000 (13:11 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 29 May 2018 11:11:40 +0000 (13:11 +0200)
Checked with -Wpointer-arith; still hindered by contrib/ucw.

lib/cache/api.c
lib/cache/entry_list.c
lib/cache/entry_rr.c
lib/cache/impl.h
lib/cache/nsec1.c
lib/cache/nsec3.c

index 246a8c7c2df54e584989fa3a8d9353511fbc87aa..afbc1c2a0b467c210b451d857955caa106a8bc17 100644 (file)
@@ -438,7 +438,7 @@ static int peek_nosync(kr_layer_t *ctx, knot_pkt_t *pkt)
                const int32_t new_ttl = get_new_ttl(v.data, qry, qry->sname,
                                                KNOT_RRTYPE_CNAME, qry->timestamp.tv_sec);
                ret = answer_simple_hit(ctx, pkt, KNOT_RRTYPE_CNAME, v.data,
-                                       v.data + v.len, new_ttl);
+                                       knot_db_val_bound(v), new_ttl);
                /* TODO: ^^ cumbersome code; we also recompute the TTL */
                return ret == kr_ok() ? KR_STATE_DONE : ctx->state;
                }
@@ -495,7 +495,7 @@ static int peek_nosync(kr_layer_t *ctx, knot_pkt_t *pkt)
                if (remains < 0) goto cont;
                {
                        const uint8_t *nsec_p_raw = el[i].len > sizeof(stamp)
-                                       ? el[i].data + sizeof(stamp) : NULL;
+                                       ? (uint8_t *)el[i].data + sizeof(stamp) : NULL;
                        nsec_p_init(&ans.nsec_p, nsec_p_raw);
                }
                /**** 2. and 3. inside */
@@ -535,8 +535,7 @@ static int peek_nosync(kr_layer_t *ctx, knot_pkt_t *pkt)
                        return ctx->state;
                }
                /* Add the SOA into the answer. */
-               void *eh_data_bound = val.data + val.len;
-               ret = entry2answer(&ans, AR_SOA, eh, eh_data_bound,
+               ret = entry2answer(&ans, AR_SOA, eh, knot_db_val_bound(val),
                                   k->zname, KNOT_RRTYPE_SOA, new_ttl);
                if (ret) return ctx->state;
        }
@@ -901,8 +900,7 @@ static ssize_t stash_rrset(struct kr_cache *cache, const struct kr_query *qry,
                struct answer ans;
                memset(&ans, 0, sizeof(ans));
                ans.mm = &qry->request->pool;
-               ret = entry2answer(&ans, AR_ANSWER, eh,
-                                  val_new_entry.data + val_new_entry.len,
+               ret = entry2answer(&ans, AR_ANSWER, eh, knot_db_val_bound(val_new_entry),
                                   rr->owner, rr->type, 0);
                /*
                VERBOSE_MSG(qry, "=> sanity: written %d and read %d\n",
@@ -1017,7 +1015,7 @@ static int stash_nsec_p(const knot_dname_t *dname, const char *nsec_p_v,
                int i_replace = ENTRY_APEX_NSECS_CNT - 1;
                for (int i = 0; i < ENTRY_APEX_NSECS_CNT; ++i) {
                        if (el[i].len != data_stride) continue;
-                       if (nsec_p && memcmp(nsec_p, el[i].data + sizeof(uint32_t),
+                       if (nsec_p && memcmp(nsec_p, (uint8_t *)el[i].data + sizeof(uint32_t),
                                                data_stride - sizeof(uint32_t)) != 0) {
                                continue;
                        }
@@ -1049,7 +1047,7 @@ static int stash_nsec_p(const knot_dname_t *dname, const char *nsec_p_v,
        /* Prepare the new data chunk */
        memcpy(el[0].data, &valid_until, sizeof(valid_until));
        if (nsec_p) {
-               memcpy(el[0].data + sizeof(valid_until), nsec_p,
+               memcpy((uint8_t *)el[0].data + sizeof(valid_until), nsec_p,
                        data_stride - sizeof(valid_until));
        }
        /* Write it all to the cache */
@@ -1135,7 +1133,7 @@ static int found_exact_hit(kr_layer_t *ctx, knot_pkt_t *pkt, knot_db_val_t val,
                return kr_error(ENOENT);
        }
 
-       const void *eh_bound = val.data + val.len;
+       const uint8_t *eh_bound = knot_db_val_bound(val);
        if (eh->is_packet) {
                /* Note: we answer here immediately, even if it's (theoretically)
                 * possible that we could generate a higher-security negative proof.
@@ -1189,8 +1187,8 @@ static int try_wild(struct key *k, struct answer *ans, const knot_dname_t *clenc
                return -ABS(ESTALE);
        }
        /* Add the RR into the answer. */
-       const void *eh_bound = val.data + val.len;
-       ret = entry2answer(ans, AR_ANSWER, eh, eh_bound, qry->sname, type, new_ttl);
+       ret = entry2answer(ans, AR_ANSWER, eh, knot_db_val_bound(val),
+                          qry->sname, type, new_ttl);
        VERBOSE_MSG(qry, "=> wildcard: answer expanded, ret = %d, new TTL %d\n",
                        ret, (int)new_ttl);
        if (ret) return kr_error(ret);
@@ -1226,7 +1224,7 @@ static int peek_exact_real(struct kr_cache *cache, const knot_dname_t *name, uin
                .ttl  = eh->ttl,
                .rank = eh->rank,
                .raw_data = val.data,
-               .raw_bound = val.data + val.len,
+               .raw_bound = knot_db_val_bound(val),
        };
        return kr_ok();
 }
index ae2f2afff526301864ed423e65882221e5012ea7..3c633538248dc0d88a3aea2008b473fa61658238 100644 (file)
@@ -61,7 +61,7 @@ int entry_list_parse(const knot_db_val_t val, entry_list_t list)
                return kr_error(EILSEQ);
        }
        const uint8_t *it = ea->data,
-               *it_bound = val.data + val.len;
+               *it_bound = knot_db_val_bound(val);
        for (int i = 0; i < ENTRY_APEX_NSECS_CNT; ++i) {
                if (it > it_bound) {
                        return kr_error(EILSEQ);
@@ -126,8 +126,8 @@ static int entry_h_len(const knot_db_val_t val)
        const bool ok = val.data && ((ssize_t)val.len) > 0;
        if (!ok) return kr_error(EINVAL);
        const struct entry_h *eh = val.data;
-       const void *d = eh->data; /* iterates over the data in entry */
-       const void *data_bound = val.data + val.len;
+       const uint8_t *d = eh->data; /* iterates over the data in entry */
+       const uint8_t *data_bound = knot_db_val_bound(val);
        if (d >= data_bound) return kr_error(EILSEQ);
        if (!eh->is_packet) { /* Positive RRset + its RRsig set (may be empty). */
                int sets = 2;
@@ -150,7 +150,7 @@ static int entry_h_len(const knot_db_val_t val)
                d += 2 + len;
        }
        if (d > data_bound) return kr_error(EILSEQ);
-       return d - val.data;
+       return d - (uint8_t *)val.data;
 }
 
 struct entry_apex * entry_apex_consistent(knot_db_val_t val)
@@ -287,7 +287,8 @@ int entry_h_splice(
        ret = cache_write_or_clear(cache, &key, &val, qry);
        if (ret) return kr_error(ret);
        memcpy(val.data, buf, val.len); /* we also copy the "empty" space, but well... */
-       val_new_entry->data = val.data + (el[i_type].data - buf);
+       val_new_entry->data = (uint8_t *)val.data
+                           + ((uint8_t *)el[i_type].data - (uint8_t *)buf);
        return kr_ok();
 }
 
index 20cabdf519756ee464fe8ef63dfa0d5d21c1aefc..729cb593474a4ec26df7844e4e7493843379acac 100644 (file)
@@ -104,7 +104,7 @@ int kr_cache_materialize(knot_rdataset_t *dst, const struct kr_cache_p *ref,
 
 
 int entry2answer(struct answer *ans, int id,
-               const struct entry_h *eh, const void *eh_bound,
+               const struct entry_h *eh, const uint8_t *eh_bound,
                const knot_dname_t *owner, uint16_t type, uint32_t new_ttl)
 {
        /* We assume it's zeroed.  Do basic sanity check. */
index d6287aac04776b749be8f4b95f0a92e634752340..6529fd561437ee55316950c21490ecb89eb56583 100644 (file)
@@ -271,7 +271,7 @@ enum {
  * \return error code.  They are all bad conditions and "guarded" by assert.
  */
 int entry2answer(struct answer *ans, int id,
-               const struct entry_h *eh, const void *eh_bound,
+               const struct entry_h *eh, const uint8_t *eh_bound,
                const knot_dname_t *owner, uint16_t type, uint32_t new_ttl);
 
 
@@ -359,3 +359,9 @@ static inline uint16_t get_uint16(const void *address)
        return tmp;
 }
 
+/** Useful pattern, especially as void-pointer arithmetic isn't standard-compliant. */
+static inline uint8_t * knot_db_val_bound(knot_db_val_t val)
+{
+       return (uint8_t *)val.data + val.len;
+}
+
index 13b63ff61b241ba6fe09f443d56661fc03eb3e8a..acf5cdeb4baaafbd288adb6476951b3ccc6357ae 100644 (file)
@@ -189,7 +189,7 @@ static const char * find_leq_NSEC1(struct kr_cache *cache, const struct kr_query
        }
        if (kwz_low) {
                *kwz_low = (knot_db_val_t){
-                       .data = key_nsec.data + nwz_off,
+                       .data = (uint8_t *)key_nsec.data + nwz_off,
                        .len = key_nsec.len - nwz_off,
                };      /* CACHE_KEY_DEF */
        }
@@ -245,7 +245,7 @@ static const char * find_leq_NSEC1(struct kr_cache *cache, const struct kr_query
        assert((ssize_t)(kwz_hi.len) >= 0);
        /* 2. do the actual range check. */
        const knot_db_val_t kwz_sname = {
-               .data = (void *)k->buf + 1 + nwz_off,
+               .data = (void *)(k->buf + 1 + nwz_off),
                .len = k->buf[0] - k->zlf_len,
        };
        assert((ssize_t)(kwz_sname.len) >= 0);
@@ -301,8 +301,7 @@ int nsec1_encloser(struct key *k, struct answer *ans,
        /* Basic checks OK -> materialize data. */
        {
                const struct entry_h *nsec_eh = val.data;
-               const void *nsec_eh_bound = val.data + val.len;
-               int ret = entry2answer(ans, AR_NSEC, nsec_eh, nsec_eh_bound,
+               int ret = entry2answer(ans, AR_NSEC, nsec_eh, knot_db_val_bound(val),
                                        owner, KNOT_RRTYPE_NSEC, new_ttl);
                if (ret) return kr_error(ret);
        }
@@ -405,7 +404,7 @@ int nsec1_src_synth(struct key *k, struct answer *ans, const knot_dname_t *clenc
        }
        /* Check if our sname-covering NSEC also covers/matches SS. */
        knot_db_val_t kwz = {
-               .data = key.data + nwz_off,
+               .data = (uint8_t *)key.data + nwz_off,
                .len = key.len - nwz_off,
        };
        assert((ssize_t)(kwz.len) >= 0);
@@ -432,11 +431,10 @@ int nsec1_src_synth(struct key *k, struct answer *ans, const knot_dname_t *clenc
                }
                /* Materialize the record into answer (speculatively). */
                const struct entry_h *nsec_eh = val.data;
-               const void *nsec_eh_bound = val.data + val.len;
                knot_dname_t owner[KNOT_DNAME_MAXLEN];
                int ret = dname_wire_reconstruct(owner, k, wild_low_kwz);
                if (ret) return kr_error(ret);
-               ret = entry2answer(ans, AR_WILD, nsec_eh, nsec_eh_bound,
+               ret = entry2answer(ans, AR_WILD, nsec_eh, knot_db_val_bound(val),
                                   owner, KNOT_RRTYPE_NSEC, new_ttl);
                if (ret) return kr_error(ret);
                nsec_rr = ans->rrsets[AR_WILD].set.rr;
index f232c7144b0df0b3b25b9e34bae6aea8ad7344f3..1b5b7ad4a70687723cb10d7849ede4e198a43c60 100644 (file)
@@ -62,8 +62,8 @@ knot_db_val_t key_NSEC3(struct key *k, const knot_dname_t *nsec3_name,
        knot_db_val_t val = key_NSEC3_common(k, nsec3_name /*only zname required*/,
                                                nsec_p_hash);
        if (!val.data) return val;
-       int len = base32hex_decode(nsec3_name + 1, nsec3_name[0], val.data + val.len,
-                                  KR_CACHE_KEY_MAXLEN - val.len);
+       int len = base32hex_decode(nsec3_name + 1, nsec3_name[0],
+                       knot_db_val_bound(val), KR_CACHE_KEY_MAXLEN - val.len);
        if (len != NSEC3_HASH_LEN) {
                return VAL_EMPTY;
        }
@@ -116,7 +116,7 @@ static knot_db_val_t key_NSEC3_name(struct key *k, const knot_dname_t *name,
                assert(false);
                return VAL_EMPTY;
        }
-       memcpy(val.data + val.len, hash.data, NSEC3_HASH_LEN);
+       memcpy(knot_db_val_bound(val), hash.data, NSEC3_HASH_LEN);
        free(hash.data);
        #endif
 
@@ -191,7 +191,7 @@ static const char * find_leq_NSEC3(struct kr_cache *cache, const struct kr_query
                *new_ttl = new_ttl_;
        }
        if (hash_low) {
-               *hash_low = key_found.data + hash_off;
+               *hash_low = (uint8_t *)key_found.data + hash_off;
        }
        if (is_exact) {
                /* Nothing else to do. */
@@ -221,11 +221,11 @@ static const char * find_leq_NSEC3(struct kr_cache *cache, const struct kr_query
                return "unexpected next hash length";
        }
        /* B. do the actual range check. */
-       const uint8_t * const hash_searched = key.data + hash_off;
+       const uint8_t * const hash_searched = (uint8_t *)key.data + hash_off;
        bool covers = /* we know for sure that the low end is before the searched name */
                nsec3_hash_ordered(hash_searched, hash_next)
                /* and the wrap-around case */
-               || nsec3_hash_ordered(hash_next, key_found.data + hash_off);
+               || nsec3_hash_ordered(hash_next, (const uint8_t *)key_found.data + hash_off);
        if (!covers) {
                return "range search miss (!covers)";
        }
@@ -237,7 +237,7 @@ static const char * find_leq_NSEC3(struct kr_cache *cache, const struct kr_query
 static void key_NSEC3_hash2text(const knot_db_val_t key, char *text)
 {
        assert(key.data && key.len > NSEC3_HASH_LEN);
-       const uint8_t *hash_raw = key.data + key.len - NSEC3_HASH_LEN;
+       const uint8_t *hash_raw = knot_db_val_bound(key) - NSEC3_HASH_LEN;
                        /* CACHE_KEY_DEF ^^ */
        int len = base32hex_encode(hash_raw, NSEC3_HASH_LEN, (uint8_t *)text,
                                   NSEC3_HASH_TXT_LEN);
@@ -341,9 +341,8 @@ int nsec3_encloser(struct key *k, struct answer *ans,
                                 ? AR_CPE : AR_NSEC;
                {
                        const struct entry_h *nsec_eh = val.data;
-                       const void *nsec_eh_bound = val.data + val.len;
                        memset(&ans->rrsets[ans_id], 0, sizeof(ans->rrsets[ans_id]));
-                       int ret = entry2answer(ans, ans_id, nsec_eh, nsec_eh_bound,
+                       int ret = entry2answer(ans, ans_id, nsec_eh, knot_db_val_bound(val),
                                                owner, KNOT_RRTYPE_NSEC3, new_ttl);
                        if (ret) return kr_error(ret);
                }
@@ -458,8 +457,7 @@ int nsec3_src_synth(struct key *k, struct answer *ans, const knot_dname_t *clenc
                int ret = dname_wire_reconstruct(owner, k->zname, hash_low);
                if (unlikely(ret)) return kr_ok();
                const struct entry_h *nsec_eh = val.data;
-               const void *nsec_eh_bound = val.data + val.len;
-               ret = entry2answer(ans, AR_WILD, nsec_eh, nsec_eh_bound,
+               ret = entry2answer(ans, AR_WILD, nsec_eh, knot_db_val_bound(val),
                                   owner, KNOT_RRTYPE_NSEC3, new_ttl);
                if (ret) return kr_error(ret);
        }