lkml.org 
[lkml]   [2022]   [Jun]   [3]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
Subject[PATCH] libbpf: Fix is_pow_of_2
From
From: Yuze Chi <chiyuze@google.com>

There is a missing not. Consider a power of 2 number like 4096:

x && (x & (x - 1))
4096 && (4096 & (4096 - 1))
4096 && (4096 & 4095)
4096 && 0
0

with the not this is:
x && !(x & (x - 1))
4096 && !(4096 & (4096 - 1))
4096 && !(4096 & 4095)
4096 && !0
4096 && 1
1

Reported-by: Yuze Chi <chiyuze@google.com>
Signed-off-by: Yuze Chi <chiyuze@google.com>
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/lib/bpf/libbpf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3f4f18684bd3..fd0414ea00df 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);

static bool is_pow_of_2(size_t x)
{
- return x && (x & (x - 1));
+ return x && !(x & (x - 1));
}

static size_t adjust_ringbuf_sz(size_t sz)
--
2.36.1.255.ge46751e96f-goog
\
 
 \ /
  Last update: 2022-06-03 06:17    [W:0.129 / U:0.176 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site