Skip to content

Commit e72cc46

Browse files
pzmarzlyqdeslandes
authored andcommitted
cli: fix parsing bf_hook enum
1 parent 5a31a46 commit e72cc46

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

src/bfcli/parser.y

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,16 @@ chain : CHAIN STRING hook hookopts verdict sets rules
171171

172172
verdict : VERDICT
173173
{
174-
enum bf_verdict verdict;
175-
176-
if (bf_verdict_from_str($1, &verdict) < 0)
174+
if (bf_verdict_from_str($1, &$$) < 0)
177175
bf_parse_err("unknown verdict '%s'\n", $1);
178-
179176
free($1);
180-
$$ = verdict;
181177
}
182178

183179
hook : HOOK
184180
{
185-
enum bf_hook hook = bf_hook_from_str($1);
186-
if (hook < 0)
181+
if (bf_hook_from_str($1, &$$) < 0)
187182
bf_parse_err("unknown hook '%s'\n", $1);
188-
189183
free($1);
190-
$$ = hook;
191184
}
192185

193186
hookopts : %empty { $$ = NULL; }
@@ -464,24 +457,16 @@ matcher : matcher_type matcher_op RAW_PAYLOAD
464457
;
465458
matcher_type : MATCHER_TYPE
466459
{
467-
enum bf_matcher_type type;
468-
469-
if (bf_matcher_type_from_str($1, &type) < 0)
460+
if (bf_matcher_type_from_str($1, &$$) < 0)
470461
bf_parse_err("unknown matcher type '%s'\n", $1);
471-
472462
free($1);
473-
$$ = type;
474463
}
475464
matcher_op : %empty { $$ = BF_MATCHER_EQ; }
476465
| MATCHER_OP
477466
{
478-
enum bf_matcher_op op;
479-
480-
if (bf_matcher_op_from_str($1, &op) < 0)
467+
if (bf_matcher_op_from_str($1, &$$) < 0)
481468
bf_parse_err("unknown matcher operator '%s'\n", $1);
482-
483469
free($1);
484-
$$ = op;
485470
}
486471
;
487472

src/libbpfilter/hook.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ static_assert(ARRAY_SIZE(_bf_hook_strs) == _BF_HOOK_MAX,
4141

4242
const char *bf_hook_to_str(enum bf_hook hook)
4343
{
44+
if (hook < 0 || hook >= _BF_HOOK_MAX)
45+
return "<bf_hook unknown>";
46+
4447
return _bf_hook_strs[hook];
4548
}
4649

47-
enum bf_hook bf_hook_from_str(const char *str)
50+
int bf_hook_from_str(const char *str, enum bf_hook *hook)
4851
{
49-
bf_assert(str);
52+
assert(hook);
5053

51-
for (enum bf_hook hook = 0; hook < _BF_HOOK_MAX; ++hook) {
52-
if (bf_streq(_bf_hook_strs[hook], str))
53-
return hook;
54+
for (enum bf_hook i = 0; i < _BF_HOOK_MAX; ++i) {
55+
if (bf_streq(_bf_hook_strs[i], str)) {
56+
*hook = i;
57+
return 0;
58+
}
5459
}
5560

5661
return -EINVAL;

src/libbpfilter/include/bpfilter/hook.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ const char *bf_hook_to_str(enum bf_hook hook);
7474
* Convert a string to a `bf_hook` value.
7575
*
7676
* @param str String to convert to a `bf_hook` value. Can't be NULL.
77-
* @return A valid `bf_hook` value on success, or a negative errno value
78-
* on error.
77+
* @param hook Hook type value, if the parsing succeeds. Can't be NULL.
78+
* @return 0 on success, or a negative errno value on error.
7979
*/
80-
enum bf_hook bf_hook_from_str(const char *str);
80+
int bf_hook_from_str(const char *str, enum bf_hook *hook);
8181

8282
/**
8383
* Convert a `bf_hook` value to a `bf_flavor` value.

tests/unit/libbpfilter/hook.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,8 @@ static void hook_from_str(void **state)
3535
{
3636
(void)state;
3737

38-
// Test valid conversions
39-
assert_int_equal(bf_hook_from_str("BF_HOOK_XDP"), BF_HOOK_XDP);
40-
assert_int_equal(bf_hook_from_str("BF_HOOK_TC_INGRESS"),
41-
BF_HOOK_TC_INGRESS);
42-
assert_int_equal(bf_hook_from_str("BF_HOOK_NF_PRE_ROUTING"),
43-
BF_HOOK_NF_PRE_ROUTING);
44-
assert_int_equal(bf_hook_from_str("BF_HOOK_TC_EGRESS"), BF_HOOK_TC_EGRESS);
45-
46-
// Test round-trip for all hooks
47-
for (enum bf_hook hook = 0; hook < _BF_HOOK_MAX; ++hook) {
48-
const char *str = bf_hook_to_str(hook);
49-
assert_int_equal(bf_hook_from_str(str), hook);
50-
}
51-
52-
// Test invalid strings
53-
assert_err((int)bf_hook_from_str("BF_HOOK_XD"));
54-
assert_err((int)bf_hook_from_str("invalid"));
55-
assert_err((int)bf_hook_from_str("BF_HOOK"));
38+
assert_enum_to_from_str(enum bf_hook, bf_hook_to_str, bf_hook_from_str, BF_HOOK_XDP,
39+
_BF_HOOK_MAX);
5640
}
5741

5842
static void hook_to_flavor(void **state)

0 commit comments

Comments
 (0)