From 432929c18cc1d4547190c1362f348745a440baa4 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 24 Nov 2017 10:51:52 +0100 Subject: [PATCH] Make use of fluid_tuning_set_name where appropriate Also add memory allocation error checks and initialize fluid_tuning_t after allocation. --- src/synth/fluid_tuning.c | 30 ++++++++++++++---------------- src/synth/fluid_tuning.h | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/synth/fluid_tuning.c b/src/synth/fluid_tuning.c index b4b45b3e..54bfc7a9 100644 --- a/src/synth/fluid_tuning.c +++ b/src/synth/fluid_tuning.c @@ -34,11 +34,11 @@ fluid_tuning_t* new_fluid_tuning(const char* name, int bank, int prog) FLUID_LOG(FLUID_PANIC, "Out of memory"); return NULL; } + FLUID_MEMSET(tuning, 0, sizeof(fluid_tuning_t)); - tuning->name = NULL; - - if (name != NULL) { - tuning->name = FLUID_STRDUP(name); + if (fluid_tuning_set_name(tuning, name) != FLUID_OK) { + delete_fluid_tuning(tuning); + return NULL; } tuning->bank = bank; @@ -66,19 +66,12 @@ fluid_tuning_duplicate (fluid_tuning_t *tuning) FLUID_LOG (FLUID_PANIC, "Out of memory"); return NULL; } + FLUID_MEMSET(new_tuning, 0, sizeof(fluid_tuning_t)); - if (tuning->name) - { - new_tuning->name = FLUID_STRDUP (tuning->name); - - if (!new_tuning->name) - { - FLUID_FREE (new_tuning); - FLUID_LOG (FLUID_PANIC, "Out of memory"); - return NULL; - } + if (fluid_tuning_set_name(new_tuning, tuning->name) != FLUID_OK) { + delete_fluid_tuning(new_tuning); + return NULL; } - else new_tuning->name = NULL; new_tuning->bank = tuning->bank; new_tuning->prog = tuning->prog; @@ -129,7 +122,7 @@ fluid_tuning_unref (fluid_tuning_t *tuning, int count) else return FALSE; } -void fluid_tuning_set_name(fluid_tuning_t* tuning, char* name) +int fluid_tuning_set_name(fluid_tuning_t* tuning, const char* name) { if (tuning->name != NULL) { FLUID_FREE(tuning->name); @@ -137,7 +130,12 @@ void fluid_tuning_set_name(fluid_tuning_t* tuning, char* name) } if (name != NULL) { tuning->name = FLUID_STRDUP(name); + if (tuning->name == NULL) { + FLUID_LOG(FLUID_ERR, "Out of memory"); + return FLUID_FAILED; + } } + return FLUID_OK; } char* fluid_tuning_get_name(fluid_tuning_t* tuning) diff --git a/src/synth/fluid_tuning.h b/src/synth/fluid_tuning.h index 39d5ed8b..5558dc6e 100644 --- a/src/synth/fluid_tuning.h +++ b/src/synth/fluid_tuning.h @@ -48,7 +48,7 @@ fluid_tuning_t *fluid_tuning_duplicate (fluid_tuning_t *tuning); void fluid_tuning_ref (fluid_tuning_t *tuning); int fluid_tuning_unref (fluid_tuning_t *tuning, int count); -void fluid_tuning_set_name(fluid_tuning_t* tuning, char* name); +int fluid_tuning_set_name(fluid_tuning_t* tuning, const char* name); char* fluid_tuning_get_name(fluid_tuning_t* tuning); #define fluid_tuning_get_bank(_t) ((_t)->bank)