GAMS-dev/gdx

remove p3GetLibName

Closed this issue · 2 comments

I'm linking several of the GDX sources into a program that should use GDX. On some system, a user reported a linking issue because I didn't link against libdl. I didn't think I need this, because the GDX library should not load any library dynamically at runtime (I link against zlib; I don't include p3library.cpp). The issue seems to be that dladdr() is used in a function p3GetLibName(), which is nowhere used. I don't know whether there exists a reason to include it in the GDX library. Can it be removed? (I cannot exclude the whole p3utils.cpp)

diff --git a/src/rtl/p3utils.cpp b/src/rtl/p3utils.cpp
index 590aa35..661e276 100644
--- a/src/rtl/p3utils.cpp
+++ b/src/rtl/p3utils.cpp
@@ -74,7 +74,6 @@
    #endif
    #include <netinet/in.h>
    #include <unistd.h>
-   #include <dlfcn.h>
    #include <poll.h>
 #endif
 
@@ -1161,122 +1160,6 @@ int p3GetExecName( std::string &execName, std::string &msg )
 #endif
 }
 
-// FIXME: Do not always return false!
-static bool isLibrary()
-{
-   return false;
-}
-
-static int xGetLibName( std::string &libName, std::string &msg )
-{
-   char libBuf[4096];
-   msg.clear();
-   int rc;
-
-#if defined( __linux ) || defined( __APPLE__ )
-   {
-      char tmpBuf[2048];
-      static_assert( sizeof( tmpBuf ) == 2048 );
-      Dl_info dlInfo;
-      const int k = dladdr( reinterpret_cast<void *>( &xGetLibName ), &dlInfo );
-      if( k > 0 )
-      {
-         strncpy( tmpBuf, dlInfo.dli_fname, sizeof( tmpBuf ) - 1 );
-         tmpBuf[sizeof( tmpBuf ) - 1] = '\0';
-         if( realpath( tmpBuf, libBuf ) )
-            rc = 0;
-         else
-         {
-            myStrError( errno, tmpBuf, sizeof( tmpBuf ) );
-            msg = "realpath() failure: "s + tmpBuf;
-            *libBuf = '\0';
-            rc = 5;
-         }
-      }
-      else
-      {
-         msg = "dladdr() failure"s;
-         *libBuf = '\0';
-         rc = 4;
-      }
-   }
-#elif defined( _WIN32 )
-   {
-      HMODULE h;
-      int k = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
-                                     GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
-                             reinterpret_cast<LPCTSTR>( &xGetLibName ), &h );
-      if( k )
-      { /* OK: got a handle */
-         k = static_cast<int>( GetModuleFileNameA( h, libBuf, sizeof( libBuf ) ) );
-         if( 0 == k )
-         {
-            msg = "GetModuleFileName() failure: rc="s + rtl::sysutils_p3::IntToStr(k);
-            *libBuf = '\0';
-            rc = 5;
-         }
-         else
-            rc = 0;
-      }
-      else
-      {
-         msg = "GetModuleHandleEx() failure: rc="s + rtl::sysutils_p3::IntToStr(k);
-         *libBuf = '\0';
-         rc = 4;
-      }
-   }
-#else
-   *libBuf = '\0';
-   msg = "not implemented for this platform"s;
-   rc = 8;
-#endif
-   libName.assign( libBuf );
-   return 0 == rc && strlen( libBuf ) > 255 ? 1 : rc;
-} /* xGetLibName */
-
-int p3GetLibName( std::string &libName, std::string &msg )
-{
-   return xGetLibName( libName, msg );
-#if defined( _WIN32 )
-   libName.clear();
-   if( !isLibrary() )
-   {
-      msg = "Not called from a library"s;
-      return 2;
-   }
-   std::array<char, 256> buf;
-   HMODULE hinstance;
-   auto rc { GetModuleFileNameA( hinstance, buf.data(), static_cast<DWORD>(buf.size()) ) };
-   if( !rc )
-   {
-      msg = "GetModuleFileNameA call failed"s;
-      return 3;
-   }
-   else if( rc >= 256 )
-   {
-      libName.assign( buf.data() );
-      msg = "Result truncated to 255 characters"s;
-      return 1;
-   }
-   else
-   {
-      libName.assign( buf.data() );
-      msg.clear();
-      return 0;
-   }
-#else
-   if( !isLibrary() )
-   {
-      libName.clear();
-      msg = "Not called from a library"s;
-      return 2;
-   }
-   libName.clear();
-   msg = "P3: not yet implemented"s;
-   return 9;
-#endif
-}
-
 #ifdef __IN_CPPMEX__
 bool p3GetFirstMACAddress( std::string &mac )
 {
diff --git a/src/rtl/p3utils.h b/src/rtl/p3utils.h
index 668337c..f41ae00 100644
--- a/src/rtl/p3utils.h
+++ b/src/rtl/p3utils.h
@@ -86,7 +86,6 @@ bool P3SetEnvPC( const std::string &name, char *val );
 uint32_t P3GetEnvPC( const std::string &name, char *buf, uint32_t bufSize );
 
 int p3GetExecName( std::string &execName, std::string &msg );
-int p3GetLibName( std::string &libName, std::string &msg );
 
 bool p3GetMemoryInfo( uint64_t &rss, uint64_t &vss );
 
0x17 commented

Thanks for pointing this out. This is indeed dead code that is adding unnecessary dependencies. I applied your patch with commit 3ff67f2 . This will move into GDX main (and GAMS) before release 48.2.

Thank you. Very appreciated.