Skip to content
Advertisement

Why can the value of the symbol returned by dlsym() be null?

In Linux. Per the dlsym(3) Linux man page,

    *Since the value of the symbol could actually be NULL
    (so that a NULL return from dlsym() need not indicate an error),*

Why is this, when can a symbol (for a function, specifically) be actually NULL? I am reviewing code and found a piece using dlerror to clean first, dlsym next, and dlerror to check for errors. But it does not check the resulting function from being null before calling it:

  • dlerror();
  • a_func_name = …dlsym(…);
  • if (dlerror()) goto end;
  • a_func_name(…); // Never checked if a_func_name == NULL;

I am just a reviewer so don’t have the option to just add the check. And perhaps the author knows NULL can never be returned. My job is to challenge that but don’t know what could make this return a valid NULL so I can then check if such a condition could be met in this code’s context. Have not found the right thing to read with Google, a pointer to good documentation would be enough unless you want to explain explicitly which would be great.

Advertisement

Answer

Well, if it’s returned with no errors, then pointer is valid and NULL is about as illegal as any random pointer from the shared object. Like the wrong function, data or whatever.

Advertisement