Skip to content

Commit 18d17d0

Browse files
yaakov-steinqdeslandes
authored andcommitted
lib: helper: simplify and extend _BF_APPLY macros
Use recursive chaining where each _BF_APPLYn calls _BF_APPLYn-1. Also change BF_FLAG to use 1ULL to avoid UB when enum values reach 31+ (bf_matcher already has 31 values).
1 parent 9c60bcd commit 18d17d0

File tree

3 files changed

+27
-62
lines changed

3 files changed

+27
-62
lines changed

src/libbpfilter/include/bpfilter/helper.h

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,29 @@ extern const char *strerrordesc_np(int errnum);
1717

1818
#define _BF_APPLY0(t, s, dummy)
1919
#define _BF_APPLY1(t, s, a) t(a)
20-
#define _BF_APPLY2(t, s, a, b) t(a) s t(b)
21-
#define _BF_APPLY3(t, s, a, b, c) \
22-
t(a) s t(b) \
23-
s t(c)
24-
#define _BF_APPLY4(t, s, a, b, c, d) \
25-
t(a) s t(b) \
26-
s t(c) \
27-
s t(d)
28-
#define _BF_APPLY5(t, s, a, b, c, d, e) \
29-
t(a) s t(b) \
30-
s t(c) \
31-
s t(d) \
32-
s t(e)
33-
#define _BF_APPLY6(t, s, a, b, c, d, e, f) \
34-
t(a) s t(b) \
35-
s t(c) \
36-
s t(d) \
37-
s t(e) \
38-
s t(f)
39-
#define _BF_APPLY7(t, s, a, b, c, d, e, f, g) \
40-
t(a) s t(b) \
41-
s t(c) \
42-
s t(d) \
43-
s t(e) \
44-
s t(f) \
45-
s t(g)
46-
#define _BF_APPLY8(t, s, a, b, c, d, e, f, g, h) \
47-
t(a) s t(b) \
48-
s t(c) \
49-
s t(d) \
50-
s t(e) \
51-
s t(f) \
52-
s t(g) \
53-
s t(h)
54-
#define _BF_APPLY9(t, s, a, b, c, d, e, f, g, h, i) \
55-
t(a) s t(b) \
56-
s t(c) \
57-
s t(d) \
58-
s t(e) \
59-
s t(f) \
60-
s t(g) \
61-
s t(h) \
62-
s t(i)
63-
#define _BF_APPLY10(t, s, a, b, c, d, e, f, g, h, i, j) \
64-
t(a) s t(b) \
65-
s t(c) \
66-
s t(d) \
67-
s t(e) \
68-
s t(f) \
69-
s t(g) \
70-
s t(h) \
71-
s t(i) \
72-
s t(j)
73-
74-
#define __BF_NUM_ARGS1(dummy, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
75-
...) \
20+
#define _BF_APPLY2(t, s, a, ...) t(a) s _BF_APPLY1(t, s, __VA_ARGS__)
21+
#define _BF_APPLY3(t, s, a, ...) t(a) s _BF_APPLY2(t, s, __VA_ARGS__)
22+
#define _BF_APPLY4(t, s, a, ...) t(a) s _BF_APPLY3(t, s, __VA_ARGS__)
23+
#define _BF_APPLY5(t, s, a, ...) t(a) s _BF_APPLY4(t, s, __VA_ARGS__)
24+
#define _BF_APPLY6(t, s, a, ...) t(a) s _BF_APPLY5(t, s, __VA_ARGS__)
25+
#define _BF_APPLY7(t, s, a, ...) t(a) s _BF_APPLY6(t, s, __VA_ARGS__)
26+
#define _BF_APPLY8(t, s, a, ...) t(a) s _BF_APPLY7(t, s, __VA_ARGS__)
27+
#define _BF_APPLY9(t, s, a, ...) t(a) s _BF_APPLY8(t, s, __VA_ARGS__)
28+
#define _BF_APPLY10(t, s, a, ...) t(a) s _BF_APPLY9(t, s, __VA_ARGS__)
29+
#define _BF_APPLY11(t, s, a, ...) t(a) s _BF_APPLY10(t, s, __VA_ARGS__)
30+
#define _BF_APPLY12(t, s, a, ...) t(a) s _BF_APPLY11(t, s, __VA_ARGS__)
31+
#define _BF_APPLY13(t, s, a, ...) t(a) s _BF_APPLY12(t, s, __VA_ARGS__)
32+
#define _BF_APPLY14(t, s, a, ...) t(a) s _BF_APPLY13(t, s, __VA_ARGS__)
33+
#define _BF_APPLY15(t, s, a, ...) t(a) s _BF_APPLY14(t, s, __VA_ARGS__)
34+
#define _BF_APPLY16(t, s, a, ...) t(a) s _BF_APPLY15(t, s, __VA_ARGS__)
35+
36+
#define __BF_NUM_ARGS1(dummy, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, \
37+
x6, x5, x4, x3, x2, x1, x0, ...) \
7638
x0
7739
#define _BF_NUM_ARGS(...) \
78-
__BF_NUM_ARGS1(dummy, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
40+
__BF_NUM_ARGS1(dummy, ##__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, \
41+
6, 5, 4, 3, 2, 1, 0)
42+
7943
#define ___BF_APPLY_ALL(t, s, n, ...) _BF_APPLY##n(t, s, __VA_ARGS__)
8044
#define __BF_APPLY_ALL(t, s, n, ...) ___BF_APPLY_ALL(t, s, n, __VA_ARGS__)
8145
#define _BF_APPLY_ALL(t, s, ...) \
@@ -112,9 +76,9 @@ extern const char *strerrordesc_np(int errnum);
11276
*
11377
* @see `BF_FLAGS`
11478
*
115-
* @return `1 << n` to be used as a flag.
79+
* @return `1ULL << n` to be used as a flag.
11680
*/
117-
#define BF_FLAG(n) (1 << (n))
81+
#define BF_FLAG(n) (1ULL << (n))
11882

11983
/**
12084
* @brief Generate a bitmask covering all valid flags below `max_value`.

src/libbpfilter/include/bpfilter/matcher.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ enum bf_matcher_type
113113
_BF_MATCHER_TYPE_MAX,
114114
};
115115

116+
/// @todo Change `bf_matcher_type` bitmasks to 64 bits.
117+
static_assert(_BF_MATCHER_TYPE_MAX <= 8 * sizeof(uint32_t),
118+
"too many matcher types for uint32_t bitmask");
119+
116120
/**
117121
* Defines the structure of the payload for bf_matcher's
118122
* @ref BF_MATCHER_IP4_SADDR and @ref BF_MATCHER_IP4_DADDR types.

src/libbpfilter/set.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ int bf_set_new(struct bf_set **set, const char *name, enum bf_matcher_type *key,
3131
assert(set);
3232
assert(key);
3333

34-
static_assert(_BF_MATCHER_TYPE_MAX < 8 * sizeof(uint32_t),
35-
"matcher type bitmask won't fit in 32 bits");
36-
3734
if (n_comps == 0)
3835
return bf_err_r(-EINVAL, "at least 1 key component is required");
3936

0 commit comments

Comments
 (0)