Linux Manual Pages development
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: Bruno Haible <bruno@clisp.org>
Cc: linux-man@vger.kernel.org, Martin Uecker <uecker@tugraz.at>
Subject: Re: clumsy cast in dlopen.3
Date: Thu, 14 May 2026 13:29:20 +0200	[thread overview]
Message-ID: <agWw2-7U82vIKSWI@devuan> (raw)
In-Reply-To: <21436742.Yz81rIOvuz@nimes>

[-- Attachment #1: Type: text/plain, Size: 897 bytes --]

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>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2026-05-14 11:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-14 10:56 clumsy cast in dlopen.3 Bruno Haible
2026-05-14 11:29 ` Alejandro Colomar [this message]
2026-05-15  8:35   ` AW: " Walter Harms
2026-05-15  9:56     ` Alejandro Colomar
2026-05-15 14:05       ` Martin Uecker
2026-05-15 15:15         ` Alejandro Colomar
2026-05-15 15:41         ` [PATCH v1] man/man3/dlopen.3: EXAMPLES: Simplify use of dlsym(3) Alejandro Colomar
2026-05-15 16:19           ` Bruno Haible
2026-05-15 22:51             ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=agWw2-7U82vIKSWI@devuan \
    --to=alx@kernel.org \
    --cc=bruno@clisp.org \
    --cc=linux-man@vger.kernel.org \
    --cc=uecker@tugraz.at \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox