xfs
[Top] [All Lists]

Re: [PATCH] Init errno before strto* calls.

To: Arkadiusz MiÅkiewicz <arekm@xxxxxxxx>
Subject: Re: [PATCH] Init errno before strto* calls.
From: Dave Chinner <david@xxxxxxxxxxxxx>
Date: Mon, 14 Jul 2014 09:04:06 +1000
Cc: xfs@xxxxxxxxxxx
Delivered-to: xfs@xxxxxxxxxxx
In-reply-to: <1405273088-7956-1-git-send-email-arekm@xxxxxxxx>
References: <1405107244-14234-1-git-send-email-arekm@xxxxxxxx> <1405273088-7956-1-git-send-email-arekm@xxxxxxxx>
User-agent: Mutt/1.5.21 (2010-09-15)
On Sun, Jul 13, 2014 at 07:38:08PM +0200, Arkadiusz MiÅkiewicz wrote:
> Eric Sandeen noted that strtol(3) and friends require errno
> initialization. From (fresh) man page:
> 
> NOTES
>        Since  strtol()  can  legitimately  return  0,  LONG_MAX,
>        or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both
>        success and failure, the calling  program should set errno
>        to 0 before the call, and then determine if an error
>        occurred by checking  whether  errno  has  a non-zero
>        value after the call.
> 
> So do it.
> ---
>  libxcmd/input.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/libxcmd/input.c b/libxcmd/input.c
> index 397a124..711b527 100644
> --- a/libxcmd/input.c
> +++ b/libxcmd/input.c
> @@ -153,6 +153,7 @@ cvtnum(
>       char            *sp;
>       int             c;
>  
> +     errno = 0;
>       i = strtoll(s, &sp, 0);
>       if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE)
>               return -1LL;

I think that just checking for (errno != 0) is better here, because
then we also catch errors from unsupported formats or invalid
strings (i.e. EINVAL).

i.e. if the result is out of range, then ERANGE is always returned,
so we don't need to check the actual value at all...

Both patches could be condensed down to a single patch that does:

+       errno = 0;
        i = strtoll(s, &sp, 0);
+       if (errno)
+               return -1LL;

Cheers,

Dave.
-- 
Dave Chinner
david@xxxxxxxxxxxxx

<Prev in Thread] Current Thread [Next in Thread>