From patchwork Fri Apr 8 12:47:22 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Longchamp X-Patchwork-Id: 90333 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3BAB2B6F7A for ; Fri, 8 Apr 2011 23:01:40 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 427DF280D1; Fri, 8 Apr 2011 15:00:30 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 80NDY9dYgdrt; Fri, 8 Apr 2011 15:00:30 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E13FD280E8; Fri, 8 Apr 2011 14:58:30 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B751E280CC for ; Fri, 8 Apr 2011 14:58:18 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Q7DRBAPPCTM1 for ; Fri, 8 Apr 2011 14:58:17 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.ch.keymile.com (mail.ch.keymile.com [193.17.201.103]) by theia.denx.de (Postfix) with SMTP id 01B3128089 for ; Fri, 8 Apr 2011 14:58:01 +0200 (CEST) Received: from SRVCHBER1212.ch.keymile.net ([172.31.32.9]) by eSafe SMTP Relay 1297237168; Fri, 08 Apr 2011 14:36:32 +0100 Received: from localhost.localdomain ([172.31.32.134]) by SRVCHBER1212.ch.keymile.net with Microsoft SMTPSVC(6.0.3790.4675); Fri, 8 Apr 2011 14:47:56 +0200 From: Valentin Longchamp To: u-boot@lists.denx.de Date: Fri, 8 Apr 2011 14:47:22 +0200 Message-Id: X-Mailer: git-send-email 1.7.0.5 In-Reply-To: In-Reply-To: References: References: X-OriginalArrivalTime: 08 Apr 2011 12:47:56.0494 (UTC) FILETIME=[2F0186E0:01CBF5EB] X-ESAFE-STATUS: Mail allowed X-ESAFE-DETAILS: Cc: Valentin Longchamp , hs@denx.de, holger.brunck@keymile.com Subject: [U-Boot] [PATCH] lib, vsprintf: introduce strict_strtoul X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Heiko Schocher as checkpatch proposes to use strict_strtoul instead of simple_strtoul, introduce it. Signed-off-by: Heiko Schocher cc: Wolfgang Denk cc: Detlev Zundel cc: Valentin Longchamp cc: Holger Brunck Signed-off-by: Valentin Longchamp --- include/_exports.h | 1 + include/common.h | 1 + include/exports.h | 1 + lib/vsprintf.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 0 deletions(-) diff --git a/include/_exports.h b/include/_exports.h index d89b65b..349a3c5 100644 --- a/include/_exports.h +++ b/include/_exports.h @@ -19,6 +19,7 @@ EXPORT_FUNC(do_reset) EXPORT_FUNC(getenv) EXPORT_FUNC(setenv) EXPORT_FUNC(simple_strtoul) +EXPORT_FUNC(strict_strtoul) EXPORT_FUNC(simple_strtol) EXPORT_FUNC(strcmp) EXPORT_FUNC(i2c_write) diff --git a/include/common.h b/include/common.h index 49c884c..d180da2 100644 --- a/include/common.h +++ b/include/common.h @@ -648,6 +648,7 @@ void udelay (unsigned long); /* lib/vsprintf.c */ ulong simple_strtoul(const char *cp,char **endp,unsigned int base); +int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base); long simple_strtol(const char *cp,char **endp,unsigned int base); void panic(const char *fmt, ...) diff --git a/include/exports.h b/include/exports.h index 6382311..2f49cea 100644 --- a/include/exports.h +++ b/include/exports.h @@ -20,6 +20,7 @@ void __udelay(unsigned long); unsigned long get_timer(unsigned long); int vprintf(const char *, va_list); unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base); +int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); char *getenv (char *name); int setenv (char *varname, char *varvalue); long simple_strtol(const char *cp,char **endp,unsigned int base); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 61e6f0d..0eda799 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #if !defined (CONFIG_PANIC_HANG) @@ -61,6 +62,52 @@ unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base) return result; } +/** + * strict_strtoul - convert a string to an unsigned long strictly + * @cp: The string to be converted + * @base: The number base to use + * @res: The converted result value + * + * strict_strtoul converts a string to an unsigned long only if the + * string is really an unsigned long string, any string containing + * any invalid char at the tail will be rejected and -EINVAL is returned, + * only a newline char at the tail is acceptible because people generally + * change a module parameter in the following way: + * + * echo 1024 > /sys/module/e1000/parameters/copybreak + * + * echo will append a newline to the tail. + * + * It returns 0 if conversion is successful and *res is set to the converted + * value, otherwise it returns -EINVAL and *res is set to 0. + * + * simple_strtoul just ignores the successive invalid characters and + * return the converted value of prefix part of the string. + */ +int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) +{ + char *tail; + unsigned long val; + size_t len; + + *res = 0; + len = strlen(cp); + if (len == 0) + return -EINVAL; + + val = simple_strtoul(cp, &tail, base); + if (tail == cp) + return -EINVAL; + + if ((*tail == '\0') || + ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { + *res = val; + return 0; + } + + return -EINVAL; +} + long simple_strtol(const char *cp,char **endp,unsigned int base) { if(*cp=='-')