lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 2 January 2015 at 11:37, Daurnimator <> wrote:
> On 2 January 2015 at 08:36, Luiz Henrique de Figueiredo
> <> wrote:
>> We expect the compilation will go smoothly as usual
>> but please report any warnings or other glitches.
> Compiling with gcc's --pedantic gives a single warning:
> loadlib.c: In function ‘lsys_sym’:
> loadlib.c:151:21: warning: ISO C forbids conversion of object pointer
> to function pointer type [-Wpedantic]
>    lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
>                      ^
> To get around this, you need to use this hack: (as seem in dlsym manpage/POSIX):
>     double (*cosine)(double);
>     /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
>        would seem more natural, but the C99 standard leaves
>        casting from "void *" to a function pointer undefined.
>        The assignment used below is the POSIX.1-2003 (Technical
>        Corrigendum 1) workaround; see the Rationale for the
>        POSIX specification of dlsym(). */
>    *(void **) (&cosine) = dlsym(handle, "cos");

Code currently in rc3:

    static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
      lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
      if (f == NULL) lua_pushstring(L, dlerror());
      return f;

Additionally, the check should not be `f == NULL`. From man page:

> Since the value of the symbol could actually be NULL (so that a NULL return from dlsym()
> need not indicate an error), the correct way to test for an error is to call dlerror() to clear any
> old error conditions, then call dlsym(), and then call dlerror() again, saving its return value into
> a variable, and check whether this saved value is not NULL.

The code should look like:

    static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
      lua_CFunction f;
      char *error;
      dlerror(); /* Clear any existing error */
      *(void **)(&f) = dlsym(lib, sym);
      if ((error = dlerror()) != NULL) lua_pushstring(L, error);
      return f;