public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Robustly parse string to unsigned integer (strtoul?)/docs
@ 2020-06-20  1:55 John Scott
  0 siblings, 0 replies; only message in thread
From: John Scott @ 2020-06-20  1:55 UTC (permalink / raw)
  To: libc-help

[-- Attachment #1: Type: text/plain, Size: 2146 bytes --]

Hi,

This is a little off-topic and doesn't pertain to glibc in particular. (If 
there's another mailing list let me know.) Maybe I'm getting stuck in details 
too much but would appreciate a fresh set of eyes.

Specifically, I want to be able to throw an error or handle the case when a 
negative integer is read as a command-line argument from argv[]. This excerpt 
from a man page (I know it's not yours) gave me hope I'd get magnitude back:
> RETURN VALUE
> The strtoul() function returns either the result of the conversion or, if
> there was a leading minus sign, the negation of the result of  the
> conversion represented as an unsigned value, unless the original
> (nonnegated) value would overflow;
so it sounds like "-5" would get me five. The glibc manual says it wraps 
around, so "-1" would get me ULONG_MAX [1], and this is what glibc and Musl do 
in practice it appears.

A draft of the C standard [2, p.345] appears to make it sound like the former—
the man page—is the correct behavior:
>  If the subject sequence begins with a minus sign, the value resulting from
> the conversion is negated (in the return type).

Also, not necessarily dependent on whether some conversion can be done,
> If the correct value is outside the range of representable values, LONG_MIN,
> LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned
> (according to the return type and sign of the value, if any), and the value
> of the macro ERANGE is stored in errno.
and my gut says neither 1 nor UINTMAX_MAX is the "correct" value of -1, but 
perhaps it's the prescribed conversion.

Realistically I'm probably not onto any discrepancy, so what's the best way 
for me to parse an integer if there's no way to recognize when strtoul "gets 
it wrong?" Checking for a minus sign outfront isn't good enough with 
whitespace but the thought of looping over the space characters sounds like 
reinventing the wheel.

Thanks,
John Scott

[1] https://www.gnu.org/software/libc/manual/html_node/Parsing-of-Integers.html#Parsing-of-Integers
[2] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-06-20  1:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20  1:55 Robustly parse string to unsigned integer (strtoul?)/docs John Scott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).