Browse Source

Add carla_strdup_safe function, use it in carla-standalone

tags/1.9.4
falkTX 10 years ago
parent
commit
39c7433dbf
5 changed files with 59 additions and 16 deletions
  1. +2
    -2
      source/backend/CarlaEngine.hpp
  2. +19
    -11
      source/backend/CarlaStandalone.cpp
  3. +3
    -3
      source/backend/engine/CarlaEngine.cpp
  4. +8
    -0
      source/utils/CarlaString.hpp
  5. +27
    -0
      source/utils/CarlaUtils.hpp

+ 2
- 2
source/backend/CarlaEngine.hpp View File

@@ -789,7 +789,7 @@ public:
/*! /*!
* Get plugin with id @a id. * 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. * Get plugin with id @a id, faster unchecked version.
@@ -800,7 +800,7 @@ public:
* Get a unique plugin name within the engine. * Get a unique plugin name within the engine.
* Returned variable must be deleted if non-null. * 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 // Project management


+ 19
- 11
source/backend/CarlaStandalone.cpp View File

@@ -349,7 +349,7 @@ static CarlaNSM gNSM;


static const char* const gNullCharPtr = ""; static const char* const gNullCharPtr = "";


static void checkStringPtr(const char*& charPtr)
static void checkStringPtr(const char*& charPtr) noexcept
{ {
if (charPtr == nullptr) if (charPtr == nullptr)
charPtr = gNullCharPtr; charPtr = gNullCharPtr;
@@ -948,7 +948,7 @@ void carla_set_engine_option(EngineOption option, int value, const char* valueSt
if (gStandalone.engineOptions.audioDevice != nullptr) if (gStandalone.engineOptions.audioDevice != nullptr)
delete[] gStandalone.engineOptions.audioDevice; delete[] gStandalone.engineOptions.audioDevice;


gStandalone.engineOptions.audioDevice = carla_strdup(valueStr);
gStandalone.engineOptions.audioDevice = carla_strdup_safe(valueStr);
break; break;


case CB:: ENGINE_OPTION_NSM_INIT: 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) if (gStandalone.engineOptions.binaryDir != nullptr)
delete[] gStandalone.engineOptions.binaryDir; delete[] gStandalone.engineOptions.binaryDir;


gStandalone.engineOptions.binaryDir = carla_strdup(valueStr);
gStandalone.engineOptions.binaryDir = carla_strdup_safe(valueStr);
break; break;


case CB::ENGINE_OPTION_PATH_RESOURCES: 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) if (gStandalone.engineOptions.resourceDir != nullptr)
delete[] gStandalone.engineOptions.resourceDir; delete[] gStandalone.engineOptions.resourceDir;


gStandalone.engineOptions.resourceDir = carla_strdup(valueStr);
gStandalone.engineOptions.resourceDir = carla_strdup_safe(valueStr);
break; break;


case CB::ENGINE_OPTION_FRONTEND_WIN_ID: case CB::ENGINE_OPTION_FRONTEND_WIN_ID:
@@ -1339,17 +1339,20 @@ const CarlaPluginInfo* carla_get_plugin_info(uint pluginId)
info.optionsEnabled = plugin->getOptionsEnabled(); info.optionsEnabled = plugin->getOptionsEnabled();


plugin->getLabel(strBufLabel); plugin->getLabel(strBufLabel);
info.label = carla_strdup(strBufLabel);
info.label = carla_strdup_safe(strBufLabel);


plugin->getMaker(strBufMaker); plugin->getMaker(strBufMaker);
info.maker = carla_strdup(strBufMaker);
info.maker = carla_strdup_safe(strBufMaker);


plugin->getCopyright(strBufCopyright); plugin->getCopyright(strBufCopyright);
info.copyright = carla_strdup(strBufCopyright);
info.copyright = carla_strdup_safe(strBufCopyright);


checkStringPtr(info.filename); checkStringPtr(info.filename);
checkStringPtr(info.name); checkStringPtr(info.name);
checkStringPtr(info.iconName); checkStringPtr(info.iconName);
checkStringPtr(info.label);
checkStringPtr(info.maker);
checkStringPtr(info.copyright);


return &info; return &info;
} }
@@ -1471,13 +1474,17 @@ const CarlaParameterInfo* carla_get_parameter_info(uint pluginId, uint32_t param
info.scalePointCount = plugin->getParameterScalePointCount(parameterId); info.scalePointCount = plugin->getParameterScalePointCount(parameterId);


plugin->getParameterName(parameterId, strBufName); plugin->getParameterName(parameterId, strBufName);
info.name = carla_strdup(strBufName);
info.name = carla_strdup_safe(strBufName);


plugin->getParameterSymbol(parameterId, strBufSymbol); plugin->getParameterSymbol(parameterId, strBufSymbol);
info.symbol = carla_strdup(strBufSymbol);
info.symbol = carla_strdup_safe(strBufSymbol);


plugin->getParameterUnit(parameterId, strBufUnit); 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 else
carla_stderr2("carla_get_parameter_info(%i, %i) - parameterId out of bounds", pluginId, parameterId); 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); info.value = plugin->getParameterScalePointValue(parameterId, scalePointId);


plugin->getParameterScalePointLabel(parameterId, scalePointId, strBufLabel); plugin->getParameterScalePointLabel(parameterId, scalePointId, strBufLabel);
info.label = carla_strdup(strBufLabel);
info.label = carla_strdup_safe(strBufLabel);
checkStringPtr(info.label);
} }
else else
carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - scalePointId out of bounds", pluginId, parameterId, scalePointId); carla_stderr2("carla_get_parameter_scalepoint_info(%i, %i, %i) - scalePointId out of bounds", pluginId, parameterId, scalePointId);


+ 3
- 3
source/backend/engine/CarlaEngine.cpp View File

@@ -757,7 +757,7 @@ bool CarlaEngine::switchPlugins(const uint idA, const uint idB)
} }
#endif #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->plugins != nullptr, "Invalid engine internal data");
CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "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; 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_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name); carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
@@ -851,7 +851,7 @@ const char* CarlaEngine::getUniquePluginName(const char* const name) const
sname += " (2)"; sname += " (2)";
} }


return sname.dup();
return sname.dupSafe();
} }


// ----------------------------------------------------------------------- // -----------------------------------------------------------------------


+ 8
- 0
source/utils/CarlaString.hpp View File

@@ -551,6 +551,14 @@ public:
return carla_strdup(fBuffer); 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 // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
// Copyright (C) 2004-2008 René Nyffenegger // Copyright (C) 2004-2008 René Nyffenegger


+ 27
- 0
source/utils/CarlaUtils.hpp View File

@@ -284,6 +284,33 @@ const char* carla_strdup_free(char* const strBuf)
return buffer; 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 // memory functions




Loading…
Cancel
Save