Mailinglist Archive: opensuse-packaging (80 mails)

< Previous Next >
Re: [opensuse-packaging] strict-aliasing and dlsym
  • From: Cristian Morales Vega <cmorve69@xxxxxxxx>
  • Date: Sat, 14 Aug 2010 20:41:11 +0200
  • Message-id: <AANLkTimzF-M12XtED5=i7tZ=z2C=EHg2dQOE+u3hRYT+@xxxxxxxxxxxxxx>
2010/8/14 Philipp Thomas <Philipp.Thomas2@xxxxxxx>:
On Fri, 13 Aug 2010 19:44:38 +0200, Cristian Morales Vega
<cmorve69@xxxxxxxx> wrote:

The thing is the offending code is

"*(void **)(&comp_compress) = dlsym(handle, "comp_compress");"

So, the question: could gcc -O2 really break that code? Or that's a
false warning and I can be 100% safe ignoring it?

Yes, it can. The C compiler assumes the normal aliasing rules, i.e.
that objects of one type can only be accessed through pointers of the
same type or char/void pointers. Therefor the compiler will organize
the code depending on whether it finds other accesses or not.

And before you remark it, there is a difference between 'void **' and
'void *' and the standard does only cover 'void *'. The only way I
know of to correct the code would be to use a union, i.e. for the
csine example


         {
          void *handle;
          double (*cosine)(double);
          char *error;
          union{ void *vp; double (*cos)(double); }pun.

          handle = dlopen("libm.so", RTLD_LAZY);

          if (!handle) {
              fprintf(stderr, "%s\n", dlerror());
              exit(EXIT_FAILURE);
          }

          dlerror();    /* Clear any existing error */

          pun.vp = dlsym(handle, "cos);
          cosine = pun.cos.

          if ((error = dlerror()) != NULL)  {
              fprintf(stderr, "%s\n", error);
              exit(EXIT_FAILURE);
          }

GCC guarantees that the union trick works.

a) I hate the world
b) It worked, thanks.

Since I'm going to bookmark this for future reference, for
completeness: any idea about if this is different in C++? Is there
"void *" to a function pointer defined?
Also, I'm not sure about how the Single UNIX Specification, IEEE and
The Open Group are mixed. But at
http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html it
says

"Due to the problem noted here, a future version may either add a new
function to return function pointers, or the current interface may be
deprecated in favor of two new functions: one that returns data
pointers and the other that returns function pointers."

And at the header it says "IEEE Std 1003.1, 2004 Edition". Wikipedia
says there exists POSIX:2008... so, there is any new function that
returns function pointers?
--
To unsubscribe, e-mail: opensuse-packaging+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse-packaging+help@xxxxxxxxxxxx

< Previous Next >