Re: clumsy cast in dlopen.3

Alejandro Colomar <[email protected]>
Newsgroups org.kernel.vger.linux-man
Message-ID <agWw2-7U82vIKSWI@devuan>
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>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmoFsgkACgkQ64mZXMKQ
wqkI4g//dgh59KPQypAztvMDhS+MIRPOnr8g+NkXn3Zcr136+cjb1gJXFUCjHBkH
3W6G/vv3sbUK4MoqJNyG2jsRMQYKHUvlmDQarjSJAB3gTrS0j8a3MW6O2Qv4XcjG
MyIkZbKMppQWuAcXy8q7sUHxO24qqqVD2RuEkSM+JltCMuGQLRxxGRqUVCOHOSdM
3nJTW6plLGioSFzRD+kB4mGoqfPNZfxYcUbjYncZV3ksXUZ1GSZ9Js0Yl+PcTpmy
gU2T+yjD0CQUdoDsA+W04aTFCm98Ui2TXOfbdI2HTKc9tQq1ka0jHXkhm7vHKTJK
cWC6JDkJLNV7jEEO2aD51P6m8jmZ1tx0D7zL6AP2MV6yzZa1DmBYZDaImEZN/DXS
oQTQfx4KPQYXdsBC8zUXpVeud2A2uOhQXu8yZCGZ7aEYqBe9W5XMG/Weak00+IYg
QHv1MibkcgpalOQcWOi9WLrkuJ9V5L/mM5bIF3t/PH1NtqzCeuNDv4iaW+zz6TC9
dW1RvPY02b1ai5jFlaV69xwq0tKWD/SLMdNfy28VYLwheLENShDa47eDDXoqry/R
2SoOw3fO4MVs0FjA9uXheD/te/pUuTjb11gd1M+6SiDsSvL6ABb0OYW+DIEadxI5
0nyWbkEAd3Nx20FF0A+OHrftHMgaE018F1EAU46wDZVuswNnGwc=
=dV4j
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.