[CID43168] rts/linker.c has a memory leak in the dlopen/dlerror code
The Coverity Scan of ghc highlighted a memory leak (CID 43168) in the internal_dlopen function in rts/linker.c. I wrote this code originally and I recall that there was a memory leak in certain situations, but I didn't fix it at the time because it seemed to be rather minor and only would occur in unusual cases. However, since Coverity identified it, I have thought about it and know how to fix the leak, so I am creating this ticket.
The leak occurs because the error message from dlerror must be copied to allocated memory, since the dlerror message is not guaranteed to be stable, and its code is not guaranteed to be reentrant (see POSIX dlopen/dlerror documentation). As I originally coded Trac ticket #2615 (closed), I didn't bother to free the allocated memory. The fix is to make sure that the allocated memory is freed once the error message is displayed. Memory is only allocated if there is an error, so it must be freed only when an error has been identified and processed. As far as I know, this bug only exists on a POSIX system.