All of lore.kernel.org
 help / color / mirror / Atom feed
From: Walter Harms <wharms@bfs.de>
To: Alejandro Colomar <alx@kernel.org>, Martin Uecker <uecker@tugraz.at>
Cc: Bruno Haible <bruno@clisp.org>,
	"linux-man@vger.kernel.org" <linux-man@vger.kernel.org>
Subject: AW: AW: clumsy cast in dlopen.3
Date: Sun, 17 May 2026 14:10:04 +0000	[thread overview]
Message-ID: <b7f2b29f15e04a70b1d1cf444411df26@bfs.de> (raw)
In-Reply-To: <agc3u2sYC_DfeCQs@devuan>

Just for clarification, my comment was not about the patch itself.
I was just expressing my fear that a comment discussing various complex
topics will be added to the example and confusing future readers.
I have notice that Bruno has send a patch removing most of the comments,
i am happy with that outcome.

CU
________________________________________
Von: Alejandro Colomar <alx@kernel.org>
Gesendet: Freitag, 15. Mai 2026 17:15:03
An: Martin Uecker
Cc: Walter Harms; Bruno Haible; linux-man@vger.kernel.org
Betreff: Re: AW: clumsy cast in dlopen.3

Hi Martin,

On 2026-05-15T16:05:24+0200, Martin Uecker wrote:
>
> There is no such thing needed here, you just do
>
> double (*cosine)(double);
>
> cosine = dlsym(handle, "cos");

Hmmmm, yes.  I don't know why the manual page uses a cast in the POSIX
version.

The above wouldn't be enough in ISO C, since void* isn't guaranteed to
work with function pointers.  However, any systems where dlsym(3) works
must support holding function pointers in void* (because otherwise,
dlsym(3) itself wouldn't work), so ISO C shouldn't be a concern at all.

Hmmm; I'll simplify the page.

Thanks!


Have a lovely day!
Alex

>
>
> Best,
> Martin
>
> Am Freitag, dem 15.05.2026 um 11:56 +0200 schrieb Alejandro Colomar:
> > 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 <alx@kernel.org>
> > > Gesendet: Donnerstag, 14. Mai 2026 13:29:20
> > > An: Bruno Haible
> > > Cc: linux-man@vger.kernel.org; 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>

  reply	other threads:[~2026-05-17 14:20 UTC|newest]

Thread overview: 10+ 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
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-17 14:10           ` Walter Harms [this message]
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=b7f2b29f15e04a70b1d1cf444411df26@bfs.de \
    --to=wharms@bfs.de \
    --cc=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.