Skip to contentSkip to conversation

kernel.h: Update comment about simple_strto<foo>() functions

PATCH In review Andy Shevchenko 4 participants 7 messages Aug 1, 2019 – Nov 8, 2019 Auxiliary display drivers

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 kernel.h: Update comment about simple_strto<foo>() functions

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.

---
v4: modify comment to be more precise (Petr)

 include/linux/kernel.h | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
include/linux/kernel.h +14 -5
index 4fa360a13c1e..60a9529ee740 100644
@@ -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);
Signature · 1 line
2.20.1
#1 lore raw

PATCH v4 2/2 auxdisplay: charlcd: Deduplicate simple_strtoul()

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.

---
v4: append Petr's Rb tag

 drivers/auxdisplay/charlcd.c | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)
drivers/auxdisplay/charlcd.c +7 -27
index 92745efefb54..3858dc7a4154 100644
@@ -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;
 		}
Signature · 1 line
2.20.1
#2 lore raw
In reply to Andy Shevchenko
Quoted 8 lines · 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.

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

#3 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · 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.

Quoted 7 lines
Quoted 3 lines

Update a comment to reduce confusion about special use cases.

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Miguel Ojeda
Quoted 7 lines · 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.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Andy Shevchenko
Quoted 3 lines · 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.

[]

include/linux/kernel.h

Quoted 1 line
include/linux/kernel.h +0 -0

[]

include/linux/kernel.h int __must_check kstrtoll(const char *s, unsigned int base, long long *res); · line 337

Quoted 7 lines
include/linux/kernel.h +1 -2
@@ -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

Quoted 1 line
#6 lore raw
Quoted 7 lines · Joe Perches
Quoted 4 lines · Andy Shevchenko

* 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.

Quoted 6 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko