Browse Source

Make lib_* functions noexcept safe

tags/1.9.4
falkTX 11 years ago
parent
commit
c4491c175b
2 changed files with 33 additions and 18 deletions
  1. +3
    -3
      source/backend/CarlaHost.h
  2. +30
    -15
      source/utils/CarlaLibUtils.hpp

+ 3
- 3
source/backend/CarlaHost.h View File

@@ -132,7 +132,7 @@ typedef struct _CarlaPluginInfo {


#ifdef __cplusplus #ifdef __cplusplus
/*! /*!
* C++ constructor.
* C++ constructor and destructor.
*/ */
_CarlaPluginInfo() noexcept; _CarlaPluginInfo() noexcept;
~_CarlaPluginInfo() noexcept; ~_CarlaPluginInfo() noexcept;
@@ -262,7 +262,7 @@ typedef struct _CarlaParameterInfo {


#ifdef __cplusplus #ifdef __cplusplus
/*! /*!
* C++ constructor.
* C++ constructor and destructor.
*/ */
_CarlaParameterInfo() noexcept; _CarlaParameterInfo() noexcept;
~_CarlaParameterInfo() noexcept; ~_CarlaParameterInfo() noexcept;
@@ -287,7 +287,7 @@ typedef struct _CarlaScalePointInfo {


#ifdef __cplusplus #ifdef __cplusplus
/*! /*!
* C++ constructor.
* C++ constructor and destructor.
*/ */
_CarlaScalePointInfo() noexcept; _CarlaScalePointInfo() noexcept;
~_CarlaScalePointInfo() noexcept; ~_CarlaScalePointInfo() noexcept;


+ 30
- 15
source/utils/CarlaLibUtils.hpp View File

@@ -32,15 +32,21 @@
* May return null, in which case "lib_error" has the error. * May return null, in which case "lib_error" has the error.
*/ */
static inline static inline
void* lib_open(const char* const filename)
void* lib_open(const char* const filename) noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr); CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);


void* ret = nullptr;

try {
#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
return (void*)LoadLibraryA(filename);
ret = (void*)LoadLibraryA(filename);
#else #else
return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
ret = dlopen(filename, RTLD_NOW|RTLD_LOCAL);
#endif #endif
} catch(...) {}

return ret;
} }


/* /*
@@ -48,15 +54,21 @@ void* lib_open(const char* const filename)
* If false is returned,"lib_error" has the error. * If false is returned,"lib_error" has the error.
*/ */
static inline static inline
bool lib_close(void* const lib)
bool lib_close(void* const lib) noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false); CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);


bool ret = false;

try {
#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
return FreeLibrary((HMODULE)lib);
ret = FreeLibrary((HMODULE)lib);
#else #else
return (dlclose(lib) == 0);
ret = (dlclose(lib) == 0);
#endif #endif
} catch(...) {}

return ret;
} }


/* /*
@@ -64,13 +76,13 @@ bool lib_close(void* const lib)
* May return null if the symbol is not found. * May return null if the symbol is not found.
*/ */
static inline static inline
void* lib_symbol(void* const lib, const char* const symbol)
void* lib_symbol(void* const lib, const char* const symbol) noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr); CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr); CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);


#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
return (void*)GetProcAddress((HMODULE)lib, symbol);
return (void*)GetProcAddressA((HMODULE)lib, symbol);
#else #else
return dlsym(lib, symbol); return dlsym(lib, symbol);
#endif #endif
@@ -78,9 +90,10 @@ void* lib_symbol(void* const lib, const char* const symbol)


/* /*
* Return the last operation error ('filename' must not be null). * Return the last operation error ('filename' must not be null).
* May return null.
*/ */
static inline static inline
const char* lib_error(const char* const filename)
const char* lib_error(const char* const filename) noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr); CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);


@@ -88,14 +101,16 @@ const char* lib_error(const char* const filename)
static char libError[2048+1]; static char libError[2048+1];
carla_zeroChar(libError, 2048+1); carla_zeroChar(libError, 2048+1);


LPVOID winErrorString;
DWORD winErrorCode = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
try {
LPVOID winErrorString;
DWORD winErrorCode = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);


std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
LocalFree(winErrorString);
std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
LocalFree(winErrorString);
} catch(...) {}


return libError;
return (libError[0] != '\0') ? libError : nullptr;
#else #else
return dlerror(); return dlerror();
#endif #endif


Loading…
Cancel
Save