From a8bffeb37037fcebdf2af6decead9dccad7d8786 Mon Sep 17 00:00:00 2001 From: David Henningsson Date: Thu, 14 May 2009 23:59:30 +0000 Subject: [PATCH] Avoid segfault in midi router command handler. Closes ticket #45. --- fluidsynth/src/fluid_midi_router.c | 13 +++++++++++++ fluidsynth/src/fluidsynth.c | 20 +++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/fluidsynth/src/fluid_midi_router.c b/fluidsynth/src/fluid_midi_router.c index 56f7fec9..e8bf8f94 100644 --- a/fluidsynth/src/fluid_midi_router.c +++ b/fluidsynth/src/fluid_midi_router.c @@ -522,6 +522,12 @@ fluid_midi_router_handle_midi_event(void* data, fluid_midi_event_t* event) return ret_val; } +#define CHECK_VALID_ROUTER(_router, _out) \ + if (router == NULL) { \ + fluid_ostream_printf(out, "cannot execute router command without a midi router.\n"); \ + goto error_recovery; \ + } + int fluid_midi_router_handle_clear(fluid_synth_t* synth, int ac, char** av, fluid_ostream_t out) { fluid_midi_router_t* router=synth->midi_router; @@ -530,6 +536,7 @@ int fluid_midi_router_handle_clear(fluid_synth_t* synth, int ac, char** av, flui fluid_ostream_printf(out, "router_clear needs no arguments.\n"); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); /* Disable rules and mark for destruction */ fluid_midi_router_disable_all_rules(router); @@ -550,6 +557,7 @@ int fluid_midi_router_handle_default(fluid_synth_t* synth, int ac, char** av, fl fluid_ostream_printf(out, "router_default needs no arguments.\n"); return -1; } + CHECK_VALID_ROUTER(router, out); /* Disable rules and mark for destruction */ fluid_midi_router_disable_all_rules(router); @@ -577,6 +585,7 @@ int fluid_midi_router_handle_begin(fluid_synth_t* synth, int ac, char** av, flui fluid_ostream_printf(out, "router_begin needs no arguments.\n"); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); if (FLUID_STRCMP(av[0],"note") == 0){ dest=& router->note_rules; @@ -618,6 +627,7 @@ int fluid_midi_router_handle_end(fluid_synth_t* synth, int ac, char** av, fluid_ fluid_ostream_printf(out, "router_end needs no arguments."); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); if (fluid_midi_router_end(router) != FLUID_OK){ FLUID_LOG(FLUID_ERR, "midi_router_end failed"); @@ -641,6 +651,7 @@ int fluid_midi_router_handle_chan(fluid_synth_t* synth, int ac, char** av, fluid fluid_ostream_printf(out, "router_chan needs four args: min, max, mul, add."); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); router->new_rule_chan_min=atoi(av[0]); router->new_rule_chan_max=atoi(av[1]); @@ -664,6 +675,7 @@ int fluid_midi_router_handle_par1(fluid_synth_t* synth, int ac, char** av, fluid fluid_ostream_printf(out, "router_par1 needs four args: min, max, mul, add."); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); router->new_rule_par1_min=atoi(av[0]); router->new_rule_par1_max=atoi(av[1]); @@ -687,6 +699,7 @@ int fluid_midi_router_handle_par2(fluid_synth_t* synth, int ac, char** av, fluid fluid_ostream_printf(out, "router_par2 needs four args: min, max, mul, add."); goto error_recovery; } + CHECK_VALID_ROUTER(router, out); router->new_rule_par2_min=atoi(av[0]); router->new_rule_par2_max=atoi(av[1]); diff --git a/fluidsynth/src/fluidsynth.c b/fluidsynth/src/fluidsynth.c index 66704cc2..7c51706a 100644 --- a/fluidsynth/src/fluidsynth.c +++ b/fluidsynth/src/fluidsynth.c @@ -484,15 +484,6 @@ int main(int argc, char** argv) goto cleanup; } - /* try to load the user or system configuration */ - if (config_file != NULL) { - fluid_source(cmd_handler, config_file); - } else if (fluid_get_userconf(buf, 512) != NULL) { - fluid_source(cmd_handler, buf); - } else if (fluid_get_sysconf(buf, 512) != NULL) { - fluid_source(cmd_handler, buf); - } - /* load the soundfonts (check that all non options are SoundFont or MIDI files) */ for (i = arg1; i < argc; i++) { if (fluid_is_soundfont(argv[i])) @@ -557,6 +548,17 @@ int main(int argc, char** argv) } #endif + /* run commands specified in config file */ + if (config_file != NULL) { + fluid_source(cmd_handler, config_file); + } else if (fluid_get_userconf(buf, 512) != NULL) { + fluid_source(cmd_handler, buf); + } else if (fluid_get_sysconf(buf, 512) != NULL) { + fluid_source(cmd_handler, buf); + } + + + /* play the midi files, if any */ for (i = arg1; i < argc; i++) { if ((argv[i][0] != '-') && fluid_is_midifile(argv[i])) {