[PATCH 1/2] NAN: Use __builtin_ffs() instead of ffs()

Chaitanya Tata chaitanya.mgit at gmail.com
Thu Sep 10 13:39:47 PDT 2026


ffs() is declared in <strings.h> only when __MISC_VISIBLE,
__XSI_VISIBLE >= 700 or __POSIX_VISIBLE < 200809 (glibc and picolibc
both use this scheme). Building with _POSIX_C_SOURCE=200809L and
without _DEFAULT_SOURCE/_XOPEN_SOURCE, a legitimate and fairly common
strict-POSIX build configuration, therefore hides the declaration and
fails with an implicit-function-declaration error.

Reproduced with plain gcc -D_POSIX_C_SOURCE=200809L (glibc), no
special build options otherwise required. Use the always-available
__builtin_ffs() compiler intrinsic instead, which needs no header at
all.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata at nordicsemi.no>
---
 src/nan/nan.c                   | 4 ++--
 src/nan/nan_util.c              | 6 +++---
 wpa_supplicant/nan_supplicant.c | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/nan/nan.c b/src/nan/nan.c
index 0a5439ad1..fc69da715 100644
--- a/src/nan/nan.c
+++ b/src/nan/nan.c
@@ -2933,7 +2933,7 @@ nan_peer_get_committed_avail_add(const struct nan_data *nan,
 		return;
 	}
 
-	idx = ffs(le_to_host16(bc_chan->chan_bitmap)) - 1;
+	idx = __builtin_ffs(le_to_host16(bc_chan->chan_bitmap)) - 1;
 	if (idx < 0) {
 		wpa_printf(MSG_DEBUG,
 			   "NAN: No channel found in chan_bitmap 0x%04x for oper_class %u",
@@ -2961,7 +2961,7 @@ nan_peer_get_committed_avail_add(const struct nan_data *nan,
 			   op->op_class);
 		return;
 	} else {
-		idx = ffs(bc_chan->pri_chan_bitmap) - 1;
+		idx = __builtin_ffs(bc_chan->pri_chan_bitmap) - 1;
 		if (idx < 0) {
 			wpa_printf(MSG_DEBUG,
 				   "NAN: No primary channel found in pri_chan_bitmap 0x%04x",
diff --git a/src/nan/nan_util.c b/src/nan/nan_util.c
index 78dcde0a3..22ddd7914 100644
--- a/src/nan/nan_util.c
+++ b/src/nan/nan_util.c
@@ -934,7 +934,7 @@ int nan_add_avail_attrs(struct nan_data *nan, u8 sequence_id,
 
 	while (map_ids_bitmap) {
 		struct nan_channels pot_chans;
-		u8 map_id = ffs(map_ids_bitmap) - 1;
+		u8 map_id = __builtin_ffs(map_ids_bitmap) - 1;
 		u16 ctrl = map_id << NAN_AVAIL_CTRL_MAP_ID_POS |
 			NAN_AVAIL_CTRL_POTENTIAL_CHANGED;
 
@@ -1462,7 +1462,7 @@ static int nan_get_control_channel(struct nan_data *nan, u8 op_class,
 	if (!op || op_class > 130)
 		return -1;
 
-	idx = ffs(cbm) - 1;
+	idx = __builtin_ffs(cbm) - 1;
 	if (idx < 0) {
 		wpa_printf(MSG_DEBUG,
 			   "NAN: No channel found in chan_bitmap 0x%04x for oper_class %u",
@@ -1498,7 +1498,7 @@ static int nan_get_control_channel(struct nan_data *nan, u8 op_class,
 		return -1;
 	}
 
-	idx = ffs(pri_cbm) - 1;
+	idx = __builtin_ffs(pri_cbm) - 1;
 
 	if (op->bw == BW80 || op->bw == BW80P80)
 		return freq - 30 + idx * 20;
diff --git a/wpa_supplicant/nan_supplicant.c b/wpa_supplicant/nan_supplicant.c
index 18057c366..ded125dda 100644
--- a/wpa_supplicant/nan_supplicant.c
+++ b/wpa_supplicant/nan_supplicant.c
@@ -2514,7 +2514,7 @@ wpas_nan_fill_ndp_schedule_chan(struct wpa_supplicant *wpa_s,
 #endif /* CONFIG_TESTING_OPTIONS */
 
 	tbm->duration = wpa_s->nan_capa.slot_duration >> 5;
-	tbm->period = ffs(wpa_s->nan_capa.schedule_period) - 7;
+	tbm->period = __builtin_ffs(wpa_s->nan_capa.schedule_period) - 7;
 	tbm->offset = 0;
 	tbm->len = bitmap_len;
 	os_memcpy(tbm->bitmap, bitmap_data, bitmap_len);
-- 
2.43.0




More information about the Hostap mailing list