From 0b2fe539effb74f2eb0b8e3517fbb006607e1d37 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 8 Jun 2015 10:54:06 -0700 Subject: [PATCH] fix segfault (rookie mistake) --- include/Param.hpp | 5 +++-- template/ladspa.cpp | 35 ++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/include/Param.hpp b/include/Param.hpp index 10b1227..09cf128 100644 --- a/include/Param.hpp +++ b/include/Param.hpp @@ -1,4 +1,5 @@ -#define PARAM_NAME_LEN 24 +// including null-terminator! +#define PARAM_NAME_LEN 25 // TOOD: dump enums in param namespace @@ -24,7 +25,7 @@ typedef enum { } param_default; struct Param { - char name[PARAM_NAME_LEN + 1]; + char name[PARAM_NAME_LEN]; float value, min, max; param_scale scale; param_default def; diff --git a/template/ladspa.cpp b/template/ladspa.cpp index 7c397eb..71445e3 100644 --- a/template/ladspa.cpp +++ b/template/ladspa.cpp @@ -5,7 +5,7 @@ //#REDEFINE #ifndef PARAM_NAME_LEN -#define PARAM_NAME_LEN 24 +#define PARAM_NAME_LEN 25 #endif enum { @@ -18,7 +18,7 @@ enum { #define ALLOC(type, amount) (type *) calloc(amount, sizeof(type)) -char p_default_strings[IO_PLUGS][PARAM_NAME_LEN + 1] = { +char p_default_strings[IO_PLUGS][PARAM_NAME_LEN] = { "Input L", "Input R", "Output L", "Output R" }; @@ -59,16 +59,22 @@ struct plug_t { TEMPLATE struct LADSPA_Plugin : public T { + //static constexpr ulong name_buf_size = (portcount)*PARAM_NAME_LEN; + static constexpr ulong portcount = IO_PLUGS + T::parameters; static Param default_params[T::parameters]; - static LADSPA_PortDescriptor descs[IO_PLUGS + T::parameters]; - static LADSPA_PortRangeHint hints[IO_PLUGS + T::parameters]; - static char names[IO_PLUGS + T::parameters][PARAM_NAME_LEN + 1]; + static LADSPA_PortDescriptor descs[portcount]; + static LADSPA_PortRangeHint hints[portcount]; + static char* names[portcount]; + static char name_buffer[portcount][PARAM_NAME_LEN]; static void init() { + for (int i = 0; i < portcount; i++) + names[i] = name_buffer[i]; + for (int i = 0; i < IO_PLUGS; i++) { - memcpy(names[i], p_default_strings[i], PARAM_NAME_LEN + 1); + memcpy(names[i], p_default_strings[i], PARAM_NAME_LEN); descs[i] = LADSPA_PORT_AUDIO; descs[i] |= (i < 2) ? LADSPA_PORT_INPUT : LADSPA_PORT_OUTPUT; hints[i] = (LADSPA_PortRangeHint){.HintDescriptor = 0}; @@ -79,7 +85,7 @@ struct LADSPA_Plugin : public T { int j = i + IO_PLUGS; Param *p = &default_params[i]; - memcpy(names[j], p->name, PARAM_NAME_LEN + 1); + memcpy(names[j], p->name, PARAM_NAME_LEN); descs[j] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL; hints[j].LowerBound = p->min; @@ -164,10 +170,13 @@ struct LADSPA_Plugin : public T { } }; -TEMPLATE Param LADSPA_Plugin::default_params[T::parameters]; -TEMPLATE LADSPA_PortDescriptor LADSPA_Plugin::descs[IO_PLUGS + T::parameters]; -TEMPLATE LADSPA_PortRangeHint LADSPA_Plugin::hints[IO_PLUGS + T::parameters]; -TEMPLATE char LADSPA_Plugin::names[IO_PLUGS + T::parameters][PARAM_NAME_LEN + 1]; +#define P LADSPA_Plugin +TEMPLATE Param P::default_params[T::parameters]; +TEMPLATE LADSPA_PortDescriptor P::descs[P::portcount]; +TEMPLATE LADSPA_PortRangeHint P::hints[P::portcount]; +TEMPLATE char* P::names[P::portcount]; +TEMPLATE char P::name_buffer[P::portcount][PARAM_NAME_LEN]; +#undef P TEMPLATE static LADSPA_Descriptor gen_desc() { @@ -179,10 +188,10 @@ LADSPA_Descriptor gen_desc() { .Name = T::name, .Maker = T::author, .Copyright = T::copyright, - .PortCount = IO_PLUGS + T::parameters, + .PortCount = T::portcount, .PortDescriptors = T::descs, .PortRangeHints = T::hints, - .PortNames = (const char * const *) T::names, + .PortNames = (const char *const *) T::names, .instantiate = T::plug_construct, .cleanup = T::plug_destruct,