siphash24: introduce siphash24_compress_typesafe() macro

To prevent copy-and-paste mistake.

This also introduce in_addr_hash_func().

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2023-12-24 17:51:30 +09:00
parent ac1b7b9e19
commit c01a5c0527
38 changed files with 177 additions and 162 deletions

View File

@@ -0,0 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
@@
expression p, s;
@@
- siphash24_compress(&p, sizeof(p), s);
+ siphash24_compress_typesafe(p, s);

View File

@@ -59,8 +59,8 @@ void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *state) {
assert(p);
assert(state);
siphash24_compress(&p->length, sizeof(p->length), state);
siphash24_compress(p->bytes, p->length, state);
siphash24_compress_typesafe(p->length, state);
siphash24_compress_safe(p->bytes, p->length, state);
}
DEFINE_HASH_OPS(hw_addr_hash_ops, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare);
@@ -106,7 +106,7 @@ int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
}
static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
siphash24_compress(p, sizeof(struct ether_addr), state);
siphash24_compress_typesafe(*p, state);
}
DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);

View File

@@ -33,7 +33,7 @@ void path_hash_func(const char *q, struct siphash *state) {
/* if path is absolute, add one "/" to the hash. */
if (path_is_absolute(q))
siphash24_compress("/", 1, state);
siphash24_compress_byte('/', state);
for (;;) {
const char *e;
@@ -67,7 +67,7 @@ DEFINE_HASH_OPS_FULL(path_hash_ops_free_free,
void, free);
void trivial_hash_func(const void *p, struct siphash *state) {
siphash24_compress(&p, sizeof(p), state);
siphash24_compress_typesafe(p, state);
}
int trivial_compare_func(const void *a, const void *b) {
@@ -93,7 +93,7 @@ const struct hash_ops trivial_hash_ops_free_free = {
};
void uint64_hash_func(const uint64_t *p, struct siphash *state) {
siphash24_compress(p, sizeof(uint64_t), state);
siphash24_compress_typesafe(*p, state);
}
int uint64_compare_func(const uint64_t *a, const uint64_t *b) {
@@ -104,7 +104,7 @@ DEFINE_HASH_OPS(uint64_hash_ops, uint64_t, uint64_hash_func, uint64_compare_func
#if SIZEOF_DEV_T != 8
void devt_hash_func(const dev_t *p, struct siphash *state) {
siphash24_compress(p, sizeof(dev_t), state);
siphash24_compress_typesafe(*p, state);
}
#endif

View File

@@ -922,12 +922,19 @@ int in_addr_prefix_from_string_auto_internal(
}
void in_addr_hash_func(const union in_addr_union *u, int family, struct siphash *state) {
assert(u);
assert(state);
siphash24_compress(u->bytes, FAMILY_ADDRESS_SIZE(family), state);
}
void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state) {
assert(a);
assert(state);
siphash24_compress(&a->family, sizeof(a->family), state);
siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state);
siphash24_compress_typesafe(a->family, state);
in_addr_hash_func(&a->address, a->family, state);
}
int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y) {
@@ -960,7 +967,7 @@ void in6_addr_hash_func(const struct in6_addr *addr, struct siphash *state) {
assert(addr);
assert(state);
siphash24_compress(addr, sizeof(*addr), state);
siphash24_compress_typesafe(*addr, state);
}
int in6_addr_compare_func(const struct in6_addr *a, const struct in6_addr *b) {

View File

@@ -185,6 +185,7 @@ static inline size_t FAMILY_ADDRESS_SIZE(int family) {
* See also oss-fuzz#11344. */
#define IN_ADDR_NULL ((union in_addr_union) { .in6 = {} })
void in_addr_hash_func(const union in_addr_union *u, int family, struct siphash *state);
void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state);
int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y);
void in6_addr_hash_func(const struct in6_addr *addr, struct siphash *state);

View File

@@ -303,7 +303,7 @@ bool pidref_is_self(const PidRef *pidref) {
}
static void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
siphash24_compress(&pidref->pid, sizeof(pidref->pid), state);
siphash24_compress_typesafe(pidref->pid, state);
}
static int pidref_compare_func(const PidRef *a, const PidRef *b) {

View File

@@ -22,15 +22,16 @@ struct siphash {
void siphash24_init(struct siphash *state, const uint8_t k[static 16]);
void siphash24_compress(const void *in, size_t inlen, struct siphash *state);
#define siphash24_compress_byte(byte, state) siphash24_compress((const uint8_t[]) { (byte) }, 1, (state))
#define siphash24_compress_typesafe(in, state) \
siphash24_compress(&(in), sizeof(typeof(in)), (state))
static inline void siphash24_compress_boolean(bool in, struct siphash *state) {
uint8_t i = in;
siphash24_compress(&i, sizeof i, state);
siphash24_compress_byte(in, state);
}
static inline void siphash24_compress_usec_t(usec_t in, struct siphash *state) {
uint64_t u = htole64(in);
siphash24_compress(&u, sizeof u, state);
siphash24_compress_typesafe(u, state);
}
static inline void siphash24_compress_safe(const void *in, size_t inlen, struct siphash *state) {

View File

@@ -476,8 +476,8 @@ int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
}
void inode_hash_func(const struct stat *q, struct siphash *state) {
siphash24_compress(&q->st_dev, sizeof(q->st_dev), state);
siphash24_compress(&q->st_ino, sizeof(q->st_ino), state);
siphash24_compress_typesafe(q->st_dev, state);
siphash24_compress_typesafe(q->st_ino, state);
}
int inode_compare_func(const struct stat *a, const struct stat *b) {

View File

@@ -45,8 +45,8 @@ static int bpf_foreign_key_compare_func(const BPFForeignKey *a, const BPFForeign
}
static void bpf_foreign_key_hash_func(const BPFForeignKey *p, struct siphash *h) {
siphash24_compress(&p->prog_id, sizeof(p->prog_id), h);
siphash24_compress(&p->attach_type, sizeof(p->attach_type), h);
siphash24_compress_typesafe(p->prog_id, h);
siphash24_compress_typesafe(p->attach_type, h);
}
DEFINE_PRIVATE_HASH_OPS_FULL(bpf_foreign_by_key_hash_ops,

View File

@@ -406,11 +406,11 @@ static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) {
assert(s);
if (s->peer.sa.sa_family == AF_INET)
siphash24_compress(&s->peer.in.sin_addr, sizeof(s->peer.in.sin_addr), state);
siphash24_compress_typesafe(s->peer.in.sin_addr, state);
else if (s->peer.sa.sa_family == AF_INET6)
siphash24_compress(&s->peer.in6.sin6_addr, sizeof(s->peer.in6.sin6_addr), state);
siphash24_compress_typesafe(s->peer.in6.sin6_addr, state);
else if (s->peer.sa.sa_family == AF_VSOCK)
siphash24_compress(&s->peer.vm.svm_cid, sizeof(s->peer.vm.svm_cid), state);
siphash24_compress_typesafe(s->peer.vm.svm_cid, state);
else
assert_not_reached();
}

View File

@@ -192,9 +192,9 @@ static uint64_t timer_get_fixed_delay_hash(Timer *t) {
}
siphash24_init(&state, hash_key);
siphash24_compress(&machine_id, sizeof(sd_id128_t), &state);
siphash24_compress_typesafe(machine_id, &state);
siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t)->manager), &state);
siphash24_compress(&uid, sizeof(uid_t), &state);
siphash24_compress_typesafe(uid, &state);
siphash24_compress_string(UNIT(t)->id, &state);
return siphash24_finalize(&state);

View File

@@ -14,10 +14,10 @@ static void lldp_neighbor_id_hash_func(const LLDPNeighborID *id, struct siphash
assert(id);
assert(state);
siphash24_compress(id->chassis_id, id->chassis_id_size, state);
siphash24_compress(&id->chassis_id_size, sizeof(id->chassis_id_size), state);
siphash24_compress(id->port_id, id->port_id_size, state);
siphash24_compress(&id->port_id_size, sizeof(id->port_id_size), state);
siphash24_compress_safe(id->chassis_id, id->chassis_id_size, state);
siphash24_compress_typesafe(id->chassis_id_size, state);
siphash24_compress_safe(id->port_id, id->port_id_size, state);
siphash24_compress_typesafe(id->port_id_size, state);
}
int lldp_neighbor_id_compare_func(const LLDPNeighborID *x, const LLDPNeighborID *y) {

View File

@@ -132,7 +132,7 @@ void client_id_hash_func(const DHCPClientId *id, struct siphash *state) {
assert(id->length > 0);
assert(id->data);
siphash24_compress(&id->length, sizeof(id->length), state);
siphash24_compress_typesafe(id->length, state);
siphash24_compress(id->data, id->length, state);
}

View File

@@ -2231,8 +2231,8 @@ static int inode_data_compare(const struct inode_data *x, const struct inode_dat
static void inode_data_hash_func(const struct inode_data *d, struct siphash *state) {
assert(d);
siphash24_compress(&d->dev, sizeof(d->dev), state);
siphash24_compress(&d->ino, sizeof(d->ino), state);
siphash24_compress_typesafe(d->dev, state);
siphash24_compress_typesafe(d->ino, state);
}
DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops, struct inode_data, inode_data_hash_func, inode_data_compare);

View File

@@ -191,11 +191,11 @@ int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id) {
}
void id128_hash_func(const sd_id128_t *p, struct siphash *state) {
siphash24_compress(p, sizeof(sd_id128_t), state);
siphash24_compress_typesafe(*p, state);
}
int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) {
return memcmp(a, b, 16);
return memcmp(a, b, sizeof(sd_id128_t));
}
sd_id128_t id128_make_v4_uuid(sd_id128_t id) {

View File

@@ -55,7 +55,7 @@ typedef struct CatalogItem {
} CatalogItem;
static void catalog_hash_func(const CatalogItem *i, struct siphash *state) {
siphash24_compress(&i->id, sizeof(i->id), state);
siphash24_compress_typesafe(i->id, state);
siphash24_compress_string(i->language, state);
}

View File

@@ -91,8 +91,8 @@ static int netdev_vlan_fill_message_create(NetDev *netdev, Link *link, sd_netlin
}
static void vlan_qos_maps_hash_func(const struct ifla_vlan_qos_mapping *x, struct siphash *state) {
siphash24_compress(&x->from, sizeof(x->from), state);
siphash24_compress(&x->to, sizeof(x->to), state);
siphash24_compress_typesafe(x->from, state);
siphash24_compress_typesafe(x->to, state);
}
static int vlan_qos_maps_compare_func(const struct ifla_vlan_qos_mapping *a, const struct ifla_vlan_qos_mapping *b) {

View File

@@ -121,7 +121,7 @@ static void generate_stable_private_address_one(
if (link->ssid)
siphash24_compress_string(link->ssid, &state);
siphash24_compress(&dad_counter, sizeof(uint8_t), &state);
siphash24_compress_typesafe(dad_counter, &state);
rid = htole64(siphash24_finalize(&state));
@@ -269,8 +269,8 @@ int radv_generate_addresses(Link *link, Set *tokens, const struct in6_addr *pref
}
static void ipv6_token_hash_func(const IPv6Token *p, struct siphash *state) {
siphash24_compress(&p->type, sizeof(p->type), state);
siphash24_compress(&p->address, sizeof(p->address), state);
siphash24_compress_typesafe(p->type, state);
siphash24_compress_typesafe(p->address, state);
id128_hash_func(&p->secret_key, state);
}

View File

@@ -400,25 +400,25 @@ static int address_ipv4_prefix(const Address *a, struct in_addr *ret) {
static void address_hash_func(const Address *a, struct siphash *state) {
assert(a);
siphash24_compress(&a->family, sizeof(a->family), state);
siphash24_compress_typesafe(a->family, state);
switch (a->family) {
case AF_INET: {
struct in_addr prefix;
siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
siphash24_compress_typesafe(a->prefixlen, state);
assert_se(address_ipv4_prefix(a, &prefix) >= 0);
siphash24_compress(&prefix, sizeof(prefix), state);
siphash24_compress_typesafe(prefix, state);
siphash24_compress(&a->in_addr.in, sizeof(a->in_addr.in), state);
siphash24_compress_typesafe(a->in_addr.in, state);
break;
}
case AF_INET6:
siphash24_compress(&a->in_addr.in6, sizeof(a->in_addr.in6), state);
siphash24_compress_typesafe(a->in_addr.in6, state);
if (in6_addr_is_null(&a->in_addr.in6))
siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
siphash24_compress_typesafe(a->prefixlen, state);
break;
default:

View File

@@ -635,7 +635,7 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
}
static void ndisc_rdnss_hash_func(const NDiscRDNSS *x, struct siphash *state) {
siphash24_compress(&x->address, sizeof(x->address), state);
siphash24_compress_typesafe(x->address, state);
}
static int ndisc_rdnss_compare_func(const NDiscRDNSS *a, const NDiscRDNSS *b) {
@@ -941,8 +941,8 @@ static int ndisc_router_process_captive_portal(Link *link, sd_ndisc_router *rt)
static void ndisc_pref64_hash_func(const NDiscPREF64 *x, struct siphash *state) {
assert(x);
siphash24_compress(&x->prefix_len, sizeof(x->prefix_len), state);
siphash24_compress(&x->prefix, sizeof(x->prefix), state);
siphash24_compress_typesafe(x->prefix_len, state);
siphash24_compress_typesafe(x->prefix, state);
}
static int ndisc_pref64_compare_func(const NDiscPREF64 *a, const NDiscPREF64 *b) {

Some files were not shown because too many files have changed in this diff Show More