Clang-6 and GNUisms.
Brooks Davis
brooks at freebsd.org
Mon Mar 12 16:21:45 UTC 2018
On Mon, Mar 12, 2018 at 04:03:44PM +0100, Dimitry Andric wrote:
> On 12 Mar 2018, at 00:56, Ian FREISLICH <ian.freislich at capeaugusta.com> wrote:
> >
> > There's been some fallout in ports land since clang-6 around null
> > pointer arithmetic and casts. I cannot think of a good reason for doing
> > the following but then I've not dabbled in the arcane much:
> >
> > # define __INT_TO_PTR(P) ((P) + (char *) 0)
>
> The idea of this construct is to store integers in pointers, and vice
> versa. This could also be done with unions, but those have their own
> portability issues.
>
> However, arithmetic on a null pointer is undefined according to the C
> and C++ standards, though this particular use case is a GNU extension.
>
> It would be safer and more portable to use intptr_t (or a custom integer
> type that is exactly as large as a pointer), then cast the pointer to
> that type, and vice versa.
>
> E.g.:
>
> #define __INT_TO_PTR(i) ((char *)(intptr_t)(i))
> #define __PTR_TO_INT(p) ((intptr_t)(char *)(p))
This is the only correct way to do this. Please don't a type other than
(__|)(u|)intptr_t for this purpose.
-- Brooks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-current/attachments/20180312/776c02f5/attachment.sig>
More information about the freebsd-current
mailing list