diff mbox series

core: ustrtoull: convert zero-length strings to 0

Message ID 20180904224206.24266-1-code@reto-schneider.ch
State Accepted
Headers show
Series core: ustrtoull: convert zero-length strings to 0 | expand

Commit Message

Reto Schneider Sept. 4, 2018, 10:42 p.m. UTC
From: Reto Schneider <reto.schneider@husqvarnagroup.com>

Before 6e4f913d1d267d045ef08cdadf44062951afcedd (ustrtoull refactoring),
zero-length (empty) strings got converted to 0. This commit mimics this
old behavior by checking the string length before calling strtoull which
would set errno to EINVAL for empty strings.

Signed-off-by: Reto Schneider <reto.schneider@husqvarnagroup.com>
---
 core/util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Stefano Babic Sept. 5, 2018, 6:30 a.m. UTC | #1
On 05/09/2018 00:42, Reto Schneider wrote:
> From: Reto Schneider <reto.schneider@husqvarnagroup.com>
> 
> Before 6e4f913d1d267d045ef08cdadf44062951afcedd (ustrtoull refactoring),
> zero-length (empty) strings got converted to 0. This commit mimics this
> old behavior by checking the string length before calling strtoull which
> would set errno to EINVAL for empty strings.
> 
> Signed-off-by: Reto Schneider <reto.schneider@husqvarnagroup.com>
> ---
>  core/util.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/core/util.c b/core/util.c
> index b71b7cc..cf0f7ed 100644
> --- a/core/util.c
> +++ b/core/util.c
> @@ -484,12 +484,12 @@ unsigned long long ustrtoull(const char *cp, unsigned int base)
>  	errno = 0;
>  	char *endp = NULL;
>  
> -	unsigned long long result = strtoull(cp, &endp, base);
> -
>  	if (strnlen(cp, MAX_SEEK_STRING_SIZE) == 0) {
>  		return 0;
>  	}
>  
> +	unsigned long long result = strtoull(cp, &endp, base);
> +
>  	if (cp == endp || (result == ULLONG_MAX && errno == ERANGE)) {
>  		errno = ERANGE;
>  		return 0;
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index b71b7cc..cf0f7ed 100644
--- a/core/util.c
+++ b/core/util.c
@@ -484,12 +484,12 @@  unsigned long long ustrtoull(const char *cp, unsigned int base)
 	errno = 0;
 	char *endp = NULL;
 
-	unsigned long long result = strtoull(cp, &endp, base);
-
 	if (strnlen(cp, MAX_SEEK_STRING_SIZE) == 0) {
 		return 0;
 	}
 
+	unsigned long long result = strtoull(cp, &endp, base);
+
 	if (cp == endp || (result == ULLONG_MAX && errno == ERANGE)) {
 		errno = ERANGE;
 		return 0;