diff --git a/src/mainwin.c b/src/mainwin.c index e153a4c..3745c08 100644 --- a/src/mainwin.c +++ b/src/mainwin.c @@ -599,16 +599,18 @@ void set_start_stop_button_hotkey_text() if (config->button1 != -1) { const char *button_1_key = keycode_to_string(display, config->button1); - start_text = malloc(strlen(start_text_1) + strlen(button_1_key) + strlen(button_2_key) + 4); - stop_text = malloc(strlen(stop_text_1) + strlen(button_1_key) + strlen(button_2_key) + 4); + // Allocate space for formatted string with null terminator + start_text = malloc(strlen(start_text_1) + strlen(button_1_key) + strlen(button_2_key) + 4 + 1); + stop_text = malloc(strlen(stop_text_1) + strlen(button_1_key) + strlen(button_2_key) + 4 + 1); sprintf(start_text, "%s (%s+%s)", start_text_1, button_1_key, button_2_key); sprintf(stop_text, "%s (%s+%s)", stop_text_1, button_1_key, button_2_key); } // Only one key else { - start_text = malloc(strlen(start_text_1) + strlen(button_2_key) + 3); - stop_text = malloc(strlen(stop_text_1) + strlen(button_2_key) + 3); + // Allocate space for formatted string with null terminator + start_text = malloc(strlen(start_text_1) + strlen(button_2_key) + 3 + 1); + stop_text = malloc(strlen(stop_text_1) + strlen(button_2_key) + 3 + 1); sprintf(start_text, "%s (%s)", start_text_1, button_2_key); sprintf(stop_text, "%s (%s)", stop_text_1, button_2_key); }