From: Dave Chinner <david@fromorbit.com>
To: "Arkadiusz Miśkiewicz" <arekm@maven.pl>
Cc: xfs@oss.sgi.com
Subject: Re: [PATCH] Init errno before strto* calls.
Date: Mon, 14 Jul 2014 09:04:06 +1000 [thread overview]
Message-ID: <20140713230406.GE22339@dastard> (raw)
In-Reply-To: <1405273088-7956-1-git-send-email-arekm@maven.pl>
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@fromorbit.com
_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs
next prev parent reply other threads:[~2014-07-13 23:04 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-11 19:34 [PATCH] Catch under/overflow cases in cvtnum() and cvttime() Arkadiusz Miśkiewicz
2014-07-11 23:14 ` Eric Sandeen
2014-07-12 6:13 ` Arkadiusz Miśkiewicz
2014-07-12 13:37 ` Eric Sandeen
2014-07-12 13:43 ` Eric Sandeen
2014-07-13 17:38 ` [PATCH] Init errno before strto* calls Arkadiusz Miśkiewicz
2014-07-13 23:04 ` Dave Chinner [this message]
2014-07-14 7:56 ` [PATCH] Detect strto* failures based on errno Arkadiusz Miśkiewicz
2014-07-16 0:00 ` Dave Chinner
2014-07-16 3:01 ` Dave Chinner
2014-07-16 7:44 ` Arkadiusz Miśkiewicz
2014-07-16 23:32 ` Dave Chinner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140713230406.GE22339@dastard \
--to=david@fromorbit.com \
--cc=arekm@maven.pl \
--cc=xfs@oss.sgi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox