Skip to contentSkip to conversation

lib/kstrtox.c clean kstrtoll function

PATCH In review Anshul Garg 2 participants 4 messages Jan 22, 2015 – Jan 24, 2015 1 message not in the archive Library code

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 lib/kstrtox.c clean kstrtoll function

From: Anshul Garg <[email protected]>

Instead of having same code for negative and postive integer, use sign variable for integer parsing.

---
 lib/kstrtox.c |   24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
lib/kstrtox.c +10 -14
index ec8da78..744cbb0 100644
@@ -146,23 +146,19 @@ EXPORT_SYMBOL(kstrtoull);
 int kstrtoll(const char *s, unsigned int base, long long *res)
 {
 	unsigned long long tmp;
-	int rv;
+	int rv, sign = 1;
 
 	if (s[0] == '-') {
-		rv = _kstrtoull(s + 1, base, &tmp);
-		if (rv < 0)
-			return rv;
-		if ((long long)(-tmp) >= 0)
-			return -ERANGE;
-		*res = -tmp;
-	} else {
-		rv = kstrtoull(s, base, &tmp);
-		if (rv < 0)
-			return rv;
-		if ((long long)tmp < 0)
-			return -ERANGE;
-		*res = tmp;
+		sign = -1;
+		s++;
 	}
+
+	rv = kstrtoull(s, base, &tmp);
+	if (rv < 0)
+		return rv;
+	if ((long long)tmp < 0)
+		return -ERANGE;
+	*res = sign * tmp;
 	return 0;
 }
 EXPORT_SYMBOL(kstrtoll);
Signature · 6 lines
1.7.9.5


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com
#2 lore raw
Quoted 3 lines · Anshul Garg
lib/kstrtox.c +0 -3
-		if ((long long)(-tmp) >= 0)
-			return -ERANGE;
-		*res = -tmp;

...

lib/kstrtox.c EXPORT_SYMBOL(kstrtoull); · line 161

Quoted 3 lines
lib/kstrtox.c +3 -0
+	if ((long long)tmp < 0)
+		return -ERANGE;
+	*res = sign * tmp;

I don't believe overflow handling is correct anymore with this patch. Did you try with the input as the most negative possible unsigned long?

Jeff

#3 lore raw

Dear Mr. Jeff,

Thanks for the comments.

Yes i think overflow check logic is wrong.

So i think we can change the overflow logic -

Quoted 1 line

From --

if ((long long)tmp < 0)
+ return -ERANGE;

to -

if (((long long)tmp < LLONG_MIN) || ((long long)tmp > LLONG_MAX) )
+ return -ERANGE;

Please give your views on this change..
If this change seems correct i will update overflow logic in my sent patch.

Hope to hear from you soon.

Thanks
Anshul Garg

Quoted 13 lines · Jeff Epler
Quoted 3 lines · Anshul Garg
lib/kstrtox.c +0 -3
-             if ((long long)(-tmp) >= 0)
-                     return -ERANGE;
-             *res = -tmp;

...

lib/kstrtox.c EXPORT_SYMBOL(kstrtoull); · line 161

Quoted 3 lines
lib/kstrtox.c +3 -0
+     if ((long long)tmp < 0)
+             return -ERANGE;
+     *res = sign * tmp;

I don't believe overflow handling is correct anymore with this patch. Did you try with the input as the most negative possible unsigned long?

Jeff

#4 lore raw
Quoted 2 lines · Anshul Garg

if (((long long)tmp < LLONG_MIN) || ((long long)tmp > LLONG_MAX) )
+ return -ERANGE;

This proposed code is still wrong (-ERANGE can never be returned by this statement). It may be best to leave the code alone, rather than propose more untested alternatives.

Jeff