From 0149fadd4a8941b7a57071d1362e77e24a0207ea Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 6 Mar 2025 17:59:03 +0000 Subject: [PATCH] address: Never pass zero to __builtin_ctz This is undefined behaviour which worked just fine in GCC, but in clang the code is doing funny business. Signed-off-by: Michael Tremer --- src/libloc/address.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libloc/address.h b/src/libloc/address.h index 082c33f..ff6e943 100644 --- a/src/libloc/address.h +++ b/src/libloc/address.h @@ -132,6 +132,7 @@ static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) { static inline unsigned int loc_address_bit_length(const struct in6_addr* address) { unsigned int bitlength = 0; + int trailing_zeroes; int octet = 0; @@ -143,8 +144,14 @@ static inline unsigned int loc_address_bit_length(const struct in6_addr* address // Walk backwards until we find the first one foreach_octet_in_address_reverse(octet, address) { + // __builtin_ctz does not support zero as input + if (!address->s6_addr[octet]) { + bitlength -= 8; + continue; + } + // Count all trailing zeroes - int trailing_zeroes = __builtin_ctz(address->s6_addr[octet]); + trailing_zeroes = __builtin_ctz(address->s6_addr[octet]); // We only have one byte if (trailing_zeroes > 8) -- 2.39.5