Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103674
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: better atoi |
| Date | 2017-02-11 12:08 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87shnlx73d.fsf@bsb.me.uk> (permalink) |
| References | <853138f9-54d1-435f-90d5-ef75a38e65bf@googlegroups.com> <1b1sv5k98w.fsf@pfeifferfamily.net> <874m011wzn.fsf@bsb.me.uk> <1bwpcxiaq2.fsf@pfeifferfamily.net> |
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
>>
>>> fir <profesor.fir@gmail.com> writes:
>>>>
>>>> http://www.cplusplus.com/reference/cstdlib/strtol/
>>>>
>>>> it is said that strtol sets errno to ERANGE but is
>>>> not mentioned with wrong value, so im not sure if
>>>> it can be uniformly checked (probably yest though
>>>> but only not noted (i not checked it though))
>>>
>>> It says it returns LONG_MAX or LONG_MIN in this case (so it appears it
>>> is possible that if the string does in fact represent one of those
>>> values, you can't reliably detect the conversion was successful -- as
>>> we've seen in other places, we can't rely on ERRNO being 0 on success.
>>> We'd sort of hope for it in this function...).
>>
>> I don't think that's the right reading of the situation. Here's the
>> relevant text (with stuff about threads elided):
>>
>> "The value of errno [...] is zero at program startup [...], but is
>> never set to zero by any library function. The value of errno may be
>> set to nonzero by a library function call whether or not there is an
>> error, provided the use of errno is not documented in the description
>> of the function in this International Standard."
>
> I hope you're right
Well, I'm right about the quote -- it's a direct cut-and-paste!!
> -- as I remember it, the documentation required is a
> statement saying it *doesn't* set it unless there is an error.
The three "it"s are confusing me. Do you mean "as I remember it, the
documentation required is a statement saying that strtol *doesn't* set
errno unless there is an error"? Or are you making a more general point
about other functions?
> My recollection is that some functions (I want to say open()?) will
> set it, even though there is no error.
Yes, they may -- provided the standard does not document the use of
errno for that function.
> You have to check the return
> value for error, and then look at errno to see what the error is.
That's the normal pattern, but for the strto* functions you need to test
for a range error and for a conversion error. There's a lot of
confusion about how to use them so here's an example:
bool parse_long(const char *s, long *lp)
{
char *endp;
errno = 0;
*(lp ? lp : &(long){0}) = strtol(s, &endp, 0);
return endp > s && errno != ERANGE && endp[strspn(endp, " \t")] == 0;
}
This function extracts a long int value from a string, putting it in *lp
if lp is not null. The key part is the return. In this case it checks
that a conversion was made (endp > s), that the result was in range
(errno != ERANGE) and that nothing but spaces and tabs follow the
number.
I hope people will pile in in the usual c.l.c way to make sure that this
really is conforming and/or to offer other ways to do this sort of
check.
(The use of strspn makes the function asymmetric -- it accepts other
kinds of space in front of the number but not after. This would be
nicer:
const char *skip(const char *s, int (*test)(int))
{
while (test((unsigned char)*s)) s++;
return s;
}
bool parse_long(const char *s, long *lp)
{
char *endp;
errno = 0;
*(lp ? lp : &(long){0}) = strtol(s, &endp, 0);
return endp > s && errno != ERANGE && *skip(endp, isspace) == 0;
}
but at the expense of complicating parts that were not relevant.)
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:24 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:35 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 14:48 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 14:12 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:36 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 01:07 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 10:10 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 09:26 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:36 -0700
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:18 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:31 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 03:47 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 04:22 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:08 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:13 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:31 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:02 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:08 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:17 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:23 -0800
Re: better atoi Robert Latest <boblatest@yahoo.com> - 2017-02-19 11:11 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:33 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:35 -0700
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-11 17:52 -0500
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:53 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 22:50 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 21:59 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 12:08 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 09:39 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:26 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:30 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 22:58 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:48 -0700
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 02:16 +0000
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-12 09:57 -0500
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:27 -0800
Re: better atoi jameskuyper@verizon.net - 2017-02-12 08:44 -0800
Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 09:15 -0800
Re: better atoi Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-12 09:44 -0800
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 18:32 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 18:56 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:18 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 00:16 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 01:56 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 10:44 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 11:38 +0000
Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:37 +0000
Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 15:00 +0000
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:15 -0800
Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:25 +0000
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:54 -0800
Re: better atoi supercat@casperkitty.com - 2017-02-12 13:13 -0800
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-10 15:16 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:01 -0700
Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-11 01:30 -0800
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:26 -0700
Re: better atoi Nick Bowler <nbowler@draconx.ca> - 2017-02-11 00:26 +0000
Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:32 -0700
Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-19 11:10 -0500
csiph-web