]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
treewide nit: avoid NULL arithmetics docs-develop-avoi-rr4td0/deployments/4530
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 10 Jul 2024 16:15:35 +0000 (18:15 +0200)
committerOto Šťáva <oto.stava@nic.cz>
Thu, 11 Jul 2024 12:51:35 +0000 (14:51 +0200)
(u)intptr_t casts seem the best in terms of compliance:
https://stackoverflow.com/q/45220134/587396

Otherwise with clang 18 we can get warnings like
 ../$path:$line:$col: runtime error: applying non-zero offset $num to null pointer
 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../$path:$line:$col

contrib/mempattern.c
contrib/mempattern.h
daemon/bindings/net.c
daemon/network.c
lib/generic/lru.c
lib/generic/test_trie.c
lib/generic/trie.c

index 6c237eac72d8c7c2e5808f8a8f189e3405511833..e0c784de1507770183b4ee0ab634f78836d16ee1 100644 (file)
@@ -125,7 +125,7 @@ void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size)
 
 void *mm_malloc_aligned(void *ctx, size_t n)
 {
-       size_t alignment = (size_t)ctx;
+       size_t alignment = (uintptr_t)ctx;
        void *res;
        int err = posix_memalign(&res, alignment, n);
        if (err == 0) {
index 4db147ae5db4c8c6328a376c063a053c73f7a578..7cb2bae89e29102c3fe5523fb4d08ee28711456b 100644 (file)
@@ -81,7 +81,7 @@ static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment)
 {
        assert(__builtin_popcount(alignment) == 1);
        mm_ctx_init(mm);
-       mm->ctx = (uint8_t *)NULL + alignment; /*< roundabout to satisfy linters */
+       mm->ctx = (void *)(uintptr_t)alignment; /*< roundabout to satisfy linters */
        /* posix_memalign() doesn't allow alignment < sizeof(void*),
         * and there's no point in using it for small values anyway,
         * as plain malloc() guarantees at least max_align_t. */
index aaeef2387b94748865640ec9c467a23658664ded..55e9a91424d9397c6f64017bc2575291bfe661d5 100644 (file)
@@ -1191,7 +1191,7 @@ static int net_register_endpoint_kind(lua_State *L)
        if (param_count == 1) {
                void *val;
                if (trie_del(the_network->endpoint_kinds, kind, kind_len, &val) == KNOT_EOK) {
-                       const int fun_id = (char *)val - (char *)NULL;
+                       const int fun_id = (intptr_t)val;
                        luaL_unref(L, LUA_REGISTRYINDEX, fun_id);
                        return 0;
                }
@@ -1209,7 +1209,7 @@ static int net_register_endpoint_kind(lua_State *L)
        if (!pp) lua_error_maybe(L, kr_error(ENOMEM));
        if (*pp != NULL || !strcasecmp(kind, "dns") || !strcasecmp(kind, "tls"))
                lua_error_p(L, "attempt to register known kind '%s'\n", kind);
-       *pp = (char *)NULL + fun_id;
+       *pp = (void *)(intptr_t)fun_id;
        /* We don't attempt to engage corresponding endpoints now.
         * That's the job for network_engage_endpoints() later. */
        return 0;
index 1ec34e9070fc237be7d774ecdc392415268c3592..7d1d5155234523f79909a62aa67b58bb87f2c557 100644 (file)
@@ -102,7 +102,7 @@ static int endpoint_open_lua_cb(struct endpoint *ep,
        if (!pp) return kr_ok();
 
        /* Now execute the callback. */
-       const int fun_id = (char *)*pp - (char *)NULL;
+       const int fun_id = (intptr_t)*pp;
        lua_rawgeti(L, LUA_REGISTRYINDEX, fun_id);
        lua_pushboolean(L, true /* open */);
        lua_pushpointer(L, ep);
@@ -182,7 +182,7 @@ static void endpoint_close_lua_cb(struct endpoint *ep)
        }
        if (!pp) return;
 
-       const int fun_id = (char *)*pp - (char *)NULL;
+       const int fun_id = (intptr_t)*pp;
        lua_rawgeti(L, LUA_REGISTRYINDEX, fun_id);
        lua_pushboolean(L, false /* close */);
        lua_pushpointer(L, ep);
@@ -262,7 +262,7 @@ static int free_key(trie_val_t *val, void* ext)
 
 int kind_unregister(trie_val_t *tv, void *L)
 {
-       int fun_id = (char *)*tv - (char *)NULL;
+       int fun_id = (intptr_t)*tv;
        luaL_unref(L, LUA_REGISTRYINDEX, fun_id);
        return 0;
 }
index 857b20b3514530837dcfe17d7dbb437a603d8ad5..71b8730b71fce3bb50eed2780c98ca1a4c5ebb4e 100644 (file)
@@ -50,9 +50,9 @@ static uint item_size(const struct lru *lru, uint key_len, uint val_len)
 /** @internal Return pointer to value in an lru_item. */
 static void * item_val(const struct lru *lru, struct lru_item *it)
 {
-       size_t key_end = it->data + it->key_len - (char *)NULL;
+       size_t key_end = (uintptr_t)(it->data + it->key_len);
        size_t val_begin = round_power(key_end, lru->val_alignment);
-       return (char *)NULL + val_begin;
+       return (void *)(uintptr_t)val_begin;
 }
 
 /** @internal Free each item. */
index 9ecd67cd97d20a26074899c4ccb6e142fd913c6d..ce164906d35d3080e443f500137a2ad433c6fe77 100644 (file)
@@ -48,7 +48,7 @@ static void test_insert(void **state)
                trie_val_t *data = trie_get_ins(t, dict[i], KEY_LEN(dict[i]));
                assert_non_null(data);
                assert_null(*data);
-               *data = (char *)NULL + i; // yes, ugly
+               *data = (void *)(intptr_t)i; // yes, ugly
                assert_ptr_equal(trie_get_try(t, dict[i], KEY_LEN(dict[i])), data);
        }
        assert_int_equal(trie_weight(t), dict_size);
@@ -82,7 +82,7 @@ static void test_iter(void **state)
                const char *key = trie_it_key(it, &len);
                assert_int_equal(KEY_LEN(key), len);
                assert_string_equal(key, dict_sorted[i]);
-               assert_ptr_equal(dict[(char *)*trie_it_val(it) - (char *)NULL],
+               assert_ptr_equal(dict[(uintptr_t)*trie_it_val(it)],
                                 dict_sorted[i]);
        }
        assert_true(trie_it_finished(it));
@@ -100,7 +100,7 @@ static void test_queue(void **state)
                assert_non_null(key);
                assert_int_equal(len, KEY_LEN(key));
                assert_non_null(data);
-               ptrdiff_t key_i = (char *)*data - (char *)NULL;
+               uintptr_t key_i = (uintptr_t)*data;
                assert_string_equal(key, dict[key_i]);
 
                len = 30;
index 21254eb4a085c3125609c8706ac3f4d727a0e080..e2ce061e3a19de8532868316298fc408c6ad9fda 100644 (file)
@@ -116,7 +116,7 @@ static inline void empty_root(node_t *root) {
 static void assert_portability(void) {
 #if FLAGS_HACK
        kr_require(((union node){ .leaf = {
-                       .key = (tkey_t *)(((uint8_t *)NULL) + 1),
+                       .key = (tkey_t *)(void *)(uintptr_t)1,
                        .val = NULL
                } }).branch.flags == 1);
 #endif