From: Bruno Haible <bruno@clisp.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: bug-gnulib@gnu.org
Subject: Re: intptr_t vs. uintptr_t
Date: Sat, 14 Dec 2019 10:14:44 +0100 [thread overview]
Message-ID: <2783436.Tsuh4lOfYa@omega> (raw)
In-Reply-To: <5120e6e1-1116-dc47-292f-d721014e3aea@cs.ucla.edu>
Hi Paul,
> > "Prefer intptr_t for internal representations of pointers"
> >
> > I disagree with this advice. uintptr_t ought to be used for representing the
> > address of a pointer.
>
> It depends on the application. For example, with two char * pointers P and Q
> into an array, it can be helpful that P - Q yields the same integer as
> ((intptr_t) P - (intptr_t) Q), assuming the usual representation.
Suppose that we have an array that extends from 0x7fff8000 to 0x80003fff - this
can happen! look at the address range maps -, and
- either P = 0x7fffc000 and Q = 0x80003000
then ((intptr_t) P - (intptr_t) Q) causes a signed integer overflow
(value > INTPTR_MAX),
- or vice versa: P = 0x80003000 and Q = 0x7fffc000
then ((intptr_t) P - (intptr_t) Q) causes a signed integer overflow
(value < INTPTR_MIN).
So, with an undefined-behaviour sanitizer enabled, the program will crash.
> That's not true for uintptr_t.
With uintptr_t, the program will not crash for an address difference computation.
The expression (intptr_t)((uintptr_t) P - (uintptr_t) Q) will always return
the expected value. (Here we assume that no array consumes more than half of
the address range, a constraint that is also your justification for using
ptrdiff_t.)
> In practice, Emacs uses uintptr_t quite a bit for things like hashes and tags;
> but it uses intptr_t a bit more, so the advice seems reasonable for Emacs.
No, the advice is bad, even for Emacs.
Bruno
next prev parent reply other threads:[~2019-12-14 9:14 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-22 23:06 [PATCH] grep: a kwset matcher not work in a grep matcher Norihiro Tanaka
2019-03-23 2:49 ` bug#34951: " Norihiro Tanaka
2019-03-23 2:58 ` Budi
2019-03-23 2:59 ` Budi
2019-03-23 12:39 ` Eric Blake
2019-03-29 10:58 ` arnold
2019-12-11 23:25 ` Paul Eggert
2019-12-12 7:23 ` arnold
2019-12-12 7:31 ` arnold
2019-12-12 7:47 ` arnold
2019-12-12 22:26 ` Paul Eggert
2019-12-13 8:09 ` arnold
2019-12-13 12:08 ` arnold
2019-12-13 17:53 ` Jim Meyering
2019-12-13 20:00 ` Paul Eggert
2019-12-14 2:35 ` intptr_t vs. uintptr_t Bruno Haible
2019-12-14 3:19 ` Paul Eggert
2019-12-14 9:14 ` Bruno Haible [this message]
2019-12-14 22:29 ` Paul Eggert
2019-12-15 0:35 ` Bruno Haible
2019-12-16 10:02 ` Paul Eggert
2019-12-15 8:14 ` bug#34951: [PATCH] grep: a kwset matcher not work in a grep matcher arnold
2019-12-16 9:56 ` Paul Eggert
2019-12-16 10:12 ` arnold
2019-12-20 3:18 ` Paul Eggert
2019-12-20 10:35 ` arnold
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
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2783436.Tsuh4lOfYa@omega \
--to=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
--cc=eggert@cs.ucla.edu \
/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).