summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVladimír Čunát <vladimir.cunat@nic.cz>2024-07-10 18:15:35 +0200
committerOto Šťáva <oto.stava@nic.cz>2024-07-11 14:51:35 +0200
commit241b5a727ece5ec10ff94f1254189c99d43907e0 (patch)
tree8d1446a64860775fe9602a2fdf7cc1492434caad
parentMerge branch 'drop-libknot-3.2' into 'master' (diff)
downloadknot-resolver-241b5a727ece5ec10ff94f1254189c99d43907e0.tar.xz
knot-resolver-241b5a727ece5ec10ff94f1254189c99d43907e0.zip
treewide nit: avoid NULL arithmetics
(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
-rw-r--r--contrib/mempattern.c2
-rw-r--r--contrib/mempattern.h2
-rw-r--r--daemon/bindings/net.c4
-rw-r--r--daemon/network.c6
-rw-r--r--lib/generic/lru.c4
-rw-r--r--lib/generic/test_trie.c6
-rw-r--r--lib/generic/trie.c2
7 files changed, 13 insertions, 13 deletions
diff --git a/contrib/mempattern.c b/contrib/mempattern.c
index 6c237eac..e0c784de 100644
--- a/contrib/mempattern.c
+++ b/contrib/mempattern.c
@@ -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) {
diff --git a/contrib/mempattern.h b/contrib/mempattern.h
index 4db147ae..7cb2bae8 100644
--- a/contrib/mempattern.h
+++ b/contrib/mempattern.h
@@ -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. */
diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c
index aaeef238..55e9a914 100644
--- a/daemon/bindings/net.c
+++ b/daemon/bindings/net.c
@@ -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;
diff --git a/daemon/network.c b/daemon/network.c
index 1ec34e90..7d1d5155 100644
--- a/daemon/network.c
+++ b/daemon/network.c
@@ -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;
}
diff --git a/lib/generic/lru.c b/lib/generic/lru.c
index 857b20b3..71b8730b 100644
--- a/lib/generic/lru.c
+++ b/lib/generic/lru.c
@@ -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. */
diff --git a/lib/generic/test_trie.c b/lib/generic/test_trie.c
index 9ecd67cd..ce164906 100644
--- a/lib/generic/test_trie.c
+++ b/lib/generic/test_trie.c
@@ -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;
diff --git a/lib/generic/trie.c b/lib/generic/trie.c
index 21254eb4..e2ce061e 100644
--- a/lib/generic/trie.c
+++ b/lib/generic/trie.c
@@ -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