Casting the return value of ...

Liste des GroupesRevenir à l c 
Sujet : Casting the return value of ...
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.lang.c
Date : 28. Mar 2024, 17:09:17
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <uu416t$33u55$1@news.xmission.com>
User-Agent : trn 4.0-test77 (Sep 1, 2010)
I think this is a variation on that old CLC standard "Why you should not
cast the return value of malloc()".

Caution: POSIX (non-strict ISO) stuff coming up. If this bothers you,
please hit "next" right now.

I frequently write "interposer" functions as shared libraries (on Linux).
Generally, this requires using dlsym() and RTLD_NEXT to get a pointer to the
"real" function, so that you can call that as part of your interposer
routine.  I have always done it like this (e.g., for a function returning
char *, taking 2 size_t args - just to pick an arbitrary example to make
things more concrete):

char *someFunction(size_t,size_t) {
    static char * (*real_someFunction) (size_t,size_t);

    if (!real_someFunction)
       real_someFunction = (char * (*real_someFunction) (size_t,size_t)) dlsym(RTLD_NEXT,"someFunction");
...
    }

This works fine (and compiles clean with the usual -W -Wall -Werror).

But here's the thing.  Is the casting of the result of dlsym() necessary?
Isn't this the same as "casting the return value of malloc()", where you
don't need to cast it since it auto-magically gets casted by being assigned
to the thing on the left of the assignment?  Note that it is duplicative,
having to specify the type info for the function pointer twice - once in
the declaration of the pointer and then again in the dlsym() call.  It'd be
better (in terms of maintenance) if you only had to do it once.

But here's where it gets interesting.  In the man page for dlopen(), we
find this example code and a long detailed comment:

           double (*cosine)(double);
...
    cosine = (double (*)(double)) dlsym(handle, "cos");

           /* According to the ISO C standard, casting between function
              pointers and 'void *', as done above, produces undefined results.
              POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and
              proposed the following workaround:

                  *(void **) (&cosine) = dlsym(handle, "cos");

              This (clumsy) cast conforms with the ISO C standard and will
              avoid any compiler warnings.

              The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a.
              POSIX.1-2013) improved matters by requiring that conforming
              implementations support casting 'void *' to a function pointer.
              Nevertheless, some compilers (e.g., gcc with the '-pedantic'
              option) may complain about the cast used in this program. */

So, they seem to think the cast is necessary - or at least a good idea,
Are they right?

And why do we even need the "clumsy" cast?  Why not just:

                  cosine = dlsym(handle, "cos");

--
You are a dreadful man, Kenny, for all your ways are the ways of death.
    - Rick C Hodgin -

(P.S. -> https://www.youtube.com/watch?v=sMmTkKz60W8)

Date Sujet#  Auteur
28 Mar 24 * Casting the return value of ...31Kenny McCormack
28 Mar 24 +* Re: Casting the return value of ...27Kaz Kylheku
28 Mar 24 i+* Re: Casting the return value of ...20Keith Thompson
28 Mar 24 ii`* Re: Casting the return value of ...19bart
28 Mar 24 ii +* Re: Casting the return value of ...12Keith Thompson
28 Mar 24 ii i+- Re: Casting the return value of ...1Chris M. Thomasson
28 Mar 24 ii i+* Re: Casting the return value of ...8Kaz Kylheku
29 Mar 24 ii ii+* Re: Casting the return value of ...5Kaz Kylheku
29 Mar 24 ii iii`* Re: Casting the return value of ...4Kaz Kylheku
29 Mar 24 ii iii `* Re: Casting the return value of ...3Michael S
29 Mar 24 ii iii  `* gcc Bugzilla search (was: Casting the return value of ...)2Michael S
29 Mar 24 ii iii   `- Re: gcc Bugzilla search1David Brown
29 Mar 24 ii ii+- Re: Casting the return value of ...1Keith Thompson
8 Jun 24 ii ii`- Re: Casting the return value of ...1Tim Rentsch
29 Mar 24 ii i`* Re: Casting the return value of ...2David Brown
30 Mar 24 ii i `- Re: Casting the return value of ...1Chris M. Thomasson
29 Mar 24 ii `* Re: Casting the return value of ...6David Brown
29 Mar 24 ii  `* Re: Casting the return value of ...5bart
29 Mar 24 ii   +- Re: Casting the return value of ...1David Brown
30 Mar 24 ii   `* Re: Casting the return value of ...3Tim Rentsch
30 Mar 24 ii    `* Re: Casting the return value of ...2bart
9 Apr 24 ii     `- Re: Casting the return value of ...1Tim Rentsch
30 Mar 24 i`* Re: Casting the return value of ...6Tim Rentsch
31 Mar 24 i +* Re: Casting the return value of ...3Lawrence D'Oliveiro
31 Mar 24 i i+- Re: Casting the return value of ...1David Brown
31 Mar 24 i i`- Re: Casting the return value of ...1Chris M. Thomasson
9 Apr 24 i `* Re: Casting the return value of ...2Tim Rentsch
9 Apr 24 i  `- Re: Casting the return value of ...1David Brown
29 Mar 24 +* Re: Casting the return value of ...2Andrey Tarasevich
29 Mar 24 i`- Re: Casting the return value of ...1Keith Thompson
30 Mar 24 `- Re: Casting the return value of ...1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal