All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Alejandro Colomar <alx@kernel.org>
Cc: liba2i@lists.linux.dev, sc22wg14@open-std.org,
	libbsd@lists.freedesktop.org, tech-misc@netbsd.org,
	christos <christos@netbsd.org>,
	"Đoàn Trần Công Danh" <congdanhqx@gmail.com>,
	"Paul Eggert" <eggert@cs.ucla.edu>,
	"Eli Schwartz" <eschwartz93@gmail.com>,
	"Guillem Jover" <guillem@hadrons.org>,
	"Iker Pedrosa" <ipedrosa@redhat.com>,
	"Michael Vetter" <jubalh@iodoru.org>,
	"Robert Elz" <kre@netbsd.org>,
	riastradh@netbsd.org, "Sam James" <sam@gentoo.org>,
	"Serge E. Hallyn" <serge@hallyn.com>
Subject: Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD
Date: Wed, 19 Mar 2025 01:15:30 +0100	[thread overview]
Message-ID: <3237498.fEcJ0Lxnt5@nimes> (raw)
In-Reply-To: <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




  reply	other threads:[~2025-03-19  0:16 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250318142555.09A86356820@www.open-std.org>
2025-03-18 13:54 ` alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Alejandro Colomar
2025-03-18 21:16   ` Alejandro Colomar
2025-03-18 21:53   ` Bruno Haible
2025-03-18 22:43     ` Alejandro Colomar
2025-03-19  0:15       ` Bruno Haible [this message]
2025-03-19 15:26         ` Alejandro Colomar
2025-03-19 18:48           ` Alejandro Colomar
2025-03-19 18:56             ` Alejandro Colomar
2025-03-19 21:59           ` Bruno Haible
2025-03-19 23:12             ` Alejandro Colomar
2025-03-19 23:30               ` strtou(3) handling of negative input Alejandro Colomar
2025-03-19 23:52               ` alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Thorsten Glaser
2025-03-20  0:19                 ` Alejandro Colomar
2025-03-20  0:31                   ` Thorsten Glaser
2025-03-20  0:36                     ` Alejandro Colomar
2025-03-19 23:52               ` nullability of status parameter in strtoi/u(3) Alejandro Colomar
2025-03-20 12:44               ` alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD Bruno Haible
2025-03-20 12:55                 ` Alejandro Colomar
2025-03-20 17:18                   ` Thorsten Glaser
2025-03-20 14:26               ` Bruno Haible
2025-03-20 14:54                 ` Alejandro Colomar
2025-03-19 19:27         ` Paul Eggert
2025-03-19 20:05           ` Alejandro Colomar
2025-03-19 20:39             ` Paul Eggert
2025-03-19 21:23               ` Alejandro Colomar
2025-03-20  0:39                 ` Paul Eggert
2025-03-20  1:15                   ` Alejandro Colomar
2025-03-20  7:03                     ` Paul Eggert
2025-03-20 10:32                       ` Alejandro Colomar
2025-03-19 15:56       ` Thorsten Glaser
2025-03-19 16:25         ` Alejandro Colomar
2025-03-19 16:36           ` Thorsten Glaser
2025-03-19 16:53             ` Alejandro Colomar
2025-03-19 17:35           ` Bruno Haible
2025-03-19 18:01             ` Alejandro Colomar
2025-03-20 16:13   ` alx-0008r2 " Alejandro Colomar
2025-03-18 17:20 ` [SC22WG14.29900] alx-0008 " Joseph Myers
2025-03-18 20:18   ` Alejandro Colomar
     [not found]   ` <20250318201854.66AB5356895@www.open-std.org>
2025-03-18 21:11     ` [SC22WG14.29912] " Joseph Myers
2025-03-18 21:35       ` Alejandro Colomar
2025-03-18 21:40         ` Alejandro Colomar
2025-03-18 22:14         ` Joseph Myers
2025-03-18 22:49           ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3237498.fEcJ0Lxnt5@nimes \
    --to=bruno@clisp.org \
    --cc=alx@kernel.org \
    --cc=christos@netbsd.org \
    --cc=congdanhqx@gmail.com \
    --cc=eggert@cs.ucla.edu \
    --cc=eschwartz93@gmail.com \
    --cc=guillem@hadrons.org \
    --cc=ipedrosa@redhat.com \
    --cc=jubalh@iodoru.org \
    --cc=kre@netbsd.org \
    --cc=liba2i@lists.linux.dev \
    --cc=libbsd@lists.freedesktop.org \
    --cc=riastradh@netbsd.org \
    --cc=sam@gentoo.org \
    --cc=sc22wg14@open-std.org \
    --cc=serge@hallyn.com \
    --cc=tech-misc@netbsd.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.