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(). > > 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. -- Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
