linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
@ 2019-08-01 19:29 Andy Shevchenko
  2019-08-01 19:29 ` [PATCH v4 2/2] auxdisplay: charlcd: Deduplicate simple_strtoul() Andy Shevchenko
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Andy Shevchenko @ 2019-08-01 19:29 UTC (permalink / raw)
  To: Miguel Ojeda Sandonis, linux-kernel, Geert Uytterhoeven,
	Mans Rullgard, Andrew Morton, Petr Mladek
  Cc: Andy Shevchenko

There were discussions in the past about use cases for
simple_strto<foo>() functions and, in some rare cases,
they have a benefit over kstrto<foo>() ones.

Update a comment to reduce confusion about special use cases.

Suggested-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v4: modify comment to be more precise (Petr)
 include/linux/kernel.h | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4fa360a13c1e..60a9529ee740 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -334,8 +334,7 @@ int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
  * @res: Where to write the result of the conversion on success.
  *
  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
- * Used as a replacement for the obsolete simple_strtoull. Return code must
- * be checked.
+ * Used as a replacement for the simple_strtoull. Return code must be checked.
 */
 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
 {
@@ -363,8 +362,7 @@ static inline int __must_check kstrtoul(const char *s, unsigned int base, unsign
  * @res: Where to write the result of the conversion on success.
  *
  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
- * Used as a replacement for the obsolete simple_strtoull. Return code must
- * be checked.
+ * Used as a replacement for the simple_strtoull. Return code must be checked.
  */
 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
 {
@@ -440,7 +438,18 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t
 	return kstrtoint_from_user(s, count, base, res);
 }
 
-/* Obsolete, do not use.  Use kstrto<foo> instead */
+/*
+ * Use kstrto<foo> instead.
+ *
+ * NOTE: simple_strto<foo> does not check for the range overflow and,
+ *	 depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if you cannot use kstrto<foo>, because
+ * the conversion ends on the first non-digit character, which may be far
+ * beyond the supported range. It might be useful to parse the strings like
+ * 10x50 or 12:21 without altering original string or temporary buffer in use.
+ * Keep in mind above caveat.
+ */
 
 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
 extern long simple_strtol(const char *,char **,unsigned int);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/2] auxdisplay: charlcd: Deduplicate simple_strtoul()
  2019-08-01 19:29 [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Andy Shevchenko
@ 2019-08-01 19:29 ` Andy Shevchenko
  2019-08-01 19:50 ` [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Miguel Ojeda
  2019-08-01 19:58 ` Joe Perches
  2 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2019-08-01 19:29 UTC (permalink / raw)
  To: Miguel Ojeda Sandonis, linux-kernel, Geert Uytterhoeven,
	Mans Rullgard, Andrew Morton, Petr Mladek
  Cc: Andy Shevchenko

Like in the commit
  8b2303de399f ("serial: core: Fix handling of options after MMIO address")
we may use simple_strtoul() which in comparison to kstrtoul() can do conversion
in-place without additional and unnecessary code to be written.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
v4: append Petr's Rb tag
 drivers/auxdisplay/charlcd.c | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)

diff --git a/drivers/auxdisplay/charlcd.c b/drivers/auxdisplay/charlcd.c
index 92745efefb54..3858dc7a4154 100644
--- a/drivers/auxdisplay/charlcd.c
+++ b/drivers/auxdisplay/charlcd.c
@@ -287,31 +287,6 @@ static int charlcd_init_display(struct charlcd *lcd)
 	return 0;
 }
 
-/*
- * Parses an unsigned integer from a string, until a non-digit character
- * is found. The empty string is not accepted. No overflow checks are done.
- *
- * Returns whether the parsing was successful. Only in that case
- * the output parameters are written to.
- *
- * TODO: If the kernel adds an inplace version of kstrtoul(), this function
- * could be easily replaced by that.
- */
-static bool parse_n(const char *s, unsigned long *res, const char **next_s)
-{
-	if (!isdigit(*s))
-		return false;
-
-	*res = 0;
-	while (isdigit(*s)) {
-		*res = *res * 10 + (*s - '0');
-		++s;
-	}
-
-	*next_s = s;
-	return true;
-}
-
 /*
  * Parses a movement command of the form "(.*);", where the group can be
  * any number of subcommands of the form "(x|y)[0-9]+".
@@ -336,6 +311,7 @@ static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
 {
 	unsigned long new_x = *x;
 	unsigned long new_y = *y;
+	char *p;
 
 	for (;;) {
 		if (!*s)
@@ -345,11 +321,15 @@ static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
 			break;
 
 		if (*s == 'x') {
-			if (!parse_n(s + 1, &new_x, &s))
+			new_x = simple_strtoul(s + 1, &p, 10);
+			if (p == s + 1)
 				return false;
+			s = p;
 		} else if (*s == 'y') {
-			if (!parse_n(s + 1, &new_y, &s))
+			new_y = simple_strtoul(s + 1, &p, 10);
+			if (p == s + 1)
 				return false;
+			s = p;
 		} else {
 			return false;
 		}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
  2019-08-01 19:29 [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Andy Shevchenko
  2019-08-01 19:29 ` [PATCH v4 2/2] auxdisplay: charlcd: Deduplicate simple_strtoul() Andy Shevchenko
@ 2019-08-01 19:50 ` Miguel Ojeda
  2019-08-26 13:43   ` Andy Shevchenko
  2019-08-01 19:58 ` Joe Perches
  2 siblings, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2019-08-01 19:50 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: linux-kernel, Geert Uytterhoeven, Mans Rullgard, Andrew Morton,
	Petr Mladek

On Thu, Aug 1, 2019 at 9:29 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> There were discussions in the past about use cases for
> simple_strto<foo>() functions and, in some rare cases,
> they have a benefit over kstrto<foo>() ones.
>
> Update a comment to reduce confusion about special use cases.
>
> Suggested-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>

I don't recall suggesting this, but I have a bad memory :-)

Andrew, should I pick both patches myself or do you want to pick this one?

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
  2019-08-01 19:29 [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Andy Shevchenko
  2019-08-01 19:29 ` [PATCH v4 2/2] auxdisplay: charlcd: Deduplicate simple_strtoul() Andy Shevchenko
  2019-08-01 19:50 ` [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Miguel Ojeda
@ 2019-08-01 19:58 ` Joe Perches
  2019-08-02  7:55   ` Andy Shevchenko
  2 siblings, 1 reply; 7+ messages in thread
From: Joe Perches @ 2019-08-01 19:58 UTC (permalink / raw)
  To: Andy Shevchenko, Miguel Ojeda Sandonis, linux-kernel,
	Geert Uytterhoeven, Mans Rullgard, Andrew Morton, Petr Mladek

On Thu, 2019-08-01 at 22:29 +0300, Andy Shevchenko wrote:
> There were discussions in the past about use cases for
> simple_strto<foo>() functions and, in some rare cases,
> they have a benefit over kstrto<foo>() ones.
[]
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
[]
> @@ -334,8 +334,7 @@ int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
>   * @res: Where to write the result of the conversion on success.
>   *
>   * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
> - * Used as a replacement for the obsolete simple_strtoull. Return code must
> - * be checked.
> + * Used as a replacement for the simple_strtoull. Return code must be checked.

Using 'the' here is unnecessary and somewhat confusing.

Perhaps:

 * Preferred over simple_strtoull() unless the end position is required
 * Return value must be checked

> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
  2019-08-01 19:58 ` Joe Perches
@ 2019-08-02  7:55   ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2019-08-02  7:55 UTC (permalink / raw)
  To: Joe Perches
  Cc: Andy Shevchenko, Miguel Ojeda Sandonis,
	Linux Kernel Mailing List, Geert Uytterhoeven, Mans Rullgard,
	Andrew Morton, Petr Mladek

On Fri, Aug 2, 2019 at 4:47 AM Joe Perches <joe@perches.com> wrote:
> On Thu, 2019-08-01 at 22:29 +0300, Andy Shevchenko wrote:

> >   * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
> > - * Used as a replacement for the obsolete simple_strtoull. Return code must
> > - * be checked.
> > + * Used as a replacement for the simple_strtoull. Return code must be checked.
>
> Using 'the' here is unnecessary and somewhat confusing.
>
> Perhaps:
>
>  * Preferred over simple_strtoull() unless the end position is required
>  * Return value must be checked

IIRC it simple returns back to the original comment.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
  2019-08-01 19:50 ` [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Miguel Ojeda
@ 2019-08-26 13:43   ` Andy Shevchenko
  2019-11-08 12:59     ` Andy Shevchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2019-08-26 13:43 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kernel, Geert Uytterhoeven, Mans Rullgard, Andrew Morton,
	Petr Mladek

On Thu, Aug 01, 2019 at 09:50:54PM +0200, Miguel Ojeda wrote:
> On Thu, Aug 1, 2019 at 9:29 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > There were discussions in the past about use cases for
> > simple_strto<foo>() functions and, in some rare cases,
> > they have a benefit over kstrto<foo>() ones.
> >
> > Update a comment to reduce confusion about special use cases.
> >
> > Suggested-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> 
> I don't recall suggesting this, but I have a bad memory :-)
> 
> Andrew, should I pick both patches myself or do you want to pick this one?

It seems you can take both.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions
  2019-08-26 13:43   ` Andy Shevchenko
@ 2019-11-08 12:59     ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2019-11-08 12:59 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: linux-kernel, Geert Uytterhoeven, Mans Rullgard, Andrew Morton,
	Petr Mladek

On Mon, Aug 26, 2019 at 04:43:56PM +0300, Andy Shevchenko wrote:
> On Thu, Aug 01, 2019 at 09:50:54PM +0200, Miguel Ojeda wrote:
> > On Thu, Aug 1, 2019 at 9:29 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:
> > >
> > > There were discussions in the past about use cases for
> > > simple_strto<foo>() functions and, in some rare cases,
> > > they have a benefit over kstrto<foo>() ones.
> > >
> > > Update a comment to reduce confusion about special use cases.
> > >
> > > Suggested-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> > 
> > I don't recall suggesting this, but I have a bad memory :-)
> > 
> > Andrew, should I pick both patches myself or do you want to pick this one?
> 
> It seems you can take both.

Can we proceed with this?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-11-08 12:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-01 19:29 [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Andy Shevchenko
2019-08-01 19:29 ` [PATCH v4 2/2] auxdisplay: charlcd: Deduplicate simple_strtoul() Andy Shevchenko
2019-08-01 19:50 ` [PATCH v4 1/2] kernel.h: Update comment about simple_strto<foo>() functions Miguel Ojeda
2019-08-26 13:43   ` Andy Shevchenko
2019-11-08 12:59     ` Andy Shevchenko
2019-08-01 19:58 ` Joe Perches
2019-08-02  7:55   ` 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).