Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use length-bounded string/memory functions #7709

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions plugins/ReverbSC/base.c
Original file line number Diff line number Diff line change
@@ -4,44 +4,24 @@
#include <math.h>
#include "base.h"

int sp_create(sp_data **spp)
{
*spp = (sp_data *) malloc(sizeof(sp_data));
sp_data *sp = *spp;
sprintf(sp->filename, "test.wav");
sp->nchan = 1;
SPFLOAT *out = malloc(sizeof(SPFLOAT) * sp->nchan);
*out = 0;
sp->out = out;
sp->sr = 44100;
sp->len = 5 * sp->sr;
sp->pos = 0;
sp->rand = 0;
return 0;
}
int sp_create(sp_data **spp) { return sp_createn(spp, 1); }

int sp_createn(sp_data **spp, int nchan)
{
*spp = (sp_data *) malloc(sizeof(sp_data));
sp_data *sp = *spp;
sprintf(sp->filename, "test.wav");
sp->nchan = nchan;
SPFLOAT *out = malloc(sizeof(SPFLOAT) * sp->nchan);
*out = 0;
sp->out = out;
sp->sr = 44100;
sp->len = 5 * sp->sr;
sp->pos = 0;
sp->rand = 0;
return 0;
const uint32_t sr = 44100; // TODO C23: constexpr auto
const unsigned long len_seconds = 5; // TODO C23: constexpr auto
*spp = malloc(sizeof(sp_data));
**spp = (sp_data){ .out = calloc(nchan, sizeof(float)), .sr = sr,
.nchan = nchan, .len = len_seconds * sr, .pos = 0,
.filename = "test.wav", .rand = 0 };
return 0;
}

int sp_destroy(sp_data **spp)
{
sp_data *sp = *spp;
free(sp->out);
free(*spp);
return 0;
free((*spp)->out);
free(*spp);
return 0;
}

#ifndef NO_LIBSNDFILE
@@ -61,7 +41,7 @@ int sp_process(sp_data *sp, void *ud, void (*callback)(sp_data *, void *))
sf[0] = sf_open(sp->filename, SFM_WRITE, &info);
} else {
for(chan = 0; chan < sp->nchan; chan++) {
sprintf(tmp, "%02d_%s", chan, sp->filename);
snprintf(tmp, sizeof(tmp), "%02d_%s", chan, sp->filename);
sf[chan] = sf_open(tmp, SFM_WRITE, &info);
}
}
120 changes: 65 additions & 55 deletions plugins/VstBase/RemoteVstPlugin.cpp
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
*
*/

#include <cmath>

#include "lmmsconfig.h"

@@ -1005,13 +1006,14 @@ bool RemoteVstPlugin::load( const std::string & _plugin_file )
return false;
}

const char id[5] = {
static_cast<char>(m_plugin->uniqueID >> 24),
static_cast<char>(m_plugin->uniqueID >> 16),
static_cast<char>(m_plugin->uniqueID >> 8),
static_cast<char>(m_plugin->uniqueID ),
0
};

char id[5];
sprintf( id, "%c%c%c%c", ((char *)&m_plugin->uniqueID)[3],
((char *)&m_plugin->uniqueID)[2],
((char *)&m_plugin->uniqueID)[1],
((char *)&m_plugin->uniqueID)[0] );
id[4] = 0;
sendMessage( message( IdVstPluginUniqueID ).addString( id ) );

pluginDispatch( effOpen );
@@ -1237,10 +1239,14 @@ void RemoteVstPlugin::getParameterLabels()

void RemoteVstPlugin::sendCurrentProgramName()
{
char presName[64];
sprintf( presName, "%d/%d: %s", pluginDispatch( effGetProgram ) + 1, m_plugin->numPrograms, programName() );

sendMessage( message( IdVstCurrentProgramName ).addString( presName ) );
char presName[64] = {0};
snprintf(presName, sizeof(presName) - 1,
"%d/%d: %s",
pluginDispatch(effGetProgram) + 1,
m_plugin->numPrograms,
programName()
);
sendMessage(message(IdVstCurrentProgramName).addString(presName));
}


@@ -1365,36 +1371,47 @@ void RemoteVstPlugin::rotateProgram( int offset )

void RemoteVstPlugin::getProgramNames()
{
char presName[1024+256*30];
char curProgName[30];
if (isInitialized() == false) return;
bool progNameIndexed = ( pluginDispatch( 29, 0, -1, curProgName ) == 1 );

if (m_plugin->numPrograms > 1) {
if (progNameIndexed) {
for (int i = 0; i< (m_plugin->numPrograms >= 256?256:m_plugin->numPrograms); i++)
char presName[1024 + 256 * 30] = {};
constexpr auto maxlen = sizeof(presName) - 1;
char curProgName[30] = {};
if (!isInitialized()) { return; }
const bool progNameIndexed = pluginDispatch(29, 0, -1, curProgName) == 1;

if (m_plugin->numPrograms > 1)
{
const auto maxPrograms = std::min(m_plugin->numPrograms, 256);
if (progNameIndexed)
{
for (int i = 0; i < maxPrograms; i++)
{
pluginDispatch( 29, i, -1, curProgName );
if (i == 0) sprintf( presName, "%s", curProgName );
else sprintf( presName + strlen(presName), "|%s", curProgName );
pluginDispatch(29, i, -1, curProgName);
if (i == 0) { snprintf(presName, maxlen, "%s", curProgName); }
else
{
const auto len = strlen(presName);
snprintf(presName + len, maxlen - len, "|%s", curProgName);
}
}
}
else
{
int currProgram = pluginDispatch( effGetProgram );
for (int i = 0; i< (m_plugin->numPrograms >= 256?256:m_plugin->numPrograms); i++)
const int currProgram = pluginDispatch(effGetProgram);
for (int i = 0; i < maxPrograms; i++)
{
pluginDispatch( effSetProgram, 0, i );
if (i == 0) sprintf( presName, "%s", programName() );
else sprintf( presName + strlen(presName), "|%s", programName() );
pluginDispatch(effSetProgram, 0, i);
if (i == 0) { snprintf(presName, maxlen, "%s", programName()); }
else
{
const auto len = strlen(presName);
const auto remaining = std::min(len, maxlen);
snprintf(presName + len, remaining, "|%s", programName());
}
}
pluginDispatch( effSetProgram, 0, currProgram );
pluginDispatch(effSetProgram, 0, currProgram);
}
} else sprintf( presName, "%s", programName() );
} else snprintf(presName, maxlen, "%s", programName());

presName[sizeof(presName)-1] = 0;

sendMessage( message( IdVstProgramNames ).addString( presName ) );
sendMessage(message(IdVstProgramNames).addString(presName));
}


@@ -1682,19 +1699,12 @@ int RemoteVstPlugin::updateInOutCount()

setInputOutputCount( inputCount(), outputCount() );

char buf[64];
sprintf( buf, "inputs: %d output: %d\n", inputCount(), outputCount() );
debugMessage( buf );
char buf[64] = {};
snprintf(buf, sizeof(buf) - 1, "inputs: %d; outputs: %d\n", inputCount(), outputCount());
debugMessage(buf);

if( inputCount() > 0 )
{
m_inputs = new float * [inputCount()];
}

if( outputCount() > 0 )
{
m_outputs = new float * [outputCount()];
}
if (inputCount() > 0) { m_inputs = new float*[inputCount()]; }
if (outputCount() > 0) { m_outputs = new float*[outputCount()]; }

return 1;
}
@@ -1722,9 +1732,9 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
{
static VstTimeInfo _timeInfo;
#ifdef DEBUG_CALLBACKS
char buf[64];
sprintf( buf, "host-callback, opcode = %d\n", (int) _opcode );
SHOW_CALLBACK( buf );
char buf[64] = {};
snprintf(buf, sizeof(buf) - 1, "host-callback, opcode = %d\n", static_cast<int>(_opcode));
SHOW_CALLBACK(buf);
#endif

// workaround for early callbacks by some plugins
@@ -1733,6 +1743,7 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
__plugin->m_plugin = _effect;
}

const auto p = static_cast<char*>(_ptr);
switch( _opcode )
{
case audioMasterAutomate:
@@ -2027,15 +2038,14 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
SHOW_CALLBACK( "amc: audioMasterGetVendorString\n" );
// fills <ptr> with a string identifying the vendor
// (max 64 char)
strcpy( (char *) _ptr, "Tobias Doerffel" );
std::strcpy(p, "Tobias Doerffel");
return 1;

case audioMasterGetProductString:
SHOW_CALLBACK( "amc: audioMasterGetProductString\n" );
// fills <ptr> with a string with product name
// (max 64 char)
strcpy( (char *) _ptr,
"LMMS VST Support Layer (LVSL)" );
std::strcpy(p, "LMMS VST Support Layer (LVSL)");
return 1;

case audioMasterGetVendorVersion:
@@ -2049,12 +2059,12 @@ intptr_t RemoteVstPlugin::hostCallback( AEffect * _effect, int32_t _opcode,
return 0;

case audioMasterCanDo:
SHOW_CALLBACK( "amc: audioMasterCanDo\n" );
return !strcmp( (char *) _ptr, "sendVstEvents" ) ||
!strcmp( (char *) _ptr, "sendVstMidiEvent" ) ||
!strcmp( (char *) _ptr, "sendVstTimeInfo" ) ||
!strcmp( (char *) _ptr, "sizeWindow" ) ||
!strcmp( (char *) _ptr, "supplyIdle" );
SHOW_CALLBACK("amc: audioMasterCanDo\n");
return !std::strcmp(p, "sendVstEvents")
|| !std::strcmp(p, "sendVstMidiEvent")
|| !std::strcmp(p, "sendVstTimeInfo")
|| !std::strcmp(p, "sizeWindow")
|| !std::strcmp(p, "supplyIdle");

case audioMasterGetLanguage:
SHOW_CALLBACK( "amc: audioMasterGetLanguage\n" );
26 changes: 10 additions & 16 deletions src/core/DrumSynth.cpp
Original file line number Diff line number Diff line change
@@ -236,19 +236,11 @@ int DrumSynth::GetPrivateProfileString(
}
else
{
k = static_cast<char*>(b + strlen(b) - 1);
while ((k >= b) && (*k == ' ' || *k == '\t'))
{
--k;
}
*(k + 1) = '\0';

len = strlen(b);
if (len > size - 1)
{
len = size - 1;
}
strncpy(buffer, b, len + 1);
k = &b[strlen(b) - 1];
while ((k >= b) && (*k == ' ' || *k == '\t')) { --k; }
k[1] = '\0';
len = std::min(static_cast<int>(1 + k - b), size - 1);
std::memcpy(buffer, b, len + 1);
}
break;
}
@@ -258,7 +250,9 @@ int DrumSynth::GetPrivateProfileString(
if (len == 0)
{
len = strlen(def);
strncpy(buffer, def, size);
const auto maxlen = std::min(len, size - 1);
std::memcpy(buffer, def, maxlen);
if (maxlen < len) { buffer[maxlen] = 0; }
}

free(line);
@@ -345,7 +339,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
}

// try to read version from input file
strcpy(sec, "General");
std::strcpy(sec, "General");
GetPrivateProfileString(sec, "Version", "", ver, sizeof(ver), dsfile);
ver[9] = 0;
if ((strcasecmp(ver, "DrumSynth") != 0) // input fail
@@ -398,7 +392,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
GetEnv(7, sec, "FilterEnv", dsfile);

// read noise parameters
strcpy(sec, "Noise");
std::strcpy(sec, "Noise");
chkOn[1] = GetPrivateProfileInt(sec, "On", 0, dsfile);
sliLev[1] = GetPrivateProfileInt(sec, "Level", 0, dsfile);
NT = GetPrivateProfileInt(sec, "Slope", 0, dsfile);
6 changes: 1 addition & 5 deletions src/core/main.cpp
Original file line number Diff line number Diff line change
@@ -291,11 +291,7 @@ int main( int argc, char * * argv )
}
else if (arg == "--geometry" || arg == "-geometry")
{
if (arg == "--geometry")
{
// Delete the first "-" so Qt recognize the option
strcpy(argv[i], "-geometry");
}
if (arg == "--geometry") { argv[i]++; } // Delete the first "-" so Qt recognize the option
// option -geometry is filtered by Qt later,
// so we need to check its presence now to
// determine, if the application should run in