[3/4] argparse: fix argument flags operate as uint32 type

Message ID 20240220131502.47510-4-fengchengwen@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series refine argparse library |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Chengwen Feng Feb. 20, 2024, 1:15 p.m. UTC
  The struct rte_argparse_arg's flags was 64bit type, uint64_t should be
used instead of uint32_t where the operation happened.

Also, the flags' bit16 was also unused, so don't test bit16 in testcase
test_argparse_invalid_arg_flags.

Fixes: 6c5c6571601c ("argparse: verify argument config")
Fixes: 31ed9f9f43bb ("argparse: parse parameters")

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test/test_argparse.c    | 16 ++++++++--------
 lib/argparse/rte_argparse.c |  4 ++--
 2 files changed, 10 insertions(+), 10 deletions(-)
  

Comments

David Marchand March 6, 2024, 5:34 p.m. UTC | #1
On Tue, Feb 20, 2024 at 2:16 PM Chengwen Feng <fengchengwen@huawei.com> wrote:
>
> The struct rte_argparse_arg's flags was 64bit type, uint64_t should be
> used instead of uint32_t where the operation happened.

Something is strange.
An enum in C is represented as an int.

Plus, this enum type is not used anywhere:
lib/argparse/rte_argparse.h:enum rte_argparse_flag {
lib/argparse/rte_argparse.h:    /** @see rte_argparse_flag */

I understand the flags are a bitmask.
So please remove this enum and define macros instead.


>
> Also, the flags' bit16 was also unused, so don't test bit16 in testcase
> test_argparse_invalid_arg_flags.
>
> Fixes: 6c5c6571601c ("argparse: verify argument config")
> Fixes: 31ed9f9f43bb ("argparse: parse parameters")
>
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> ---
>  app/test/test_argparse.c    | 16 ++++++++--------
>  lib/argparse/rte_argparse.c |  4 ++--
>  2 files changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
> index c98bcee56d..708a575e16 100644
> --- a/app/test/test_argparse.c
> +++ b/app/test/test_argparse.c
> @@ -188,7 +188,7 @@ test_argparse_invalid_arg_help(void)
>  static int
>  test_argparse_invalid_has_val(void)
>  {
> -       uint32_t set_mask[] = { 0,
> +       uint64_t set_mask[] = { 0,
>                                 RTE_ARGPARSE_ARG_NO_VALUE,
>                                 RTE_ARGPARSE_ARG_OPTIONAL_VALUE
>                               };
> @@ -197,7 +197,7 @@ test_argparse_invalid_has_val(void)
>         int ret;
>
>         obj = test_argparse_init_obj();
> -       obj->args[0].flags &= ~0x3u;
> +       obj->args[0].flags &= ~0x3ull;

If flags is a uint64_t, use RTE_BIT64().


I don't know the argparse API, but why do we need this hardcoded (and
hard to understand) ~3 value?
Can it be expressed with the flags defined in the API?


>         ret = rte_argparse_parse(obj, default_argc, default_argv);
>         TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
>
> @@ -205,7 +205,7 @@ test_argparse_invalid_has_val(void)
>                 obj = test_argparse_init_obj();
>                 obj->args[0].name_long = "abc";
>                 obj->args[0].name_short = NULL;
> -               obj->args[0].flags &= ~0x3u;
> +               obj->args[0].flags &= ~0x3ull;
>                 obj->args[0].flags |= set_mask[index];
>                 ret = rte_argparse_parse(obj, default_argc, default_argv);
>                 TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
> @@ -269,7 +269,7 @@ test_argparse_invalid_arg_flags(void)
>         int ret;
>
>         obj = test_argparse_init_obj();
> -       obj->args[0].flags |= ~0x107FFu;
> +       obj->args[0].flags |= ~0x7FFull;

Same comments as above.
  
Chengwen Feng March 7, 2024, 1:14 p.m. UTC | #2
Hi David,

On 2024/3/7 1:34, David Marchand wrote:
> On Tue, Feb 20, 2024 at 2:16 PM Chengwen Feng <fengchengwen@huawei.com> wrote:
>>
>> The struct rte_argparse_arg's flags was 64bit type, uint64_t should be
>> used instead of uint32_t where the operation happened.
> 
> Something is strange.
> An enum in C is represented as an int.
> 
> Plus, this enum type is not used anywhere:
> lib/argparse/rte_argparse.h:enum rte_argparse_flag {
> lib/argparse/rte_argparse.h:    /** @see rte_argparse_flag */
> 
> I understand the flags are a bitmask.
> So please remove this enum and define macros instead.

Thanks for point it out, already send v2.

> 
> 
>>
>> Also, the flags' bit16 was also unused, so don't test bit16 in testcase
>> test_argparse_invalid_arg_flags.
>>
>> Fixes: 6c5c6571601c ("argparse: verify argument config")
>> Fixes: 31ed9f9f43bb ("argparse: parse parameters")
>>
>> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
>> ---
>>  app/test/test_argparse.c    | 16 ++++++++--------
>>  lib/argparse/rte_argparse.c |  4 ++--
>>  2 files changed, 10 insertions(+), 10 deletions(-)
>>
>> diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
>> index c98bcee56d..708a575e16 100644
>> --- a/app/test/test_argparse.c
>> +++ b/app/test/test_argparse.c
>> @@ -188,7 +188,7 @@ test_argparse_invalid_arg_help(void)
>>  static int
>>  test_argparse_invalid_has_val(void)
>>  {
>> -       uint32_t set_mask[] = { 0,
>> +       uint64_t set_mask[] = { 0,
>>                                 RTE_ARGPARSE_ARG_NO_VALUE,
>>                                 RTE_ARGPARSE_ARG_OPTIONAL_VALUE
>>                               };
>> @@ -197,7 +197,7 @@ test_argparse_invalid_has_val(void)
>>         int ret;
>>
>>         obj = test_argparse_init_obj();
>> -       obj->args[0].flags &= ~0x3u;
>> +       obj->args[0].flags &= ~0x3ull;
> 
> If flags is a uint64_t, use RTE_BIT64().
> 
> 
> I don't know the argparse API, but why do we need this hardcoded (and
> hard to understand) ~3 value?

The lowest two bits was a represent whether has value.

> Can it be expressed with the flags defined in the API?

Its not part of API, I defined a macro in test_argparse.c to express its meaning in v2.

> 
> 
>>         ret = rte_argparse_parse(obj, default_argc, default_argv);
>>         TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
>>
>> @@ -205,7 +205,7 @@ test_argparse_invalid_has_val(void)
>>                 obj = test_argparse_init_obj();
>>                 obj->args[0].name_long = "abc";
>>                 obj->args[0].name_short = NULL;
>> -               obj->args[0].flags &= ~0x3u;
>> +               obj->args[0].flags &= ~0x3ull;
>>                 obj->args[0].flags |= set_mask[index];
>>                 ret = rte_argparse_parse(obj, default_argc, default_argv);
>>                 TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
>> @@ -269,7 +269,7 @@ test_argparse_invalid_arg_flags(void)
>>         int ret;
>>
>>         obj = test_argparse_init_obj();
>> -       obj->args[0].flags |= ~0x107FFu;
>> +       obj->args[0].flags |= ~0x7FFull;
> 
> Same comments as above.

Done in v2, defined macros in test_argpase.c and express with multi-marco's or value.

Thanks

> 
>
  

Patch

diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
index c98bcee56d..708a575e16 100644
--- a/app/test/test_argparse.c
+++ b/app/test/test_argparse.c
@@ -188,7 +188,7 @@  test_argparse_invalid_arg_help(void)
 static int
 test_argparse_invalid_has_val(void)
 {
-	uint32_t set_mask[] = { 0,
+	uint64_t set_mask[] = { 0,
 				RTE_ARGPARSE_ARG_NO_VALUE,
 				RTE_ARGPARSE_ARG_OPTIONAL_VALUE
 			      };
@@ -197,7 +197,7 @@  test_argparse_invalid_has_val(void)
 	int ret;
 
 	obj = test_argparse_init_obj();
-	obj->args[0].flags &= ~0x3u;
+	obj->args[0].flags &= ~0x3ull;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
@@ -205,7 +205,7 @@  test_argparse_invalid_has_val(void)
 		obj = test_argparse_init_obj();
 		obj->args[0].name_long = "abc";
 		obj->args[0].name_short = NULL;
-		obj->args[0].flags &= ~0x3u;
+		obj->args[0].flags &= ~0x3ull;
 		obj->args[0].flags |= set_mask[index];
 		ret = rte_argparse_parse(obj, default_argc, default_argv);
 		TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
@@ -269,7 +269,7 @@  test_argparse_invalid_arg_flags(void)
 	int ret;
 
 	obj = test_argparse_init_obj();
-	obj->args[0].flags |= ~0x107FFu;
+	obj->args[0].flags |= ~0x7FFull;
 	ret = rte_argparse_parse(obj, default_argc, default_argv);
 	TEST_ASSERT(ret == -EINVAL, "Argparse parse expect failed!");
 
@@ -337,7 +337,7 @@  test_argparse_invalid_option(void)
 static int
 test_argparse_opt_autosave_parse_int_of_no_val(void)
 {
-	uint32_t flags = RTE_ARGPARSE_ARG_NO_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
+	uint64_t flags = RTE_ARGPARSE_ARG_NO_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
 	struct rte_argparse *obj;
 	int val_saver = 0;
 	char *argv[2];
@@ -369,7 +369,7 @@  test_argparse_opt_autosave_parse_int_of_no_val(void)
 static int
 test_argparse_opt_autosave_parse_int_of_required_val(void)
 {
-	uint32_t flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
+	uint64_t flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
 	struct rte_argparse *obj;
 	int val_saver = 0;
 	char *argv[3];
@@ -410,7 +410,7 @@  test_argparse_opt_autosave_parse_int_of_required_val(void)
 static int
 test_argparse_opt_autosave_parse_int_of_optional_val(void)
 {
-	uint32_t flags = RTE_ARGPARSE_ARG_OPTIONAL_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
+	uint64_t flags = RTE_ARGPARSE_ARG_OPTIONAL_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
 	struct rte_argparse *obj;
 	int val_saver = 0;
 	char *argv[2];
@@ -645,7 +645,7 @@  test_argparse_opt_callback_parse_int_of_optional_val(void)
 static int
 test_argparse_pos_autosave_parse_int(void)
 {
-	uint32_t flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
+	uint64_t flags = RTE_ARGPARSE_ARG_REQUIRED_VALUE | RTE_ARGPARSE_ARG_VALUE_INT;
 	struct rte_argparse *obj;
 	int val_saver = 0;
 	char *argv[3];
diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 6e890cdc0d..9e799a16c5 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -50,7 +50,7 @@  arg_attr_flag_multi(const struct rte_argparse_arg *arg)
 	return RTE_FIELD_GET64(ARG_ATTR_SUPPORT_MULTI_MASK, arg->flags);
 }
 
-static inline uint32_t
+static inline uint64_t
 arg_attr_unused_bits(const struct rte_argparse_arg *arg)
 {
 #define USED_BIT_MASK	(ARG_ATTR_HAS_VAL_MASK | ARG_ATTR_VAL_TYPE_MASK | \
@@ -172,7 +172,7 @@  static int
 verify_arg_flags(const struct rte_argparse *obj, uint32_t index)
 {
 	const struct rte_argparse_arg *arg = &obj->args[index];
-	uint32_t unused_bits = arg_attr_unused_bits(arg);
+	uint64_t unused_bits = arg_attr_unused_bits(arg);
 
 	if (unused_bits != 0) {
 		ARGPARSE_LOG(ERR, "argument %s flags unused bits should not be set!",