diff options
-rw-r--r-- | lib/kstrtox.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/kstrtox.c b/lib/kstrtox.c index 7a94c8f14e29..b1dd3e7d88cb 100644 --- a/lib/kstrtox.c +++ b/lib/kstrtox.c | |||
@@ -44,12 +44,13 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) | |||
44 | * | 44 | * |
45 | * Don't you dare use this function. | 45 | * Don't you dare use this function. |
46 | */ | 46 | */ |
47 | unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *res) | 47 | unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p) |
48 | { | 48 | { |
49 | unsigned long long res; | ||
49 | unsigned int rv; | 50 | unsigned int rv; |
50 | int overflow; | 51 | int overflow; |
51 | 52 | ||
52 | *res = 0; | 53 | res = 0; |
53 | rv = 0; | 54 | rv = 0; |
54 | overflow = 0; | 55 | overflow = 0; |
55 | while (*s) { | 56 | while (*s) { |
@@ -64,12 +65,19 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long | |||
64 | 65 | ||
65 | if (val >= base) | 66 | if (val >= base) |
66 | break; | 67 | break; |
67 | if (*res > div_u64(ULLONG_MAX - val, base)) | 68 | /* |
68 | overflow = 1; | 69 | * Check for overflow only if we are within range of |
69 | *res = *res * base + val; | 70 | * it in the max base we support (16) |
71 | */ | ||
72 | if (unlikely(res & (~0ull << 60))) { | ||
73 | if (res > div_u64(ULLONG_MAX - val, base)) | ||
74 | overflow = 1; | ||
75 | } | ||
76 | res = res * base + val; | ||
70 | rv++; | 77 | rv++; |
71 | s++; | 78 | s++; |
72 | } | 79 | } |
80 | *p = res; | ||
73 | if (overflow) | 81 | if (overflow) |
74 | rv |= KSTRTOX_OVERFLOW; | 82 | rv |= KSTRTOX_OVERFLOW; |
75 | return rv; | 83 | return rv; |