Skip to content

Commit

Permalink
Merge !1566: treewide nit: avoid NULL arithmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
vcunat committed Jul 16, 2024
2 parents 9f99d5b + 241b5a7 commit 7418381
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contrib/mempattern.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion contrib/mempattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions daemon/bindings/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions daemon/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/generic/lru.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
6 changes: 3 additions & 3 deletions lib/generic/test_trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/generic/trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7418381

Please sign in to comment.