Skip to contentSkip to conversation

lib/kstrtox.c break if overflow is detected

PATCH In review Anshul Garg 2 participants 3 messages Jan 21, 2015 – Jan 22, 2015 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/kstrtox.c break if overflow is detected

From: Anshul Garg <[email protected]>

1. While converting string representation to integer break the loop if overflow is detected.
2. Clean kstrtoll function

---
 lib/kstrtox.c |   28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
lib/kstrtox.c +13 -15
index ec8da78..8cbe5ca 100644
@@ -70,8 +70,10 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
 		 * it in the max base we support (16)
 		 */
 		if (unlikely(res & (~0ull << 60))) {
-			if (res > div_u64(ULLONG_MAX - val, base))
+			if (res > div_u64(ULLONG_MAX - val, base)) {
 				overflow = 1;
+				break;
+			}
 		}
 		res = res * base + val;
 		rv++;
@@ -146,23 +148,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
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 51 lines · Anshul Garg

From: Anshul Garg <[email protected]>

1. While converting string representation to integer break the loop if overflow is detected.
2. Clean kstrtoll function

---
 lib/kstrtox.c |   28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
lib/kstrtox.c +6 -15
index ec8da78..8cbe5ca 100644
@@ -70,8 +70,10 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
 		 * it in the max base we support (16)
 		 */
 		if (unlikely(res & (~0ull << 60))) {
-			if (res > div_u64(ULLONG_MAX - val, base))
+			if (res > div_u64(ULLONG_MAX - val, base)) {
 				overflow = 1;
+				break;
+			}
 		}
 		res = res * base + val;
 		rv++;
@@ -146,23 +148,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++;
 	}

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

Quoted 10 lines
lib/kstrtox.c +7 -0
+
+	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);

Looks correct to me, so:

But I believe the two hunks are completely unrelated to eachother and hence should split into a patch series.

Could you please do that and resend? Or if Andrew is OK with picking it up as is, then it's fine.

Thanks,
Levente.

#2 lore raw

Dear Mr. Levente ,

Thanks for the reply.

I will segregate this patch in two different patches.

I will send these patches again.

Thanks
Anshul Garg

Quoted 77 lines · Levente Kurusa

Hi,

Quoted 51 lines · Anshul Garg

From: Anshul Garg <[email protected]>

1. While converting string representation to integer break the loop if overflow is detected.
2. Clean kstrtoll function

---
 lib/kstrtox.c |   28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
lib/kstrtox.c +6 -15
index ec8da78..8cbe5ca 100644
@@ -70,8 +70,10 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
               * it in the max base we support (16)
               */
              if (unlikely(res & (~0ull << 60))) {
-                     if (res > div_u64(ULLONG_MAX - val, base))
+                     if (res > div_u64(ULLONG_MAX - val, base)) {
                              overflow = 1;
+                             break;
+                     }
              }
              res = res * base + val;
              rv++;
@@ -146,23 +148,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++;
      }

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

Quoted 10 lines
lib/kstrtox.c +7 -0
+
+     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);

Looks correct to me, so:

But I believe the two hunks are completely unrelated to eachother and hence should split into a patch series.

Could you please do that and resend? Or if Andrew is OK with picking it up as is, then it's fine.

Thanks,
Levente.