Re: Clang-6 and GNUisms.

From: Brooks Davis <brooks_at_freebsd.org>
Date: Mon, 12 Mar 2018 16:21:43 +0000
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

Received on Mon Mar 12 2018 - 15:21:45 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:15 UTC