Index | Thread | Search

From:
Claudio Jeker <cjeker@diehard.n-r-g.com>
Subject:
bgpd: fix chash on big-endian archs
To:
tech@openbsd.org
Date:
Sat, 22 Nov 2025 14:39:34 +0100

Download raw body.

Thread
Oups, found by denis@ chash did not work on big-endian archs :(

The problem is cg_meta_set_hash() that did an array access into the 64bit
cg_meta value. That is not endian safe. Instead do the write with a
combination of shift and mask operations like all the other functions do.

With this the chash regress test pass both on sparc64 and amd64.
Further tests welcome.
-- 
:wq Claudio

Index: chash.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/chash.c,v
diff -u -p -r1.4 chash.c
--- chash.c	4 Nov 2025 13:07:14 -0000	1.4
+++ chash.c	22 Nov 2025 13:26:28 -0000
@@ -121,9 +121,13 @@ cg_meta_check_flags(const struct ch_grou
 }
 
 static inline void
-cg_meta_set_hash(struct ch_group *g, int slot, uint8_t hash)
+cg_meta_set_hash(struct ch_group *g, int slot, uint64_t hash)
 {
-	((uint8_t *)&g->cg_meta)[slot] = hash;
+	uint64_t newval;
+
+	newval = g->cg_meta & ~(0xffULL << (slot * 8));
+	newval |= hash << (slot * 8);
+	g->cg_meta = newval;
 }
 
 /*