Date: Mon, 12 Mar 2018 16:21:43 +0000 From: Brooks Davis <brooks@freebsd.org> To: Dimitry Andric <dim@FreeBSD.org> Cc: Ian FREISLICH <ian.freislich@capeaugusta.com>, current <current@freebsd.org> Subject: Re: Clang-6 and GNUisms. Message-ID: <20180312162143.GB83713@spindle.one-eyed-alien.net> In-Reply-To: <E03C5179-E64D-4491-B30F-9BA02A048361@FreeBSD.org> References: <4ea06b48-d451-f2b3-4c20-4963f829333b@capeaugusta.com> <E03C5179-E64D-4491-B30F-9BA02A048361@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--s/l3CgOIzMHHjg/5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 12, 2018 at 04:03:44PM +0100, Dimitry Andric wrote: > On 12 Mar 2018, at 00:56, Ian FREISLICH <ian.freislich@capeaugusta.com> w= rote: > >=20 > > 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: > >=20 > > # define __INT_TO_PTR(P) ((P) + (char *) 0) >=20 > 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. >=20 > However, arithmetic on a null pointer is undefined according to the C > and C++ standards, though this particular use case is a GNU extension. >=20 > 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. >=20 > E.g.: >=20 > #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 --s/l3CgOIzMHHjg/5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEcBAEBAgAGBQJapqkWAAoJEKzQXbSebgfAQ4EH/RcWvMY2gh2LgLIx95PCRzmp f3zvQQ3UsMsAfh0oxVNRgwR7Ycx1pTWDZeTvT25HL+CfSBTRKtEwbAGIR/InN48M GUf1grNGFw84iWgNa0rB26ntTQhJHJ/67IK4blIJQTkm3Wikbv5DUB0Jm3K2un3C CJZ2WCy/BAOeDSphpEC+pEGlJNI9GU+DVwZF/fMja2Ox4OaeXDjtnSd5At4dMY3V chuxldYffsPLxAlnBS7AUr7Aa+sE0IZwXOxOyKYKTSErHIce/NGlh3djnPlCpet9 IHIx1O8XMPUovE3+C0fxdrQKkaIuMQLwsTx7tn7Whg3r5YbD/vmmf7ncxLXDWyQ= =M83W -----END PGP SIGNATURE----- --s/l3CgOIzMHHjg/5--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20180312162143.GB83713>