Skip to contentSkip to conversation

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

PATCH Superseded Andy Shevchenko 2 participants 5 messages Jun 26, 2019 – Jul 4, 2019 Auxiliary display drivers

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 on kstrto<foo>() ones.

Update a comment to reduce confusing about special use cases.

---
- Cc to Andrew

 include/linux/kernel.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
include/linux/kernel.h +11 -5
index 74b1ee9027f5..e156b8b41d05 100644
@@ -331,8 +331,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)
 {
@@ -360,8 +359,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)
 {
@@ -437,7 +435,15 @@ 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: The simple_strto<foo> does not check for overflow and,
+ *	 depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if the code will need in place
+ * conversion and otherwise looks very ugly. 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 v2 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.

---
- Cc to Andrew
- fix compilation issue

 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

Hi Andy,

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 on kstrto<foo>() ones.

over

Quoted 1 line

Update a comment to reduce confusing about special use cases.

confusion

Quoted 9 lines
include/linux/kernel.h +0 -0
@@ -437,7 +435,15 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t
        return kstrtoint_from_user(s, count, base, res);
 }

include/linux/kernel.h static inline int __must_check kstrtos32_from_user(const char __user *s, size_t · line 445

Quoted 10 lines
include/linux/kernel.h +8 -1
 
-/* Obsolete, do not use.  Use kstrto<foo> instead */
+/*
+ * Use kstrto<foo> instead.
+ *
+ * NOTE: The simple_strto<foo> does not check for overflow and,
+ *      depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if the code will need in place
+ * conversion and otherwise looks very ugly. Keep in mind above caveat.

What do you mean by "in place conversion"?
The input buffer is const, and not modified by the callee. Do you mean that these functions do not require NUL termination (just after the number), and the characters making up the number don't have to be copied to a separate buffer to make them NUL-terminated?

include/linux/kernel.h static inline int __must_check kstrtos32_from_user(const char __user *s, size_t · line 449

Quoted 4 lines
include/linux/kernel.h +1 -0
+ */
 
 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
 extern long simple_strtol(const char *,char **,unsigned int);

Yeah, they're still very useful.

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
#3 lore raw
Quoted 9 lines · Geert Uytterhoeven

Hi Andy,

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 on kstrto<foo>() ones.

over

Will fix.

Quoted 3 lines
Quoted 1 line

Update a comment to reduce confusing about special use cases.

confusion

Will fix.

Quoted 10 lines
Quoted 10 lines
include/linux/kernel.h +0 -0
@@ -437,7 +435,15 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t
        return kstrtoint_from_user(s, count, base, res);
 }
 
Quoted 15 lines

include/linux/kernel.h static inline int __must_check kstrtos32_from_user(const char __user *s, size_t · line 445

Quoted 9 lines
include/linux/kernel.h +8 -1
-/* Obsolete, do not use.  Use kstrto<foo> instead */
+/*
+ * Use kstrto<foo> instead.
+ *
+ * NOTE: The simple_strto<foo> does not check for overflow and,
+ *      depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if the code will need in place
+ * conversion and otherwise looks very ugly. Keep in mind above caveat.

What do you mean by "in place conversion"?
The input buffer is const, and not modified by the callee. Do you mean that these functions do not require NUL termination (just after the number), and the characters making up the number don't have to be copied to a separate buffer to make them NUL-terminated?

The second one, could you propose better wording for that?

Quoted 6 lines

include/linux/kernel.h static inline int __must_check kstrtos32_from_user(const char __user *s, size_t · line 449

Quoted 4 lines
include/linux/kernel.h +1 -0
+ */
 
 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
 extern long simple_strtol(const char *,char **,unsigned int);

Yeah, they're still very useful.

Thanks for review.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi Andy,

Quoted 26 lines · Andy Shevchenko
Quoted 9 lines · Geert Uytterhoeven
Quoted 7 lines · Andy Shevchenko
include/linux/kernel.h +0 -0
@@ -437,7 +435,15 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t
        return kstrtoint_from_user(s, count, base, res);
 }
 
Quoted 15 lines

include/linux/kernel.h static inline int __must_check kstrtos32_from_user(const char __user *s, size_t · line 445

Quoted 9 lines
include/linux/kernel.h +8 -1
-/* Obsolete, do not use.  Use kstrto<foo> instead */
+/*
+ * Use kstrto<foo> instead.
+ *
+ * NOTE: The simple_strto<foo> does not check for overflow and,
+ *      depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if the code will need in place
+ * conversion and otherwise looks very ugly. Keep in mind above caveat.

What do you mean by "in place conversion"?
The input buffer is const, and not modified by the callee. Do you mean that these functions do not require NUL termination (just after the number), and the characters making up the number don't have to be copied to a separate buffer to make them NUL-terminated?

Quoted 1 line

The second one, could you propose better wording for that?

What about:

"Use these functions if and only if you cannot use kstrto<foo>, because
the number is not immediately followed by a NUL-character in the buffer. Keep in mind above caveat."

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