From 9c99628bb8a13ce825cf6ee7a3544a40b5527068 Mon Sep 17 00:00:00 2001 From: John Vogel Date: Mon, 9 Oct 2023 10:36:06 -0400 Subject: [PATCH 3/4] volume.c: take some care Use strcpy instead of sprintf. Be more careful with the printf format. --- volume.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/volume.c b/volume.c index 8117279..b0c49ee 100644 --- a/volume.c +++ b/volume.c @@ -25,6 +25,8 @@ int put_infos(int vol_min, int vol_max, int volume; int volume_percent; bool mute_switch; + bool print_switch_state; + bool print_volume_percent; char switch_state[4]; snd_hctl_elem_read(volume_elem, volume_ctl); @@ -36,16 +38,21 @@ int put_infos(int vol_min, int vol_max, volume_percent = (int)(100.0f * ((float)volume - vol_min) / (vol_max - vol_min)); if (mute_switch == true) - sprintf(switch_state, "on"); + strcpy(switch_state, "on"); else - sprintf(switch_state, "off"); + strcpy(switch_state, "off"); - if (strstr(format, "%s") == NULL) - printf(format, volume_percent); - else if (strstr(format, "%i") == NULL) + print_switch_state = (strstr(format, "%s") == NULL) ? false : true; + print_volume_percent = (strstr(format, "%i") == NULL) ? false : true; + + if (print_switch_state && print_volume_percent) + printf(format, switch_state, volume_percent); + else if (print_switch_state) printf(format, switch_state); + else if (print_volume_percent) + printf(format, volume_percent); else - printf(format, switch_state, volume_percent); + exit(EXIT_FAILURE); printf("\n"); fflush(stdout); -- 2.42.0