[PATCH] bpf: fix netfilter link comparison to handle unsigned flags
From: Haofeng Li
Date: Fri Sep 19 2025 - 05:09:23 EST
From: lihaofeng <lihaofeng@xxxxxxxxxx>
The original implementation of netfilter_link_compar() used subtraction
to compare the netfilter.flags field, which is an unsigned type.
This could result in incorrect comparison results when the unsigned
value wrapped around due to underflow.
Changed the comparison logic for flags to use explicit conditional
checks (similar to how priority is handled) instead of subtraction,
ensuring correct negative/zero/positive return values regardless of
the underlying data type.
This fixes potential sorting issues when using this comparison function
with algorithms like qsort() or bsearch().
Signed-off-by: lihaofeng <lihaofeng@xxxxxxxxxx>
---
tools/bpf/bpftool/net.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index cfc6f944f7c3..9f840821beda 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -816,7 +816,11 @@ static int netfilter_link_compar(const void *a, const void *b)
if (nfa->netfilter.priority > nfb->netfilter.priority)
return 1;
- return nfa->netfilter.flags - nfb->netfilter.flags;
+ if (nfa->netfilter.flags < nfb->netfilter.flags)
+ return -1;
+ if (nfa->netfilter.flags > nfb->netfilter.flags)
+ return 1;
+ return 0;
}
static void show_link_netfilter(void)
--
2.25.1