Re: AW: clumsy cast in dlopen.3
Alejandro Colomar <[email protected]>
| Newsgroups | org.kernel.vger.linux-man |
|---|---|
| Message-ID | <agbsgTSLbKb-jz-p@devuan> |
Hi Walter,
On 2026-05-15T08:35:49+0000, Walter Harms wrote:
> Hello,
> I agree the cast is not nice, (someone for a extension of C standard ?)
> but i have to admit that i have never seen the trick with the union.
> But it needs some explaination. The comment in the example is already huge,
> i would ask for a comment subsektion for this behavier here.
The thing about unions is that the only two ways for type punning that
are blessed by ISO C are unions and memcpy(3). Everything else isn't
allowed.
Perfectly valid:
static_assert(sizeof(int) == sizeof(float));
union u {int i; float f;};
float f;
union u u;
u.i = 42;
f = u.f;
Perfectly valid:
static_assert(sizeof(int) == sizeof(float));
int i;
float f;
i = 42;
memcpy(&f, &i, sizeof(float));
UB:
static_assert(sizeof(int) == sizeof(float));
int i;
float f;
i = 42;
f = *(float *) &i;
Have a lovely day!
Alex
> btw: the original code in the example looks like this ...
> cosine = (typeof(double (double)) *) dlsym(handle, "cos");
>
> my2c
> wh
>
> ________________________________________
> Von: Alejandro Colomar <[email protected]>
> Gesendet: Donnerstag, 14. Mai 2026 13:29:20
> An: Bruno Haible
> Cc: [email protected]; Martin Uecker
> Betreff: Re: clumsy cast in dlopen.3
>
> Hi Bruno,
>
> On 2026-05-14T12:56:55+0200, Bruno Haible wrote:
> > The dlopen.3 man page contains this text:
> >
> > *(void **) &cosine = dlsym(handle, "cos");
> >
> > This (clumsy) cast conforms with the ISO C standard and will
> > avoid any compiler warnings.
> >
> > However, such a cast violates the strict aliasing rules of ISO C, no?
>
> I think I agree. Dereferencing the pointer &cosine with a type
> different than the type of the object is not allowed. I've CCed Martin,
> who might be able to confirm.
>
> >
> > The proper workaround is to use a union:
> >
> > union { double (*cosine) (double); void *pointer; } u;
> >
> > u.pointer = dlsym(handle, "cos");
> > ...
> > printf("%f\n", u.cosine(2.0));
>
> This is seems much better, indeed.
>
>
> Have a lovely day!
> Alex
>
> >
> > Bruno
> >
> >
> >
> >
>
> --
> <https://www.alejandro-colomar.es>
--
<https://www.alejandro-colomar.es>
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmoG7dMACgkQ64mZXMKQ wqkX/w/8CoJR0yuorbEOW2k7/RBluCp6iZ86Z8eVlkR7Hj68saY0vNr4V8lcPo/5 YVhtNscgXYqyzSgaNr9YXQiZB6hnb6xfWSs0Qy9chOCACXS/+kFWKQubjjHqDajT 3hpSokvV7LZ4vAFaAP9bAkP6oo9VLRGaOK7XQU62ldgnOcyBfcktGmhP6Klnt+EN Db5rezHVN8YLcZL+w6SpnFzD7DZhPkbRpbzV3wwIUlwLVZ6B33MQAMQ7jM+HDlnm y3SLiXciOxbcImseRh3m9URwZnV65gmdg0ty+XevcyOJtNYMt6FtwS5vv31y2oPz c9gy6TxV3yQh9rjL6wOpaPSJP2o9qHz3SIehoSVzcoNyp7LpJrAM+bpjFWmD3xzW /u8waYTd2RG/zPj5CQc9imRE53xopJVCZhbO/W9jgghVVF6reoxQiAQeUAhNqVad v4OZQEMgwHKN2+7LcBocLxffX95NR9rx4cpyYs97ZkgS7JUCnTCzRqTYrH03uJQc jKuPQxlUPO7rvN5IgO4BSYfvhJ+RV8pHfEnyt7e6KzJU2kMqCgL1hwBVFom1L7gN 7IEL8/p+ul8FdoNNUgRH17asIqNPhGhbfx4jSuhX2a1HZF2EWdS9WPpO3Fy3KFpV jLThQkMxbOBkmDl/uqdfv2gE+ETynz5zXr9iPEpa/TQJzLmhc7o= =Rj1p -----END PGP SIGNATURE-----