From 39c7433dbfba44c1d1632ef7d23b4e64264ad8c2 Mon Sep 17 00:00:00 2001 From: falkTX Date: Wed, 23 Jul 2014 02:57:04 +0100 Subject: [PATCH] Add carla_strdup_safe function, use it in carla-standalone --- source/backend/CarlaEngine.hpp | 4 ++-- source/backend/CarlaStandalone.cpp | 30 +++++++++++++++++---------- source/backend/engine/CarlaEngine.cpp | 6 +++--- source/utils/CarlaString.hpp | 8 +++++++ source/utils/CarlaUtils.hpp | 27 ++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/source/backend/CarlaEngine.hpp b/source/backend/CarlaEngine.hpp index 390834c55..4f86d9e49 100644 --- a/source/backend/CarlaEngine.hpp +++ b/source/backend/CarlaEngine.hpp @@ -789,7 +789,7 @@ public: /*! * Get plugin with id @a id. */ - CarlaPlugin* getPlugin(const uint id) const; + CarlaPlugin* getPlugin(const uint id) const noexcept; /*! * Get plugin with id @a id, faster unchecked version. @@ -800,7 +800,7 @@ public: * Get a unique plugin name within the engine. * Returned variable must be deleted if non-null. */ - const char* getUniquePluginName(const char* const name) const; + const char* getUniquePluginName(const char* const name) const noexcept; // ------------------------------------------------------------------- // Project management diff --git a/source/backend/CarlaStandalone.cpp b/source/backend/CarlaStandalone.cpp index 844606ba5..59f6cb0ce 100644 --- a/source/backend/CarlaStandalone.cpp +++ b/source/backend/CarlaStandalone.cpp @@ -349,7 +349,7 @@ static CarlaNSM gNSM; static const char* const gNullCharPtr = ""; -static void checkStringPtr(const char*& charPtr) +static void checkStringPtr(const char*& charPtr) noexcept { if (charPtr == nullptr) charPtr = gNullCharPtr; @@ -948,7 +948,7 @@ void carla_set_engine_option(EngineOption option, int value, const char* valueSt if (gStandalone.engineOptions.audioDevice != nullptr) delete[] gStandalone.engineOptions.audioDevice; - gStandalone.engineOptions.audioDevice = carla_strdup(valueStr); + gStandalone.engineOptions.audioDevice = carla_strdup_safe(valueStr); break; case CB:: ENGINE_OPTION_NSM_INIT: @@ -963,7 +963,7 @@ void carla_set_engine_option(EngineOption option, int value, const char* valueSt if (gStandalone.engineOptions.binaryDir != nullptr) delete[] gStandalone.engineOptions.binaryDir; - gStandalone.engineOptions.binaryDir = carla_strdup(valueStr); + gStandalone.engineOptions.binaryDir = carla_strdup_safe(valueStr); break; case CB::ENGINE_OPTION_PATH_RESOURCES: @@ -972,7 +972,7 @@ void carla_set_engine_option(EngineOption option, int value, const char* valueSt if (gStandalone.engineOptions.resourceDir != nullptr) delete[] gStandalone.engineOptions.resourceDir; - gStandalone.engineOptions.resourceDir = carla_strdup(valueStr); + gStandalone.engineOptions.resourceDir = carla_strdup_safe(valueStr); break; case CB::ENGINE_OPTION_FRONTEND_WIN_ID: @@ -1339,17 +1339,20 @@ const CarlaPluginInfo* carla_get_plugin_info(uint pluginId) info.optionsEnabled = plugin->getOptionsEnabled(); plugin->getLabel(strBufLabel); - info.label = carla_strdup(strBufLabel); + info.label = carla_strdup_safe(strBufLabel); plugin->getMaker(strBufMaker); - info.maker = carla_strdup(strBufMaker); + info.maker = carla_strdup_safe(strBufMaker); plugin->getCopyright(strBufCopyright); - info.copyright = carla_strdup(strBufCopyright); + info.copyright = carla_strdup_safe(strBufCopyright); checkStringPtr(info.filename); checkStringPtr(info.name); checkStringPtr(info.iconName); + checkStringPtr(info.label); + checkStringPtr(info.maker); + checkStringPtr(info.copyright); return &info; } @@ -1471,13 +1474,17 @@ const CarlaParameterInfo* carla_get_parameter_info(uint pluginId, uint32_t param info.scalePointCount = plugin->getParameterScalePointCount(parameterId); plugin->getParameterName(parameterId, strBufName); - info.name = carla_strdup(strBufName); + info.name = carla_strdup_safe(strBufName); plugin->getParameterSymbol(parameterId, strBufSymbol); - info.symbol = carla_strdup(strBufSymbol); + info.symbol = carla_strdup_safe(strBufSymbol); plugin->getParameterUnit(parameterId, strBufUnit); - info.unit = carla_strdup(strBufUnit); + info.unit = carla_strdup_safe(strBufUnit); + + checkStringPtr(info.name); + checkStringPtr(info.symbol); + checkStringPtr(info.unit); } else carla_stderr2("carla_get_parameter_info(%i, %i) - parameterId out of bounds", pluginId, parameterId); @@ -1520,7 +1527,8 @@ const CarlaScalePointInfo* carla_get_parameter_scalepoint_info(uint pluginId, ui info.value = plugin->getParameterScalePointValue(parameterId, scalePointId); plugin->getParameterScalePointLabel(parameterId, scalePointId, strBufLabel); - info.label = carla_strdup(strBufLabel); + info.label = carla_strdup_safe(strBufLabel); + checkStringPtr(info.label); } else carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - scalePointId out of bounds", pluginId, parameterId, scalePointId); diff --git a/source/backend/engine/CarlaEngine.cpp b/source/backend/engine/CarlaEngine.cpp index d961d5973..865b68b92 100644 --- a/source/backend/engine/CarlaEngine.cpp +++ b/source/backend/engine/CarlaEngine.cpp @@ -757,7 +757,7 @@ bool CarlaEngine::switchPlugins(const uint idA, const uint idB) } #endif -CarlaPlugin* CarlaEngine::getPlugin(const uint id) const +CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept { CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data"); CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data"); @@ -771,7 +771,7 @@ CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept return pData->plugins[id].plugin; } -const char* CarlaEngine::getUniquePluginName(const char* const name) const +const char* CarlaEngine::getUniquePluginName(const char* const name) const noexcept { CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr); carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name); @@ -851,7 +851,7 @@ const char* CarlaEngine::getUniquePluginName(const char* const name) const sname += " (2)"; } - return sname.dup(); + return sname.dupSafe(); } // ----------------------------------------------------------------------- diff --git a/source/utils/CarlaString.hpp b/source/utils/CarlaString.hpp index 03b89ad4c..023f471e6 100644 --- a/source/utils/CarlaString.hpp +++ b/source/utils/CarlaString.hpp @@ -551,6 +551,14 @@ public: return carla_strdup(fBuffer); } + /* + * Return a duplicate string buffer or null. + */ + const char* dupSafe() const noexcept + { + return carla_strdup_safe(fBuffer); + } + // ------------------------------------------------------------------- // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html // Copyright (C) 2004-2008 René Nyffenegger diff --git a/source/utils/CarlaUtils.hpp b/source/utils/CarlaUtils.hpp index 26cbb7b98..61aa31f33 100644 --- a/source/utils/CarlaUtils.hpp +++ b/source/utils/CarlaUtils.hpp @@ -284,6 +284,33 @@ const char* carla_strdup_free(char* const strBuf) return buffer; } +/* + * Custom 'strdup' function, safe version. + * Returned value may be null. + */ +static inline +const char* carla_strdup_safe(const char* const strBuf) noexcept +{ + CARLA_SAFE_ASSERT(strBuf != nullptr); + + const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0; + char* buffer; + + try { + buffer = new char[bufferLen+1]; + } + catch(...) { + return nullptr; + } + + if (strBuf != nullptr && bufferLen > 0) + std::strncpy(buffer, strBuf, bufferLen); + + buffer[bufferLen] = '\0'; + + return buffer; +} + // ----------------------------------------------------------------------- // memory functions