lib/cmdline_kunit: add a new test case for get_options()
Subsystems (3)
PATCH v3 1/6 lib/cmdline_kunit: add a new test case for get_options()
Add a test case for get_options() which is provided by cmdline.c.
---
v3: drop string macros (Bart)
lib/cmdline_kunit.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
lib/cmdline_kunit.c +47 -0
@@ -18,6 +18,26 @@ static const int cmdline_test_values[] = {1 ,3 ,2 ,1 ,1 ,1 ,3 ,1 , }; +static_assert (ARRAY_SIZE (cmdline_test_strings) ==ARRAY_SIZE (cmdline_test_values)); + +static const char *cmdline_test_range_strings[] = { +"-7" ,"--7" ,"-1-2" ,"7--9" , +"7-" ,"-7--9" ,"7-9," ,"9-7" , +"5-a" ,"a-5" ,"5-8" ,",8-5" , +"+,1" ,"-,4" ,"-3,0-1,6" ,"4,-" , +" +2" ," -9" ,"0-1,-3,6" ,"- 9" , +}; + +static const int cmdline_test_range_values[][16 ] = { + {1 ,-7 , }, {0 ,-0 , }, {4 ,-1 ,0 ,+1 ,2 , }, {0 ,7 , }, + {0 ,+7 , }, {0 ,-7 , }, {3 ,+7 ,8 ,+9 ,0 , }, {0 ,9 , }, + {0 ,+5 , }, {0 ,-0 , }, {4 ,+5 ,6 ,+7 ,8 , }, {0 ,0 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,-3 ,0 ,+1 ,6 , }, {1 ,4 , }, + {0 ,+0 , }, {0 ,-0 , }, {4 ,+0 ,1 ,-3 ,6 , }, {0 ,0 , }, +}; + +static_assert (ARRAY_SIZE (cmdline_test_range_strings) ==ARRAY_SIZE (cmdline_test_range_values)); +static void cmdline_do_one_test (struct kunit *test,const char *in,int rc,int offset) {const char *fmt ="Pattern: %s" ;
@@ -84,10 +104,37 @@ static void cmdline_test_tail_int(struct kunit *test) }while (++i <ARRAY_SIZE (cmdline_test_strings)); } +static void cmdline_do_one_range_test (struct kunit *test,const char *in, +unsigned int n,const int *e) +{ +unsigned int i; +int r[16 ]; + +memset (r,0 ,sizeof (r)); +get_options (in,ARRAY_SIZE (r), r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],"in test %u expected %d numbers, got %d" , + n, e[0 ], r[0 ]); +for (i =1 ; i <ARRAY_SIZE (r); i++) +KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],"in test %u at %d" , n, i); +} + +static void cmdline_test_range (struct kunit *test) +{ +unsigned int i =0 ; + +do { +const char *str = cmdline_test_range_strings[i]; +const int *e = cmdline_test_range_values[i]; + +cmdline_do_one_range_test (test, str, i, e); + }while (++i <ARRAY_SIZE (cmdline_test_range_strings)); +} +static struct kunit_case cmdline_test_cases[] = {KUNIT_CASE (cmdline_test_noint),KUNIT_CASE (cmdline_test_lead_int),KUNIT_CASE (cmdline_test_tail_int), +KUNIT_CASE (cmdline_test_range), {} };
Signature · 1 line
2.29.2
PATCH v3 2/6 lib/cmdline: Update documentation to reflect behaviour
get_options() API has some tricks to optimize that may be not so obvious to the caller. Update documentation to reflect current behaviour.
---
v3: added Rb tag (Bart)
lib/cmdline.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/cmdline.c +6 -1
@@ -83,7 +83,7 @@ EXPORT_SYMBOL(get_option);* get_options - Parse a string into a list of integers * @str: String to be parsed * @nints: size of integer array -* @ints: integer array +* @ints: integer array (must have a room for at least one element) * * This function parses a string containing a comma-separated * list of integers, a hyphen-separated range of _positive_ integers,
@@ -91,6 +91,11 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* Returns: +* +* The first element is filled by the amount of the collected numbers +* in the range. The rest is what was parsed from the @str. +* * Return value is the character in the string which caused * the parse to end (typically a null terminator, if @str is * completely parseable).
Signature · 1 line
2.29.2
Re: [PATCH v3 2/6] lib/cmdline: Update documentation to reflect behaviour
Hi Andriy,
Quoted 5 lines · Andy Shevchenko
get_options()API has some tricks to optimize that may be not so obvious to the caller. Update documentation to reflect current behaviour.Signed-off-by Andy Shevchenko <[email protected]>Reviewed-by Bartosz Golaszewski <[email protected]>
Thanks for your patch!
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 86
Quoted 8 lines
lib/cmdline.c +1
-1@@ -83,7 +83,7 @@ EXPORT_SYMBOL(get_option);* get_options - Parse a string into a list of integers * @str: String to be parsed * @nints: size of integer array -* @ints: integer array +* @ints: integer array (must have a room for at least one element)
must have room
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 96
Quoted 10 lines
lib/cmdline.c +3
-0* * This function parses a string containing a comma-separated * list of integers, a hyphen-separated range of _positive_ integers, @@ -91,6 +91,11 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* Returns: +* +* The first element is filled by the amount of the collected numbers
the number of collected integers?
Yes, the lack of articles in RU can be a disadvantage...
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 101
Quoted 5 lines
lib/cmdline.c +2
-0+* in the range. The rest is what was parsed from the @str. +* * Return value is the character in the string which caused * the parse to end (typically a null terminator, if @str is * completely parseable).
With the above fixed:
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v3 2/6] lib/cmdline: Update documentation to reflect behaviour
Quoted 9 lines · Geert Uytterhoeven
Quoted 5 lines · Andy Shevchenko
get_options()API has some tricks to optimize that may be not so obvious to the caller. Update documentation to reflect current behaviour.Signed-off-by Andy Shevchenko <[email protected]>Reviewed-by Bartosz Golaszewski <[email protected]>Thanks for your patch!
Thanks for review.
I'll fix this in my repo w/o sending a v4.
Quoted 24 lines
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 86
Quoted 8 lines
lib/cmdline.c +1
-1@@ -83,7 +83,7 @@ EXPORT_SYMBOL(get_option);* get_options - Parse a string into a list of integers * @str: String to be parsed * @nints: size of integer array -* @ints: integer array +* @ints: integer array (must have a room for at least one element) must have room
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 96
Quoted 10 lines
lib/cmdline.c +3
-0* * This function parses a string containing a comma-separated * list of integers, a hyphen-separated range of _positive_ integers, @@ -91,6 +91,11 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* Returns: +* +* The first element is filled by the amount of the collected numbers the number of collected integers?
Quoted 1 line
Yes, the lack of articles in RU can be a disadvantage...
:-)
Quoted 20 lines
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 101
Quoted 6 lines
lib/cmdline.c +2
-0+* in the range. The rest is what was parsed from the @str. +* * Return value is the character in the string which caused * the parse to end (typically a null terminator, if @str is * completely parseable). With the above fixed:
Reviewed-by Geert Uytterhoeven <[email protected]>Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 3/6 lib/cmdline: Allow get_options() to take 0 to validate the input
In reply to Andy ShevchenkoAllow get_options() to take 0 as a number of integers parameter to validate the input.
---
v3: dropped string macros, added "the" in the comment (Bart)
lib/cmdline.c | 14 +++++++++++--- lib/cmdline_kunit.c | 11 ++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-)
lib/cmdline.c +11 -3
@@ -91,6 +91,9 @@ EXPORT_SYMBOL(get_option);* full, or when no more numbers can be retrieved from the * string. * +* When @nints is 0, the function just validates the given @str and +* returns the amount of parseable integers as described below. +* * Returns: * * The first element is filled by the amount of the collected numbers
@@ -103,15 +106,20 @@ EXPORT_SYMBOL(get_option);char *get_options (const char *str,int nints,int *ints) { +bool validate = (nints ==0 );int res, i =1 ; -while (i < nints) { - res =get_option ((char **)&str, ints + i); +while (i < nints || validate) { +int *pint = validate ? ints : ints + i; + + res =get_option ((char **)&str, pint);if (res ==0 )break ;if (res ==3 ) { +int n = validate ?0 : nints - i;int range_nums; - range_nums =get_range ((char **)&str, ints + i, nints - i); + + range_nums =get_range ((char **)&str, pint, n);if (range_nums <0 )break ;/*
lib/cmdline_kunit.c +10 -1
@@ -109,13 +109,22 @@ static void cmdline_do_one_range_test(struct kunit *test, const char *in, {unsigned int i;int r[16 ]; +int *p;memset (r,0 ,sizeof (r));get_options (in,ARRAY_SIZE (r), r); -KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],"in test %u expected %d numbers, got %d" , +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],"in test %u (parsed) expected %d numbers, got %d" , n, e[0 ], r[0 ]);for (i =1 ; i <ARRAY_SIZE (r); i++)KUNIT_EXPECT_EQ_MSG (test, r[i], e[i],"in test %u at %d" , n, i); + +memset (r,0 ,sizeof (r)); +get_options (in,0 , r); +KUNIT_EXPECT_EQ_MSG (test, r[0 ], e[0 ],"in test %u (validated) expected %d numbers, got %d" , + n, e[0 ], r[0 ]); + + p =memchr_inv (&r[1 ],0 ,sizeof (r) -sizeof (r[0 ])); +KUNIT_EXPECT_PTR_EQ_MSG (test, p, (int *)0 ,"in test %u out of bound at %d" , n, p - r); }static void cmdline_test_range (struct kunit *test)
Signature · 1 line
2.29.2
Re: [PATCH v3 3/6] lib/cmdline: Allow get_options() to take 0 to validate the input
Hi Andy,
Quoted 4 lines · Andy Shevchenko
Allow
get_options()to take 0 as a number of integers parameter to validate the input.Signed-off-by Andy Shevchenko <[email protected]>
Thanks for your patch!
Quoted 4 lines
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 113
Quoted 10 lines
lib/cmdline.c +3
-2@@ -103,15 +106,20 @@ EXPORT_SYMBOL(get_option);char *get_options (const char *str,int nints,int *ints) { +bool validate = (nints ==0 );int res, i =1 ; -while (i < nints) { - res =get_option ((char **)&str, ints + i); +while (i < nints || validate) { +int *pint = validate ? ints : ints + i;
I think you can use NULL for validation, as per the documentation for get_option().
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 125
Quoted 13 lines
lib/cmdline.c +5
-1+ + res =get_option ((char **)&str, pint);if (res ==0 )break ;if (res ==3 ) { +int n = validate ?0 : nints - i;int range_nums; - range_nums =get_range ((char **)&str, ints + i, nints - i); + + range_nums =get_range ((char **)&str, pint, n);if (range_nums <0 )break ;/*
Regardless:
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v3 3/6] lib/cmdline: Allow get_options() to take 0 to validate the input
Quoted 2 lines · Geert Uytterhoeven
On Fri, Jan 22, 2021 at 1:39 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 5 lines
lib/cmdline.c EXPORT_SYMBOL(get_option); · line 113
Quoted 2 lines
lib/cmdline.c +2
-0+while (i < nints || validate) { +int *pint = validate ? ints : ints + i;I think you can use NULL for validation, as per the documentation for
get_option().
That's what takes me a long time to realize how this machinery works and no, unfortunately we may not use NULL, we have to keep the parsed number for the further operations.
Thanks for review!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 4/6 gpio: aggregator: Replace isrange() by using get_options()
In reply to Andy ShevchenkoWe already have a nice helper called get_options() which can be used to validate the input format. Replace isrange() by using it.
---
v3: added Rb tags (Linus, Bart)
drivers/gpio/gpio-aggregator.c | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-)
drivers/gpio/gpio-aggregator.c +3 -30
@@ -62,34 +62,6 @@ static char *get_arg(char **args)return start; } -static bool isrange (const char *s) -{ -size_t n; - -if (IS_ERR_OR_NULL (s)) -return false; - -while (1 ) { - n =strspn (s,"0123456789" ); -if (!n) -return false; - - s += n; - -switch (*s++) { -case '\0' : -return true; - -case '-' : -case ',' : -break ; - -default : -return false; - } - } -} -static int aggr_add_gpio (struct gpio_aggregator *aggr,const char *key,int hwnum,unsigned int *n) {
@@ -112,10 +84,10 @@ static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,static int aggr_parse (struct gpio_aggregator *aggr) { +char *name, *offsets, *p;char *args = aggr->args;unsigned long *bitmap;unsigned int i, n =0 ; -char *name, *offsets;int error =0 ; bitmap =bitmap_alloc (ARCH_NR_GPIOS ,GFP_KERNEL );
@@ -130,7 +102,8 @@ static int aggr_parse(struct gpio_aggregator *aggr) goto free_bitmap; } -if (!isrange (offsets)) { + p =get_options (offsets,0 , &error); +if (error ==0 || *p) {/* Named GPIO line */ error =aggr_add_gpio (aggr, name,U16_MAX , &n);if (error)
Signature · 1 line
2.29.2
Re: [PATCH v3 4/6] gpio: aggregator: Replace isrange() by using get_options()
Hi Andy,
Quoted 6 lines · Andy Shevchenko
We already have a nice helper called
get_options()which can be used to validate the input format. Replaceisrange()by using it.Signed-off-by Andy Shevchenko <[email protected]>Reviewed-by Linus Walleij <[email protected]>Reviewed-by Bartosz Golaszewski <[email protected]>
Let's hope this is never backported to stable before [PATCH 3/6]....
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
PATCH v3 5/6 gpio: aggregator: Use compound literal from the header
In reply to Andy ShevchenkoInstead of doing it in place, convert GPIO_LOOKUP_IDX() and GPIO_HOG() to be compund literals that's allow to use them as rvalue in assignments.
Due to above conversion, use compound literal from the header in the gpio-aggregator.c.
---
v3: new patch
drivers/gpio/gpio-aggregator.c | 3 +-- include/linux/gpio/machine.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpio-aggregator.c +1 -2
@@ -72,8 +72,7 @@ static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,if (!lookups)return -ENOMEM ; - lookups->table[*n] = - (struct gpiod_lookup )GPIO_LOOKUP_IDX (key, hwnum,NULL , *n,0 ); + lookups->table[*n] =GPIO_LOOKUP_IDX (key, hwnum,NULL , *n,0 ); (*n)++;memset (&lookups->table[*n],0 ,sizeof (lookups->table[*n]));
include/linux/gpio/machine.h +2 -2
@@ -75,7 +75,7 @@ struct gpiod_hog {* gpiod_get_index() */ #define GPIO_LOOKUP_IDX (_key, _chip_hwnum, _con_id, _idx, _flags) \ -{ \ +(struct gpiod_lookup) { \ .key = _key, \ .chip_hwnum = _chip_hwnum, \ .con_id = _con_id, \
@@ -87,7 +87,7 @@ struct gpiod_hog {* Simple definition of a single GPIO hog in an array. */ #define GPIO_HOG (_chip_label, _chip_hwnum, _line_name, _lflags, _dflags) \ -{ \ +(struct gpiod_hog) { \ .chip_label = _chip_label, \ .chip_hwnum = _chip_hwnum, \ .line_name = _line_name, \
Signature · 1 line
2.29.2
Re: [PATCH v3 5/6] gpio: aggregator: Use compound literal from the header
Hi Andy,
Thanks for your patch!
Quoted 2 lines · Andy Shevchenko
Instead of doing it in place, convert
GPIO_LOOKUP_IDX()andGPIO_HOG()to be compund literals that's allow to use them as rvalue in assignments.
... to compound literals, which can be used as rvalues ...
Quoted 4 lines
Due to above conversion, use compound literal from the header in the
gpio-aggregator.c.Signed-off-by Andy Shevchenko <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
PATCH v3 6/6 gpio: aggregator: Remove trailing comma in terminator entries
In reply to Andy ShevchenkoRemove trailing comma in terminator entries to avoid potential expanding an array behind it.
---
v3: added tags (Linus, Bart, Geert)
drivers/gpio/gpio-aggregator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpio-aggregator.c +2 -2
@@ -243,7 +243,7 @@ static DRIVER_ATTR_WO(delete_device);static struct attribute *gpio_aggregator_attrs[] = { &driver_attr_new_device.attr, &driver_attr_delete_device.attr, -NULL , +NULL };ATTRIBUTE_GROUPS (gpio_aggregator);
@@ -517,7 +517,7 @@ static const struct of_device_id gpio_aggregator_dt_ids[] = {* Add GPIO-operated devices controlled from userspace below, * or use "driver_override" in sysfs */ - {}, + {} };MODULE_DEVICE_TABLE (of, gpio_aggregator_dt_ids);#endif
Signature · 1 line
2.29.2