* [PATCH] lib/string: update match_string() doc-strings with correct behavior
@ 2020-02-12 14:47 Alexandru Ardelean
2020-02-12 16:46 ` Andy Shevchenko
2020-02-13 7:27 ` [PATCH v2] " Alexandru Ardelean
0 siblings, 2 replies; 5+ messages in thread
From: Alexandru Ardelean @ 2020-02-12 14:47 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, keescook, andriy.shevchenko, tobin, gregkh, Alexandru Ardelean
There were a few attempts at changing behavior of the match_string()
helpers (i.e. 'match_string()' & 'sysfs_match_string()'), to change &
extend the behavior according to the doc-string.
But the simplest approach is to just fix the doc-strings. The current
behavior is fine as-is, and some bugs were introduced trying to fix it.
As for extending the behavior, new helpers can always be introduced if
needed.
The match_string() helpers behave more like 'strncmp()' in the sense that
they go up to n elements or until the first NULL element in the array of
strings.
This change updates the doc-strings with this info.
Some references to the previous attempts (in no particular order):
https://lore.kernel.org/lkml/20190508111913.7276-1-alexandru.ardelean@analog.com/
https://lore.kernel.org/lkml/20190625130104.29904-1-alexandru.ardelean@analog.com/
https://lore.kernel.org/lkml/20190422083257.21805-1-alexandru.ardelean@analog.com/
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
lib/string.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/lib/string.c b/lib/string.c
index f607b967d978..061a17b062b1 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -696,9 +696,12 @@ EXPORT_SYMBOL(sysfs_streq);
/**
* match_string - matches given string in an array
* @array: array of strings
- * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @n: number of strings in the array to compare
* @string: string to match with
*
+ * This routine will look for a string in an array of strings up to the
+ * n-th element in the array or until the first NULL element.
+ *
* Return:
* index of a @string in the @array if matches, or %-EINVAL otherwise.
*/
@@ -722,11 +725,14 @@ EXPORT_SYMBOL(match_string);
/**
* __sysfs_match_string - matches given string in an array
* @array: array of strings
- * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @n: number of strings in the array to compare
* @str: string to match with
*
* Returns index of @str in the @array or -EINVAL, just like match_string().
* Uses sysfs_streq instead of strcmp for matching.
+ *
+ * This routine will look for a string in an array of strings up to the
+ * n-th element in the array or until the first NULL element.
*/
int __sysfs_match_string(const char * const *array, size_t n, const char *str)
{
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] lib/string: update match_string() doc-strings with correct behavior
2020-02-12 14:47 [PATCH] lib/string: update match_string() doc-strings with correct behavior Alexandru Ardelean
@ 2020-02-12 16:46 ` Andy Shevchenko
2020-02-13 7:09 ` Ardelean, Alexandru
2020-02-13 7:27 ` [PATCH v2] " Alexandru Ardelean
1 sibling, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2020-02-12 16:46 UTC (permalink / raw)
To: Alexandru Ardelean; +Cc: linux-kernel, akpm, keescook, tobin, gregkh
On Wed, Feb 12, 2020 at 04:47:23PM +0200, Alexandru Ardelean wrote:
> There were a few attempts at changing behavior of the match_string()
> helpers (i.e. 'match_string()' & 'sysfs_match_string()'), to change &
> extend the behavior according to the doc-string.
>
> But the simplest approach is to just fix the doc-strings. The current
> behavior is fine as-is, and some bugs were introduced trying to fix it.
>
> As for extending the behavior, new helpers can always be introduced if
> needed.
>
> The match_string() helpers behave more like 'strncmp()' in the sense that
> they go up to n elements or until the first NULL element in the array of
> strings.
>
> This change updates the doc-strings with this info.
>
> Some references to the previous attempts (in no particular order):
> https://lore.kernel.org/lkml/20190508111913.7276-1-alexandru.ardelean@analog.com/
> https://lore.kernel.org/lkml/20190625130104.29904-1-alexandru.ardelean@analog.com/
> https://lore.kernel.org/lkml/20190422083257.21805-1-alexandru.ardelean@analog.com/
...
> /**
> * match_string - matches given string in an array
> * @array: array of strings
> - * @n: number of strings in the array or -1 for NULL terminated arrays
> + * @n: number of strings in the array to compare
But this change won't be helpful, it actually hides the part of behaviour that
is being used.
> * @string: string to match with
> *
> + * This routine will look for a string in an array of strings up to the
> + * n-th element in the array or until the first NULL element.
> + *
Perhaps this needs to be rephrased. Because now it has completely hidden the -1 case.
> * Return:
> * index of a @string in the @array if matches, or %-EINVAL otherwise.
> */
Ditto for the second part.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] lib/string: update match_string() doc-strings with correct behavior
2020-02-12 16:46 ` Andy Shevchenko
@ 2020-02-13 7:09 ` Ardelean, Alexandru
0 siblings, 0 replies; 5+ messages in thread
From: Ardelean, Alexandru @ 2020-02-13 7:09 UTC (permalink / raw)
To: andriy.shevchenko; +Cc: keescook, gregkh, linux-kernel, akpm, tobin
On Wed, 2020-02-12 at 18:46 +0200, Andy Shevchenko wrote:
> [External]
>
> On Wed, Feb 12, 2020 at 04:47:23PM +0200, Alexandru Ardelean wrote:
> > There were a few attempts at changing behavior of the match_string()
> > helpers (i.e. 'match_string()' & 'sysfs_match_string()'), to change &
> > extend the behavior according to the doc-string.
> >
> > But the simplest approach is to just fix the doc-strings. The current
> > behavior is fine as-is, and some bugs were introduced trying to fix it.
> >
> > As for extending the behavior, new helpers can always be introduced if
> > needed.
> >
> > The match_string() helpers behave more like 'strncmp()' in the sense that
> > they go up to n elements or until the first NULL element in the array of
> > strings.
> >
> > This change updates the doc-strings with this info.
> >
> > Some references to the previous attempts (in no particular order):
> >
> > https://lore.kernel.org/lkml/20190508111913.7276-1-alexandru.ardelean@analog.com/
> >
> > https://lore.kernel.org/lkml/20190625130104.29904-1-alexandru.ardelean@analog.com/
> >
> > https://lore.kernel.org/lkml/20190422083257.21805-1-alexandru.ardelean@analog.com/
>
> ...
>
> > /**
> > * match_string - matches given string in an array
> > * @array: array of strings
> > - * @n: number of strings in the array or -1 for NULL terminated
> > arrays
> > + * @n: number of strings in the array to compare
>
> But this change won't be helpful, it actually hides the part of behaviour that
> is being used.
>
> > * @string: string to match with
> > *
> > + * This routine will look for a string in an array of strings up to the
> > + * n-th element in the array or until the first NULL element.
> > + *
>
> Perhaps this needs to be rephrased. Because now it has completely hidden the
> -1 case.
Hmm, it does make sense to specify the -1 behavior purely for historical
purposes.
Otheriwse, I don't feel it's that important to mention it, since you could
technically specify UINT_MAX [or similar] and get the same behavior.
>
> > * Return:
> > * index of a @string in the @array if matches, or %-EINVAL otherwise.
> > */
>
> Ditto for the second part.
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] lib/string: update match_string() doc-strings with correct behavior
2020-02-12 14:47 [PATCH] lib/string: update match_string() doc-strings with correct behavior Alexandru Ardelean
2020-02-12 16:46 ` Andy Shevchenko
@ 2020-02-13 7:27 ` Alexandru Ardelean
2020-02-13 9:36 ` Andy Shevchenko
1 sibling, 1 reply; 5+ messages in thread
From: Alexandru Ardelean @ 2020-02-13 7:27 UTC (permalink / raw)
To: linux-kernel
Cc: akpm, keescook, andriy.shevchenko, tobin, gregkh, Alexandru Ardelean
There were a few attempts at changing behavior of the match_string()
helpers (i.e. 'match_string()' & 'sysfs_match_string()'), to change &
extend the behavior according to the doc-string.
But the simplest approach is to just fix the doc-strings. The current
behavior is fine as-is, and some bugs were introduced trying to fix it.
As for extending the behavior, new helpers can always be introduced if
needed.
The match_string() helpers behave more like 'strncmp()' in the sense that
they go up to n elements or until the first NULL element in the array of
strings.
This change updates the doc-strings with this info.
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
lib/string.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/lib/string.c b/lib/string.c
index f607b967d978..6012c385fb31 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -699,6 +699,14 @@ EXPORT_SYMBOL(sysfs_streq);
* @n: number of strings in the array or -1 for NULL terminated arrays
* @string: string to match with
*
+ * This routine will look for a string in an array of strings up to the
+ * n-th element in the array or until the first NULL element.
+ *
+ * Historically the value of -1 for @n, was used to search in arrays that
+ * are NULL terminated. However, the function does not make a distinction
+ * when finishing the search: either @n elements have been compared OR
+ * the first NULL element was found.
+ *
* Return:
* index of a @string in the @array if matches, or %-EINVAL otherwise.
*/
@@ -727,6 +735,14 @@ EXPORT_SYMBOL(match_string);
*
* Returns index of @str in the @array or -EINVAL, just like match_string().
* Uses sysfs_streq instead of strcmp for matching.
+ *
+ * This routine will look for a string in an array of strings up to the
+ * n-th element in the array or until the first NULL element.
+ *
+ * Historically the value of -1 for @n, was used to search in arrays that
+ * are NULL terminated. However, the function does not make a distinction
+ * when finishing the search: either @n elements have been compared OR
+ * the first NULL element was found.
*/
int __sysfs_match_string(const char * const *array, size_t n, const char *str)
{
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] lib/string: update match_string() doc-strings with correct behavior
2020-02-13 7:27 ` [PATCH v2] " Alexandru Ardelean
@ 2020-02-13 9:36 ` Andy Shevchenko
0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2020-02-13 9:36 UTC (permalink / raw)
To: Alexandru Ardelean; +Cc: linux-kernel, akpm, keescook, tobin, gregkh
On Thu, Feb 13, 2020 at 09:27:22AM +0200, Alexandru Ardelean wrote:
> There were a few attempts at changing behavior of the match_string()
> helpers (i.e. 'match_string()' & 'sysfs_match_string()'), to change &
> extend the behavior according to the doc-string.
>
> But the simplest approach is to just fix the doc-strings. The current
> behavior is fine as-is, and some bugs were introduced trying to fix it.
>
> As for extending the behavior, new helpers can always be introduced if
> needed.
>
> The match_string() helpers behave more like 'strncmp()' in the sense that
> they go up to n elements or until the first NULL element in the array of
> strings.
>
> This change updates the doc-strings with this info.
Thanks, looks good to me now.
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
> ---
> lib/string.c | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/lib/string.c b/lib/string.c
> index f607b967d978..6012c385fb31 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -699,6 +699,14 @@ EXPORT_SYMBOL(sysfs_streq);
> * @n: number of strings in the array or -1 for NULL terminated arrays
> * @string: string to match with
> *
> + * This routine will look for a string in an array of strings up to the
> + * n-th element in the array or until the first NULL element.
> + *
> + * Historically the value of -1 for @n, was used to search in arrays that
> + * are NULL terminated. However, the function does not make a distinction
> + * when finishing the search: either @n elements have been compared OR
> + * the first NULL element was found.
> + *
> * Return:
> * index of a @string in the @array if matches, or %-EINVAL otherwise.
> */
> @@ -727,6 +735,14 @@ EXPORT_SYMBOL(match_string);
> *
> * Returns index of @str in the @array or -EINVAL, just like match_string().
> * Uses sysfs_streq instead of strcmp for matching.
> + *
> + * This routine will look for a string in an array of strings up to the
> + * n-th element in the array or until the first NULL element.
> + *
> + * Historically the value of -1 for @n, was used to search in arrays that
> + * are NULL terminated. However, the function does not make a distinction
> + * when finishing the search: either @n elements have been compared OR
> + * the first NULL element was found.
> */
> int __sysfs_match_string(const char * const *array, size_t n, const char *str)
> {
> --
> 2.20.1
>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-02-13 9:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-12 14:47 [PATCH] lib/string: update match_string() doc-strings with correct behavior Alexandru Ardelean
2020-02-12 16:46 ` Andy Shevchenko
2020-02-13 7:09 ` Ardelean, Alexandru
2020-02-13 7:27 ` [PATCH v2] " Alexandru Ardelean
2020-02-13 9:36 ` Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).