qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 2/3] block: Add support to "open" /dev/fd/X file


From: Corey Bryant
Subject: Re: [Qemu-devel] [PATCH 2/3] block: Add support to "open" /dev/fd/X filenames
Date: Mon, 04 Jun 2012 12:40:21 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1



On 06/04/2012 12:36 PM, Eric Blake wrote:
On 06/04/2012 10:28 AM, Corey Bryant wrote:

But at least strtol lets you detect errors:

char *tmp;
errno = 0;
fd = strtol(p,&tmp, 10);
if (errno || tmp == p) {
      /* raise your error here */
}

I don't think this is legitimate.  errno can be set under the covers of
library calls even if the strtol() call is successful.

Wrong.  POSIX _specifically_ requires that strtol() leave errno
unchanged unless strtol() is reporting a failure, no matter what other
library calls (if any) are made under the covers by strtol().

In other words, pre-setting errno to 0, then calling strtol(), then
checking errno, _is_ the documented way to check for strtol() failures,
and a correct usage of strtol() MUST use this method.  See also commit
6b0e33be88bbccc3bcb987026089aa09f9622de9.  atoi() does not have this
same guarantee, which makes atoi() worthless at detecting errors in
relation to strtol().


Great! I see it now (excerpt below is from the opengroup specification). This is definitely an exception from normal behavior of C APIs.

"The strtol() function will not change the setting of errno if successful."


I was thinking if strtol returns 0 and errno is 0, perhaps we could
assume success, but I don't think this is guaranteed either.

Actually, it _is_ guaranteed - if you pre-set errno to 0, then call
strtol(), then errno is still 0, then the result did not encounter an
error, so a result of 0 at that point means that you indeed parsed a 0.


Maybe a combination of isdigit() then strtol() will give a better idea
of success.

Not necessary.


--
Regards,
Corey




reply via email to

[Prev in Thread] Current Thread [Next in Thread]