lkml.org 
[lkml]   [2012]   [Nov]   [5]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] Lib:The patch include fix for "-" during string to long conversion
>>> On 04.11.12 at 14:26, Namit Gupta <namit2010@gmail.com> wrote:
> API simple_strtol() and simple_strtoul() are giving incorrect result for
> string "-".
> These API's consider "-" as a '-' (negative integer) and return incorrect
> string pointer.
> The API returns pointer next to '-' character. However it should return
> starting pointer of the string.

Where is that behavior specified? If we take the C standard as
reference, it is not being made explicit whether, for "base" other
than zero, the sequence of letters and digits has to be non-empty.

If we take this as implied, then your patch should not only handle
"-" alone, but also cases where "-" is followed by other than a
letter or digit, or an out of range one.

Jan

> Below I have included possible solution for that issue.
> Please review.
>
>
> From eea8b5fd7e5bb7b206a41f5eec934152a77a9431 Mon Sep 17 00:00:00 2001
> From: Namit Gupta <namit2010@gmail.com>
> Date: Sun, 4 Nov 2012 23:36:23 +0530
> Subject: [PATCH 1/1] Lib:The patch include fix for "-" during string to
> long conversion
>
> API simple_strtol and simple_strtoll give wrong result for string "-",
> The API consider "-" as a -ve number which and give incorrect result
> for "-" string.
>
> Signed-off-by: Namit Gupta <namit2010@gmail.com>
> ---
> lib/vsprintf.c | 20 ++++++++++++++++----
> 1 files changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 39c99fe..cde449d 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -76,8 +76,14 @@ EXPORT_SYMBOL(simple_strtoul);
> */
> long simple_strtol(const char *cp, char **endp, unsigned int base)
> {
> - if (*cp == '-')
> - return -simple_strtoul(cp + 1, endp, base);
> + if (*cp == '-') {
> + if (*(cp+1))
> + return -simple_strtoul(cp + 1, endp, base);
> + else if (*endp) { /* The string contains only "-"*/
> + *endp = (char *)cp;
> + return 0;
> + }
> + }
>
> return simple_strtoul(cp, endp, base);
> }
> @@ -91,8 +97,14 @@ EXPORT_SYMBOL(simple_strtol);
> */
> long long simple_strtoll(const char *cp, char **endp, unsigned int base)
> {
> - if (*cp == '-')
> - return -simple_strtoull(cp + 1, endp, base);
> + if (*cp == '-') {
> + if (*(cp+1))
> + return -simple_strtoull(cp + 1, endp, base);
> + else if (*endp) { /* The string contains only "-"*/
> + *endp = (char *)cp;
> + return 0;
> + }
> + }
>
> return simple_strtoull(cp, endp, base);
> }
> --
> 1.7.1
>
>
> Regards,
> Namit





\
 
 \ /
  Last update: 2012-11-05 10:02    [W:0.255 / U:0.104 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site