Functions simple_strtoul and simple_strtoull have been deprecated
as they cannot return overflows.
See https://lkml.org/lkml/2011/2/26/52

Unfortunately functions simple_strtoul and simple_strtoull cannot
be replaced by kstrtoul and kstrtoull in some places, because they
expect a zero terminated string instead of returning a pointer to
the character after the last digit.

This patch introduces two new functions kstrtoul_e and kstrtoull_e
which fill this gap.

Signed-off-by: Heinrich Schuchardt <[email protected]>
---
 include/linux/kernel.h |  4 +++
 lib/kstrtox.c          | 71 +++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d6d630d..580212e 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -255,6 +255,10 @@ void complete_and_exit(struct completion *, long)
 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long 
*res);
 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
 
+int __must_check kstrtoull_e(const char *s, char **ends, unsigned int base,
+                            unsigned long long *res);
+int __must_check kstrtoul_e(const char *s, char **ends, unsigned int base,
+                           unsigned 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);
 
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index ec8da78..e2b8618 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -83,7 +83,8 @@ unsigned int _parse_integer(const char *s, unsigned int base, 
unsigned long long
        return rv;
 }
 
-static int _kstrtoull(const char *s, unsigned int base, unsigned long long 
*res)
+static int _kstrtoull_e(const char *s, char **ends, unsigned int base,
+                       unsigned long long *res)
 {
        unsigned long long _res;
        unsigned int rv;
@@ -95,15 +96,79 @@ static int _kstrtoull(const char *s, unsigned int base, 
unsigned long long *res)
        if (rv == 0)
                return -EINVAL;
        s += rv;
+
+       if (ends) {
+               *ends = (char *) s;
+               goto out_ok;
+       }
+
        if (*s == '\n')
                s++;
        if (*s)
                return -EINVAL;
+
+out_ok:
        *res = _res;
        return 0;
 }
 
 /**
+ * kstrtoull_e - convert a string to an unsigned long long
+ * @s: The start of the string.
+ * @ends: Returns a pointer to the character after the last digit.
+ * @base: The number base to use. The maximum supported base is 16. If base is
+ *  given as 0, then the base of the string is automatically detected with the
+ *  conventional semantics - If it begins with 0x the number will be parsed as 
a
+ *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
+ *  parsed as an octal number. Otherwise it will be parsed as a decimal.
+ * @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.
+ */
+int kstrtoull_e(const char *s, char **ends, unsigned int base,
+               unsigned long long *res)
+{
+       if (!ends)
+               return -EINVAL;
+
+       return _kstrtoull_e(s, ends, base, res);
+}
+EXPORT_SYMBOL(kstrtoull_e);
+
+/**
+ * kstrtoul_e - convert a string to an unsigned long
+ * @s: The start of the string.
+ * @ends: Returns a pointer to the character after the last digit.
+ * @base: The number base to use. The maximum supported base is 16. If base is
+ *  given as 0, then the base of the string is automatically detected with the
+ *  conventional semantics - If it begins with 0x the number will be parsed as 
a
+ *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
+ *  parsed as an octal number. Otherwise it will be parsed as a decimal.
+ * @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_strtoul. Return code must
+ * be checked.
+ */
+int kstrtoul_e(const char *s, char **ends, unsigned int base,
+              unsigned long *res)
+{
+       unsigned long long value;
+       int rv;
+
+       rv = kstrtoull_e(s, ends, base, &value);
+       if (rv < 0)
+               return rv;
+       if (value != (unsigned long long) (unsigned long) value)
+               return -ERANGE;
+       *res = value;
+       return 0;
+}
+EXPORT_SYMBOL(kstrtoul_e);
+
+/**
  * kstrtoull - convert a string to an unsigned long long
  * @s: The start of the string. The string must be null-terminated, and may 
also
  *  include a single newline before its terminating null. The first character
@@ -123,7 +188,7 @@ int kstrtoull(const char *s, unsigned int base, unsigned 
long long *res)
 {
        if (s[0] == '+')
                s++;
-       return _kstrtoull(s, base, res);
+       return _kstrtoull_e(s, NULL, base, res);
 }
 EXPORT_SYMBOL(kstrtoull);
 
@@ -149,7 +214,7 @@ int kstrtoll(const char *s, unsigned int base, long long 
*res)
        int rv;
 
        if (s[0] == '-') {
-               rv = _kstrtoull(s + 1, base, &tmp);
+               rv = _kstrtoull_e(s + 1, NULL, base, &tmp);
                if (rv < 0)
                        return rv;
                if ((long long)(-tmp) >= 0)
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to