Skip to contentSkip to conversation

kstrto*: add documentation

PATCH In review Eldad Zack 3 participants 8 messages Jul 12, 2012

Subsystems (2)
lore
Share & tools
lore

Eldad Zack

author
#0 lore raw

PATCH 1/2 kstrto*: add documentation

As J. Bruce Fields <[email protected]> pointed out, kstrto* is currently lacking kerneldoc comments.
This patch adds kerneldoc comments to common variants of kstrto*:
kstrto(u)l, kstrto(u)ll and kstrto(u)int.

Cc J. Bruce Fields <[email protected]>
---
 include/linux/kernel.h |   36 ++++++++++++++++++++++++++++++++++++
 lib/kstrtox.c          |   18 ++++++++++++++++++
 2 files changed, 54 insertions(+)
include/linux/kernel.h +36 -0
index e07f5e0..582df0f 100644
@@ -220,6 +220,16 @@ int __must_check _kstrtol(const char *s, unsigned int base, long *res);
 
 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
+
+/**
+ * kstrtoul - convert a string to an unsigned long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoul.
+ */
 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
 {
 	/*
@@ -233,6 +243,15 @@ static inline int __must_check kstrtoul(const char *s, unsigned int base, unsign
 		return _kstrtoul(s, base, res);
 }
 
+/**
+ * kstrtol - convert a string to a long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtol.
+ */
 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
 {
 	/*
@@ -246,7 +265,24 @@ static inline int __must_check kstrtol(const char *s, unsigned int base, long *r
 		return _kstrtol(s, base, res);
 }
 
+/**
+ * kstrtouint - convert a string to an unsigned int
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ */
 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
+
+/**
+ * kstrtoint - convert a string to an int
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ */
 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
 
 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
lib/kstrtox.c +18 -0
index c3615ea..7f5bca2 100644
@@ -104,6 +104,15 @@ static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 	return 0;
 }
 
+/**
+ * kstrtoull - convert a string to an unsigned long long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoull.
+ */
 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 {
 	if (s[0] == '+')
@@ -112,6 +121,15 @@ int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 }
 EXPORT_SYMBOL(kstrtoull);
 
+/**
+ * kstrtoll - convert a string to a long long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoll.
+ */
 int kstrtoll(const char *s, unsigned int base, long long *res)
 {
 	unsigned long long tmp;
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 2/2 simple_strto*: annotate function as obsolete

This patch update the documentation to simple_strto* to reflect that it has been obsoleted and advise the usage of kstrto*.

Cc J. Bruce Fields <[email protected]>
---
 lib/vsprintf.c |    8 ++++++++
 1 file changed, 8 insertions(+)
lib/vsprintf.c +8 -0
index c3f36d41..1980cf3 100644
@@ -38,6 +38,8 @@
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
+ *
+ * This function is obsolete. Please use kstrtoull instead.
  */
 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
 {
@@ -61,6 +63,8 @@ EXPORT_SYMBOL(simple_strtoull);
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
+ *
+ * This function is obsolete. Please use kstrtoul instead.
  */
 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
 {
@@ -73,6 +77,8 @@ EXPORT_SYMBOL(simple_strtoul);
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
+ *
+ * This function is obsolete. Please use kstrtol instead.
  */
 long simple_strtol(const char *cp, char **endp, unsigned int base)
 {
@@ -88,6 +94,8 @@ EXPORT_SYMBOL(simple_strtol);
  * @cp: The start of the string
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
+ *
+ * This function is obsolete. Please use kstrtoll instead.
  */
 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
 {
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to Eldad Zack

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

Quoted 12 lines · Eldad Zack
include/linux/kernel.h +3 -0
index e07f5e0..582df0f 100644
@@ -220,6 +220,16 @@ int __must_check _kstrtol(const char *s, unsigned int base, long *res);
 
 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
+
+/**
+ * kstrtoul - convert a string to an unsigned long

Aren't function names supposed to have () after them in kernel doc?

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#3 lore raw
Quoted 7 lines · Stephen Boyd
Quoted 6 lines · Eldad Zack
include/linux/kernel.h +0 -0
index e07f5e0..582df0f 100644
@@ -220,6 +220,16 @@ int __must_check _kstrtol(const char *s, unsigned int base, long *res);
 
Quoted 7 lines

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

Quoted 5 lines
include/linux/kernel.h +3 -0
 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
+
+/**
+ * kstrtoul - convert a string to an unsigned long

Aren't function names supposed to have () after them in kernel doc?

Argh, ignore me. Apparently it's optional.

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#4 lore raw
In reply to Eldad Zack
Quoted 17 lines · Eldad Zack

As J. Bruce Fields <[email protected]> pointed out, kstrto* is currently lacking kerneldoc comments.
This patch adds kerneldoc comments to common variants of kstrto*:
kstrto(u)l, kstrto(u)ll and kstrto(u)int.

Cc J. Bruce Fields <[email protected]>
---
 include/linux/kernel.h |   36 ++++++++++++++++++++++++++++++++++++
 lib/kstrtox.c          |   18 ++++++++++++++++++
 2 files changed, 54 insertions(+)
include/linux/kernel.h +0 -0
index e07f5e0..582df0f 100644

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

Quoted 6 lines
include/linux/kernel.h +3 -0
@@ -220,6 +220,16 @@ int __must_check _kstrtol(const char *s, unsigned int base, long *res);
 
 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
+
+/**
+ * kstrtoul - convert a string to an unsigned long

Also, is it worth mentioning that the number is required to be followed by a string or newline?

--b.

Quoted 90 lines
include/linux/kernel.h +33 -0
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoul.
+ */
 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
 {
 	/*
@@ -233,6 +243,15 @@ static inline int __must_check kstrtoul(const char *s, unsigned int base, unsign
 		return _kstrtoul(s, base, res);
 }
 
+/**
+ * kstrtol - convert a string to a long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtol.
+ */
 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
 {
 	/*
@@ -246,7 +265,24 @@ static inline int __must_check kstrtol(const char *s, unsigned int base, long *r
 		return _kstrtol(s, base, res);
 }
 
+/**
+ * kstrtouint - convert a string to an unsigned int
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ */
 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
+
+/**
+ * kstrtoint - convert a string to an int
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ */
 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
 
 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
lib/kstrtox.c +18 -0
index c3615ea..7f5bca2 100644
@@ -104,6 +104,15 @@ static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 	return 0;
 }
 
+/**
+ * kstrtoull - convert a string to an unsigned long long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoull.
+ */
 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 {
 	if (s[0] == '+')
@@ -112,6 +121,15 @@ int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
 }
 EXPORT_SYMBOL(kstrtoull);
 
+/**
+ * kstrtoll - convert a string to a long long
+ * @s: The start of the string
+ * @base: The number base to use
+ * @res: Where to write the result of the conversion if successful
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoll.
+ */
 int kstrtoll(const char *s, unsigned int base, long long *res)
 {
 	unsigned long long tmp;
Signature · 1 line
1.7.10.4
author
#5 lore raw
Quoted 6 lines · J. Bruce Fields
Quoted 2 lines · Eldad Zack

+/**
+ * kstrtoul - convert a string to an unsigned long

Also, is it worth mentioning that the number is required to be followed by a string or newline?

I am not sure if I understand _parse_integer correctly (which is called to do the actual parsing and has a very nice comment to it) - but it expects a null-terminated string, but will also stop as soon as it bumps into any other non-number character without error (please correct me I'm wrong).
In that case maybe "This function stops parsing as soon as it gets to a character which doesn't belong to the given base, including newline or null.".

And now that I read it more closely, how about:
"If base is given as 0, then the base of the string is automatically detected with the conventional semantics: If the string begins with 0x the number will be parsed as a hexadecimel (case insensitive). If it otherwise begins with 0, it will be parsed as an octal number. Otherwise it will be parsed as a decimal."

Eldad

#6 lore raw
Quoted 6 lines · Eldad Zack
Quoted 4 lines · J. Bruce Fields
Quoted 2 lines · Eldad Zack

+/**
+ * kstrtoul - convert a string to an unsigned long

Quoted 8 lines
Quoted 2 lines

Also, is it worth mentioning that the number is required to be followed by a string or newline?

I am not sure if I understand _parse_integer correctly (which is called to do the actual parsing and has a very nice comment to it) - but it expects a null-terminated string, but will also stop as soon as it bumps into any other non-number character without error (please correct me I'm wrong).

I believe it, but, in _kstrtoull:

	rv = _parse_integer(s, base, &_res);
        if (rv & KSTRTOX_OVERFLOW)
                return -ERANGE;
        rv &= ~KSTRTOX_OVERFLOW;
        if (rv == 0)
                return -EINVAL;
        s += rv;
        if (*s == '\n')
                s++;
        if (*s)
                return -EINVAL;

So actually it appears the string must be all numeric except possibly a final newline.

--b.

author
#7 lore raw
Quoted 23 lines · J. Bruce Fields
Quoted 7 lines · Eldad Zack

On Thu, 12 Jul 2012, J. Bruce Fields wrote:
I am not sure if I understand _parse_integer correctly (which is called to do the actual parsing and has a very nice comment to it) - but it expects a null-terminated string, but will also stop as soon as it bumps into any other non-number character without error (please correct me I'm wrong).

I believe it, but, in _kstrtoull:

	rv = _parse_integer(s, base, &_res);
        if (rv & KSTRTOX_OVERFLOW)
                return -ERANGE;
        rv &= ~KSTRTOX_OVERFLOW;
        if (rv == 0)
                return -EINVAL;
        s += rv;
        if (*s == '\n')
                s++;
        if (*s)
                return -EINVAL;
Quoted 2 lines

So actually it appears the string must be all numeric except possibly a final newline.

Ah. You're right of course. Thanks! I also noticed I missed the SGML templates, so I'll resend this at some point later.

Eldad