Skip to content

Commit

Permalink
Consider null terminator when using malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaSajt committed Nov 21, 2023
1 parent e37c609 commit b62600c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/mainwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b62600c

Please sign in to comment.