[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1467217334-58990-1-git-send-email-zengzhaoxiu@163.com>
Date: Thu, 30 Jun 2016 00:22:13 +0800
From: zengzhaoxiu@....com
To: linux-kernel@...r.kernel.org
Cc: Zhaoxiu Zeng <zhaoxiu.zeng@...il.com>,
Andrew Morton <akpm@...ux-foundation.org>,
Kees Cook <keescook@...omium.org>,
Alexey Dobriyan <adobriyan@...il.com>
Subject: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches
From: Zhaoxiu Zeng <zhaoxiu.zeng@...il.com>
Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@...il.com>
---
lib/kstrtox.c | 22 +++++-----------------
1 file changed, 5 insertions(+), 17 deletions(-)
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index d8a5cf6..70d3374 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -48,38 +48,26 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
{
unsigned long long res;
unsigned int rv;
- int overflow;
+ unsigned int overflow;
+ unsigned int val;
res = 0;
rv = 0;
overflow = 0;
- while (*s) {
- unsigned int val;
-
- if ('0' <= *s && *s <= '9')
- val = *s - '0';
- else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
- val = _tolower(*s) - 'a' + 10;
- else
- break;
-
- if (val >= base)
- break;
+ while ((val = hex_to_bin(*s++)) < base) {
/*
* Check for overflow only if we are within range of
* it in the max base we support (16)
*/
if (unlikely(res & (~0ull << 60))) {
if (res > div_u64(ULLONG_MAX - val, base))
- overflow = 1;
+ overflow = KSTRTOX_OVERFLOW;
}
res = res * base + val;
rv++;
- s++;
}
*p = res;
- if (overflow)
- rv |= KSTRTOX_OVERFLOW;
+ rv |= overflow;
return rv;
}
--
2.7.4
Powered by blists - more mailing lists