All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/kstrtox.c break if overflow is detected
@ 2015-01-21 19:19 Anshul Garg
  0 siblings, 0 replies; 4+ messages in thread
From: Anshul Garg @ 2015-01-21 19:19 UTC (permalink / raw)
  To: akpm, levex, felipe.contreras, linux-input; +Cc: aksgarg1989, anshul.g

From: Anshul Garg <aksgarg1989@gmail.com>

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

Signed-off-by: Anshul Garg <aksgarg1989@gmail.com>
---
 lib/kstrtox.c |   28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index ec8da78..8cbe5ca 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -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);
-- 
1.7.9.5


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] lib/kstrtox.c break if overflow is detected
  2015-01-21 19:39 ` Levente Kurusa
@ 2015-01-22 13:38   ` Anshul Garg
  0 siblings, 0 replies; 4+ messages in thread
From: Anshul Garg @ 2015-01-22 13:38 UTC (permalink / raw)
  To: Levente Kurusa; +Cc: akpm, Felipe Contreras, linux-kernel, anshul.g

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



On Thu, Jan 22, 2015 at 1:09 AM, Levente Kurusa <levex@linux.com> wrote:
> Hi,
>
> On Wed, Jan 21, 2015 at 11:26:26AM -0800, Anshul Garg wrote:
>> From: Anshul Garg <aksgarg1989@gmail.com>
>>
>> 1. While converting string representation to integer
>> break the loop if overflow is detected.
>> 2. Clean kstrtoll function
>>
>> Signed-off-by: Anshul Garg <aksgarg1989@gmail.com>
>> ---
>>  lib/kstrtox.c |   28 +++++++++++++---------------
>>  1 file changed, 13 insertions(+), 15 deletions(-)
>>
>> diff --git a/lib/kstrtox.c b/lib/kstrtox.c
>> index ec8da78..8cbe5ca 100644
>> --- a/lib/kstrtox.c
>> +++ b/lib/kstrtox.c
>> @@ -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);
>
> Looks correct to me, so:
>
> Reviewed-by: Levente Kurusa <levex@linux.com>
>
> 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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] lib/kstrtox.c break if overflow is detected
  2015-01-21 19:26 Anshul Garg
@ 2015-01-21 19:39 ` Levente Kurusa
  2015-01-22 13:38   ` Anshul Garg
  0 siblings, 1 reply; 4+ messages in thread
From: Levente Kurusa @ 2015-01-21 19:39 UTC (permalink / raw)
  To: Anshul Garg; +Cc: akpm, felipe.contreras, linux-kernel, anshul.g

[-- Attachment #1: Type: text/plain, Size: 2030 bytes --]

Hi,

On Wed, Jan 21, 2015 at 11:26:26AM -0800, Anshul Garg wrote:
> From: Anshul Garg <aksgarg1989@gmail.com>
> 
> 1. While converting string representation to integer
> break the loop if overflow is detected.
> 2. Clean kstrtoll function
> 
> Signed-off-by: Anshul Garg <aksgarg1989@gmail.com>
> ---
>  lib/kstrtox.c |   28 +++++++++++++---------------
>  1 file changed, 13 insertions(+), 15 deletions(-)
> 
> diff --git a/lib/kstrtox.c b/lib/kstrtox.c
> index ec8da78..8cbe5ca 100644
> --- a/lib/kstrtox.c
> +++ b/lib/kstrtox.c
> @@ -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);

Looks correct to me, so:

Reviewed-by: Levente Kurusa <levex@linux.com>

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.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] lib/kstrtox.c break if overflow is detected
@ 2015-01-21 19:26 Anshul Garg
  2015-01-21 19:39 ` Levente Kurusa
  0 siblings, 1 reply; 4+ messages in thread
From: Anshul Garg @ 2015-01-21 19:26 UTC (permalink / raw)
  To: akpm, levex, felipe.contreras, linux-kernel; +Cc: aksgarg1989, anshul.g

From: Anshul Garg <aksgarg1989@gmail.com>

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

Signed-off-by: Anshul Garg <aksgarg1989@gmail.com>
---
 lib/kstrtox.c |   28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index ec8da78..8cbe5ca 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -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);
-- 
1.7.9.5


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-01-22 13:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-21 19:19 [PATCH] lib/kstrtox.c break if overflow is detected Anshul Garg
2015-01-21 19:26 Anshul Garg
2015-01-21 19:39 ` Levente Kurusa
2015-01-22 13:38   ` Anshul Garg

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.