From 9f8508ed45c9c8e9d77cfecc4b4679ce7bd96d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez-Cabanillas?= Date: Sun, 8 Dec 2024 19:27:26 +0100 Subject: [PATCH] Fix crash on startup when there are no MIDI devices and autoconnect is enabled --- src/drivers/fluid_winmidi.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/drivers/fluid_winmidi.c b/src/drivers/fluid_winmidi.c index 2f5eefebe..4da2c124f 100644 --- a/src/drivers/fluid_winmidi.c +++ b/src/drivers/fluid_winmidi.c @@ -484,24 +484,26 @@ static void fluid_winmidi_autoconnect_build_name(char *name) int j; unsigned int i, n = 0; unsigned int num = midiInGetNumDevs(); - - for (i = 0; i < num; ++i) + if (num > 0) { - char x[4]; - j = FLUID_SNPRINTF(x, sizeof(x), "%d;", i); - n += j; - if (n >= sizeof(new_name)) + for (i = 0; i < num; ++i) { - FLUID_LOG(FLUID_DBG, "winmidi: autoconnect dev name exceeds MAXPNAMELEN (%d), num (%d), n (%d)", MAXPNAMELEN, num, n); - return; + char x[4]; + j = FLUID_SNPRINTF(x, sizeof(x), "%d;", i); + n += j; + if (n >= sizeof(new_name)) + { + FLUID_LOG(FLUID_DBG, "winmidi: autoconnect dev name exceeds MAXPNAMELEN (%d), num (%d), n (%d)", MAXPNAMELEN, num, n); + return; + } + strncat(new_name, x, j); } - strncat(new_name, x, j); + + name[n - 1] = 0; + + FLUID_MEMSET(name, 0, MAXPNAMELEN); + FLUID_STRCPY(name, new_name); } - - name[n - 1] = 0; - - FLUID_MEMSET(name, 0, MAXPNAMELEN); - FLUID_STRCPY(name, new_name); } /*