>>> On 04.11.12 at 14:26, Namit Gupta <[email protected]> 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 <[email protected]> > 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 <[email protected]> > --- > 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 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/

