Groups | Search | Server Info | Login | Register
Groups > muc.lists.netbsd.tech.misc > #91
| From | Bruno Haible <bruno@clisp.org> |
|---|---|
| Newsgroups | muc.lists.netbsd.tech.misc |
| Subject | Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD |
| Date | 2025-03-19 01:15 +0100 |
| Organization | GNU |
| Message-ID | <3237498.fEcJ0Lxnt5@nimes> (permalink) |
| References | <> <mvwnrmk2xf45ivyk4kzxdxuwdk67666yt3kwafck6vo4vq2lru@wkqmoqsacqkf> |
Alejandro Colomar wrote: > > It would be useful to show how a success test looks like, after > > strtoi (s, &end, base, min, max, &status) > > for each of the four frequent use-cases: > > -a. expect to parse the initial portion of the string, no coercion, > > -b. expect to parse the initial portion of the string, silent coercion, > > -c. expect to parse the entire string, no coercion, > > -d. expect to parse the entire string, silent coercion. > > > > AFAICS, the success tests are: > > -a. status == 0 || status == ENOTSUP > > Correct. > > > -b. status == 0 || status == ENOTSUP || status == ERANGE > > Correct (but most likely a bug). > > > -c. status == 0 > > Correct. > > > -d. status == 0 || (status == ERANGE && end > s && *end == '\0') > > You don't need end>s, because that would preclude ERANGE. > > status == 0 || (status == ERANGE && end == '\0') > > Aaand, most likely a bug. Cases b. and d. are not bugs. Often, the programmer knows that treating a value > ULONG_MAX is equivalent to treating the value ULONG_MAX. These are *normal* uses of strto[u]l[l]. Often it is the programmer's intent that the values "4294967297" and "4294967295" produce the same behaviour (the same error message, for example). It is for these cases that your specification contains the clamping / coercion behaviour. Now, when you look at the table of success tests: -a. status == 0 || status == ENOTSUP -b. status == 0 || status == ENOTSUP || status == ERANGE -c. status == 0 -d. status == 0 || (status == ERANGE && *end == '\0') it is immediately clear that the status return convention is ill-designed, because the returned 'status' is not the only thing a programmer has to test after calling the function. > Cases b and d are not real, IMO. I have never seen code where that is > wanted, AFAIR, and I analyzed the entire Debian and NetBSD code bases > looking precisely for that usage. I disagree. Any use of strtoul that does not test errno wants overflow to be mapped to ULONG_MAX, that is, is in case b. or d. Just looking in gnulib and gettext, I find already 6 occurrences: gnulib/lib/getaddrinfo.c:299 gnulib/lib/nproc.c:402 gnulib/lib/omp-init.c:48 gettext/gettext-tools/src/msgfmt.c:287 gettext/gettext-tools/src/msgl-check.c:379 gettext/gettext-tools/src/read-stringtable.c:561 > > I would therefore propose to change the status value to a bit mask, so that > > the error conditions "The converted value was out of range and has been > > coerced" and "The given string contains characters that did not get converted" > > can be both returned together, without conflicting. > > Because it is theoretical conditions that a real program never wants, > let's not do that. If you don't want to do that, I can only repeat what I said in the previous mail: The proposal *does not achieve the goal* of avoiding the most common programmer mistakes. For a robust API, the success test should *only* involve testing the returned 'status', nothing else. Bruno -- Posted automagically by a mail2news gateway at muc.de e.V. Please direct questions, flames, donations, etc. to news-admin@muc.de
Back to muc.lists.netbsd.tech.misc | Previous | Next — Previous in thread | Next in thread | Find similar
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-18 23:43 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Bruno Haible <bruno@clisp.org> - 2025-03-19 01:15 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 16:26 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 19:48 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 19:56 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Bruno Haible <bruno@clisp.org> - 2025-03-19 22:59 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Paul Eggert <eggert@cs.ucla.edu> - 2025-03-19 12:27 -0700
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Thorsten Glaser <tg@mirbsd.org> - 2025-03-19 16:56 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 17:25 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Thorsten Glaser <tg@mirbsd.org> - 2025-03-19 17:36 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 17:53 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Bruno Haible <bruno@clisp.org> - 2025-03-19 18:35 +0100
Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar <alx@kernel.org> - 2025-03-19 19:01 +0100
csiph-web