public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: Amol Surati <suratiamol@gmail.com>
Cc: libc-help@sourceware.org, gcc-help@gcc.gnu.org,
	Guillem Jover <guillem@hadrons.org>,
	libbsd@lists.freedesktop.org
Subject: Re: restrictness of strtoi(3bsd) and strtol(3)
Date: Sun, 3 Dec 2023 12:35:42 +0100	[thread overview]
Message-ID: <ZWxoFYR4ZsxRe5V5@debian> (raw)
In-Reply-To: <CA+nuEB-T9Qi8eRwVovsZau33J5o+sAQ7X-MD9wy8Up1C_-3qkA@mail.gmail.com>

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

Hello Amol,

On Sun, Dec 03, 2023 at 04:29:07PM +0530, Amol Surati wrote:
> The section "7. Library" at [1] has some information about the 'restrict'
> keyword.
> 
> I think the restrict keywords compel the programmer to keep the string
> (or that portion of the string that strtol actually accesses) and the
> pointer to a string in non-overlapping memory regions. Calling
> strtol(p, &p, 0) should be well-defined in such cases.

That would justify the restrict on char **restrict, but it doesn't
justify the const char *restrict.

I think a more appropriate prototype would be

	long
	strtol(const char *nptr, char **restrict endptr, int base);

The above means that endptr points to memory that is not pointed to by
anything else in this call.

But any of the following is somewhere between confusing and a lie:

	long
	strtol(const char *nptr,
	       char *restrict *restrict endptr,
	       int base);

	long
	strtol(const char *restrict nptr,
	       char **restrict endptr,
	       int base);

	long
	strtol(const char *restrict nptr,
	       char *restrict *restrict endptr,
	       int base);

These 3 from above all mean the same thing: nptr, endptr, and *endptr
each point to a different memory region.  That's of course a lie,
because nptr and *endptr may alias.  The formal definition by ISO C,
which is in terms of accesses, seems to be compatible with these uses of
restrict, because as long as the function doesn't access the memory, it
doesn't matter if they overlap.  However, that definition of restrict is
useless IMO, and still doesn't justify why the compiler isn't
complaining at call site, where it can't know that strtol(3) won't
use **endptr.

Cheers,
Alex

-- 
<https://www.alejandro-colomar.es/>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-12-03 11:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-02 11:50 Alejandro Colomar
2023-12-02 12:29 ` Alejandro Colomar
2023-12-02 12:34   ` Alejandro Colomar
2023-12-03 10:59     ` Amol Surati
2023-12-03 11:35       ` Alejandro Colomar [this message]
2023-12-03 15:38         ` Amol Surati
2023-12-03 16:33           ` Alejandro Colomar
2023-12-03 16:46             ` 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=ZWxoFYR4ZsxRe5V5@debian \
    --to=alx@kernel.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=guillem@hadrons.org \
    --cc=libbsd@lists.freedesktop.org \
    --cc=libc-help@sourceware.org \
    --cc=suratiamol@gmail.com \
    /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 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).