From c91c9e5eeae06a7cbed94354d62c1281dc356407 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 30 Aug 2023 17:26:56 -0700 Subject: modules/*: til_module_context_free() error paths Several modules still had vestigial ad-hoc free() cleanups on error paths in their create_context(). Largely mechanical change of replacing those with til_module_context_free() which is more appropriate, since the til_module_context_t holds a reference on the setup. A plain free() will leak that reference. But it's only on create_context() failures which are uncommon, so this was in practice mostly harmless... --- src/modules/drizzle/drizzle.c | 6 ++---- src/modules/signals/signals.c | 30 +++++++++++------------------- src/modules/sparkler/sparkler.c | 10 +++++----- src/modules/stars/stars.c | 3 ++- src/modules/swab/swab.c | 6 ++---- 5 files changed, 22 insertions(+), 33 deletions(-) (limited to 'src/modules') diff --git a/src/modules/drizzle/drizzle.c b/src/modules/drizzle/drizzle.c index 54ca204..18ef731 100644 --- a/src/modules/drizzle/drizzle.c +++ b/src/modules/drizzle/drizzle.c @@ -110,10 +110,8 @@ static til_module_context_t * drizzle_create_context(const til_module_t *module, return NULL; ctxt->puddle = puddle_new(PUDDLE_SIZE, PUDDLE_SIZE); - if (!ctxt->puddle) { - free(ctxt); - return NULL; - } + if (!ctxt->puddle) + return til_module_context_free(&ctxt->til_module_context); ctxt->taps.viscosity = til_tap_init_float(ctxt, &ctxt->viscosity, 1, &ctxt->vars.viscosity, "viscosity"); ctxt->taps.rainfall = til_tap_init_float(ctxt, &ctxt->rainfall, 1, &ctxt->vars.rainfall, "rainfall"); diff --git a/src/modules/signals/signals.c b/src/modules/signals/signals.c index c56c6f8..76fe4f6 100644 --- a/src/modules/signals/signals.c +++ b/src/modules/signals/signals.c @@ -38,7 +38,7 @@ static til_module_context_t * signals_create_context(const til_module_t *module, ctxt->signals[0] = sig_new_sin(sig_new_const(.5f)); /* oscillate @ .5hz */ if (!ctxt->signals[0]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[1] = sig_new_sin( /* oscillate */ sig_new_scale( /* at a scaled frequency */ @@ -48,7 +48,7 @@ static til_module_context_t * signals_create_context(const til_module_t *module, ) ); if (!ctxt->signals[1]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[2] = sig_new_lerp( /* interpolate */ sig_new_sin(sig_new_const(.33f)), /* a .33hz oscillator */ @@ -56,21 +56,21 @@ static til_module_context_t * signals_create_context(const til_module_t *module, sig_new_sin(sig_new_const(2.f)) /* weighted by a 2hz oscillator */ ); if (!ctxt->signals[2]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[3] = sig_new_pow( /* raise */ sig_new_sin(sig_new_const(4.f)), /* a 4hz oscillator */ sig_new_sin(sig_new_const(.33f)) /* to the power of a .33f hz oscillator */ ); if (!ctxt->signals[3]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[4] = sig_new_mult( /* multiply */ sig_new_sin(sig_new_const(4.f)), /* a 4hz oscillator */ sig_new_sin(sig_new_const(1.f)) /* by a 1hz oscillator */ ); if (!ctxt->signals[4]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[5] = sig_new_lerp( /* interpolate */ sig_ref(ctxt->signals[3]), /* signals[3] */ @@ -78,7 +78,7 @@ static til_module_context_t * signals_create_context(const til_module_t *module, sig_ref(ctxt->signals[2]) /* weighted by signals[2] */ ); if (!ctxt->signals[5]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[6] = sig_new_lerp( /* interpolate */ sig_new_inv(sig_ref(ctxt->signals[5])), /* invert of signals[5] */ @@ -87,21 +87,21 @@ static til_module_context_t * signals_create_context(const til_module_t *module, sig_ref(ctxt->signals[2]) /* weighted by signals[2] */ ); if (!ctxt->signals[6]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[7] = sig_new_mult( /* multiply */ sig_ref(ctxt->signals[6]), /* signals[6] */ sig_ref(ctxt->signals[5]) /* signals[5] */ ); if (!ctxt->signals[7]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[8] = sig_new_mult( /* multiply */ sig_ref(ctxt->signals[1]), /* signals[1] */ sig_ref(ctxt->signals[7]) /* signals[7] */ ); if (!ctxt->signals[8]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[9] = sig_new_pow( /* raise */ sig_ref(ctxt->signals[3]), /* signals[3] */ @@ -112,7 +112,7 @@ static til_module_context_t * signals_create_context(const til_module_t *module, ) ); if (!ctxt->signals[9]) - goto err; + return til_module_context_free(&ctxt->til_module_context); ctxt->signals[10] = sig_new_lerp( /* interpolate */ sig_ref(ctxt->signals[9]), /* signals[9] */ @@ -125,17 +125,9 @@ static til_module_context_t * signals_create_context(const til_module_t *module, ) ); if (!ctxt->signals[10]) - goto err; + return til_module_context_free(&ctxt->til_module_context); return &ctxt->til_module_context; - -err: - for (unsigned i = 0; i < N_SIGNALS; i++) - sig_free(ctxt->signals[i]); - - free(ctxt); - - return NULL; } diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 76f1c9b..e37257c 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -50,10 +50,8 @@ static til_module_context_t * sparkler_create_context(const til_module_t *module .show_bsp_matches_affected_only = ((sparkler_setup_t *)setup)->show_bsp_matches_affected_only, .seedp = &ctxt->til_module_context.seed, }); - if (!ctxt->particles) { - free(ctxt); - return NULL; - } + if (!ctxt->particles) + return til_module_context_free(&ctxt->til_module_context); particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS, 0); @@ -65,7 +63,9 @@ static void sparkler_destroy_context(til_module_context_t *context) { sparkler_context_t *ctxt = (sparkler_context_t *)context; - particles_free(ctxt->particles); + if (ctxt->particles) + particles_free(ctxt->particles); + free(ctxt); } diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index bff3d8f..ef35c4e 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -123,7 +123,8 @@ static til_module_context_t * stars_create_context(const til_module_t *module, t for(int i=0; iseed)%16; i++){ p_ptr = malloc(sizeof(struct points)); if (!p_ptr) - return NULL; + return til_module_context_free(&ctxt->til_module_context); + p_ptr->x = get_random_unit_coord(&ctxt->seed); p_ptr->y = get_random_unit_coord(&ctxt->seed); p_ptr->z = z; diff --git a/src/modules/swab/swab.c b/src/modules/swab/swab.c index 9b7043c..343eb04 100644 --- a/src/modules/swab/swab.c +++ b/src/modules/swab/swab.c @@ -75,10 +75,8 @@ static til_module_context_t * swab_create_context(const til_module_t *module, ti return NULL; ctxt->din = din_new(12, 12, 100, seed); - if (!ctxt->din) { - free(ctxt); - return NULL; - } + if (!ctxt->din) + return til_module_context_free(&ctxt->til_module_context); return &ctxt->til_module_context; } -- cgit v1.2.3