Merge branch 'for-5.14-vsprintf-scanf' into for-linus
authorPetr Mladek <pmladek@suse.com>
Tue, 29 Jun 2021 07:51:58 +0000 (09:51 +0200)
committerPetr Mladek <pmladek@suse.com>
Tue, 29 Jun 2021 07:51:58 +0000 (09:51 +0200)
1  2 
lib/vsprintf.c

diff --combined lib/vsprintf.c
  #include <linux/string_helpers.h>
  #include "kstrtox.h"
  
+ static unsigned long long simple_strntoull(const char *startp, size_t max_chars,
+                                          char **endp, unsigned int base)
+ {
+       const char *cp;
+       unsigned long long result = 0ULL;
+       size_t prefix_chars;
+       unsigned int rv;
+       cp = _parse_integer_fixup_radix(startp, &base);
+       prefix_chars = cp - startp;
+       if (prefix_chars < max_chars) {
+               rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
+               /* FIXME */
+               cp += (rv & ~KSTRTOX_OVERFLOW);
+       } else {
+               /* Field too short for prefix + digit, skip over without converting */
+               cp = startp + max_chars;
+       }
+       if (endp)
+               *endp = (char *)cp;
+       return result;
+ }
  /**
   * simple_strtoull - convert a string to an unsigned long long
   * @cp: The start of the string
   */
  unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  {
-       unsigned long long result;
-       unsigned int rv;
-       cp = _parse_integer_fixup_radix(cp, &base);
-       rv = _parse_integer(cp, base, &result);
-       /* FIXME */
-       cp += (rv & ~KSTRTOX_OVERFLOW);
-       if (endp)
-               *endp = (char *)cp;
-       return result;
+       return simple_strntoull(cp, INT_MAX, endp, base);
  }
  EXPORT_SYMBOL(simple_strtoull);
  
@@@ -109,6 -123,21 +123,21 @@@ long simple_strtol(const char *cp, cha
  }
  EXPORT_SYMBOL(simple_strtol);
  
+ static long long simple_strntoll(const char *cp, size_t max_chars, char **endp,
+                                unsigned int base)
+ {
+       /*
+        * simple_strntoull() safely handles receiving max_chars==0 in the
+        * case cp[0] == '-' && max_chars == 1.
+        * If max_chars == 0 we can drop through and pass it to simple_strntoull()
+        * and the content of *cp is irrelevant.
+        */
+       if (*cp == '-' && max_chars > 0)
+               return -simple_strntoull(cp + 1, max_chars - 1, endp, base);
+       return simple_strntoull(cp, max_chars, endp, base);
+ }
  /**
   * simple_strtoll - convert a string to a signed long long
   * @cp: The start of the string
   */
  long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  {
-       if (*cp == '-')
-               return -simple_strtoull(cp + 1, endp, base);
-       return simple_strtoull(cp, endp, base);
+       return simple_strntoll(cp, INT_MAX, endp, base);
  }
  EXPORT_SYMBOL(simple_strtoll);
  
@@@ -1798,8 -1824,7 +1824,8 @@@ char *rtc_str(char *buf, char *end, con
              struct printf_spec spec, const char *fmt)
  {
        bool have_t = true, have_d = true;
 -      bool raw = false;
 +      bool raw = false, iso8601_separator = true;
 +      bool found = true;
        int count = 2;
  
        if (check_pointer(&buf, end, tm, spec))
                break;
        }
  
 -      raw = fmt[count] == 'r';
 +      do {
 +              switch (fmt[count++]) {
 +              case 'r':
 +                      raw = true;
 +                      break;
 +              case 's':
 +                      iso8601_separator = false;
 +                      break;
 +              default:
 +                      found = false;
 +                      break;
 +              }
 +      } while (found);
  
        if (have_d)
                buf = date_str(buf, end, tm, raw);
        if (have_d && have_t) {
 -              /* Respect ISO 8601 */
                if (buf < end)
 -                      *buf = 'T';
 +                      *buf = iso8601_separator ? 'T' : ' ';
                buf++;
        }
        if (have_t)
@@@ -2273,7 -2287,7 +2299,7 @@@ early_param("no_hash_pointers", no_hash
   * - 'd[234]' For a dentry name (optionally 2-4 last components)
   * - 'D[234]' Same as 'd' but for a struct file
   * - 'g' For block_device name (gendisk + partition number)
 - * - 't[RT][dt][r]' For time and date as represented by:
 + * - 't[RT][dt][r][s]' For time and date as represented by:
   *      R    struct rtc_time
   *      T    time64_t
   * - 'C' For a clock, it prints the name (Common Clock Framework) or address
@@@ -3538,8 -3552,12 +3564,12 @@@ int vsscanf(const char *buf, const cha
                str = skip_spaces(str);
  
                digit = *str;
-               if (is_sign && digit == '-')
+               if (is_sign && digit == '-') {
+                       if (field_width == 1)
+                               break;
                        digit = *(str + 1);
+               }
  
                if (!digit
                    || (base == 16 && !isxdigit(digit))
                        break;
  
                if (is_sign)
-                       val.s = qualifier != 'L' ?
-                               simple_strtol(str, &next, base) :
-                               simple_strtoll(str, &next, base);
+                       val.s = simple_strntoll(str,
+                                               field_width >= 0 ? field_width : INT_MAX,
+                                               &next, base);
                else
-                       val.u = qualifier != 'L' ?
-                               simple_strtoul(str, &next, base) :
-                               simple_strtoull(str, &next, base);
-               if (field_width > 0 && next - str > field_width) {
-                       if (base == 0)
-                               _parse_integer_fixup_radix(str, &base);
-                       while (next - str > field_width) {
-                               if (is_sign)
-                                       val.s = div_s64(val.s, base);
-                               else
-                                       val.u = div_u64(val.u, base);
-                               --next;
-                       }
-               }
+                       val.u = simple_strntoull(str,
+                                                field_width >= 0 ? field_width : INT_MAX,
+                                                &next, base);
  
                switch (qualifier) {
                case 'H':       /* that's 'hh' in format */