From ff7d6aea3a87ce52d818d2ca5dc11ea79d1ab135 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 30 Mar 2021 15:53:50 +0200 Subject: [PATCH] src: replace sprintf with snprintf and zero last byte --- src/classic/btstack_cvsd_plc.c | 26 +++++++++++++----- src/classic/btstack_sbc_plc.c | 26 +++++++++++++----- src/classic/hfp_ag.c | 49 ++++++++++++++++++++++++---------- src/classic/hfp_hf.c | 19 ++++++++++--- src/classic/hsp_ag.c | 8 ++++-- src/classic/hsp_hs.c | 8 ++++-- 6 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/classic/btstack_cvsd_plc.c b/src/classic/btstack_cvsd_plc.c index b52de51fa..f458c9f3a 100644 --- a/src/classic/btstack_cvsd_plc.c +++ b/src/classic/btstack_cvsd_plc.c @@ -191,7 +191,10 @@ static void octave_fprintf_array_int16(FILE * oct_file, char * name, int data_le static FILE * open_octave_file(btstack_cvsd_plc_state_t *plc_state, octave_frame_type_t frame_type){ char oct_file_name[1200]; octave_frame_type = frame_type; - sprintf(oct_file_name, "%s_octave_plc_%d_%s.m", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); + snprintf(oct_file_name, sizeof(oct_file_name), "%s_octave_plc_%d_%s.m", + octave_base_name, plc_state->frame_count, + octave_frame_type2str(octave_frame_type)); + oct_file_name[sizeof(oct_file_name) - 1] = 0; FILE * oct_file = fopen(oct_file_name, "wb"); if (oct_file == NULL){ @@ -205,7 +208,8 @@ static FILE * open_octave_file(btstack_cvsd_plc_state_t *plc_state, octave_frame static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file, int frame_nr){ char title[100]; char hist_name[10]; - sprintf(hist_name, "hist%d", plc_state->nbf); + snprintf(hist_name, sizeof(hist_name), "hist%d", plc_state->nbf); + hist_name[sizeof(hist_name) - 1] = 0; octave_fprintf_array_int16(oct_file, hist_name, CVSD_LHIST, plc_state->hist); @@ -230,7 +234,9 @@ static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_stat fprintf(oct_file, "pattern_window_x = x + %d;\n", CVSD_LHIST - CVSD_M); fprintf(oct_file, "hf = figure();\n"); - sprintf(title, "PLC %s frame %d", octave_frame_type2str(octave_frame_type), frame_nr); + snprintf(title, sizeof(title), "PLC %s frame %d", + octave_frame_type2str(octave_frame_type), frame_nr); + title[sizeof(title) - 1] = 0; fprintf(oct_file, "hold on;\n"); fprintf(oct_file, "h1 = plot(%s); \n", hist_name); @@ -273,28 +279,34 @@ static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_stat static void octave_fprintf_plot_output(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file){ if (!oct_file) return; char out_name[10]; - sprintf(out_name, "out%d", plc_state->nbf); + snprintf(out_name, sizeof(out_name), "out%d", plc_state->nbf); + out_name[sizeof(out_name) - 1] = 0; int x0 = CVSD_LHIST; int x1 = x0 + CVSD_FS - 1; octave_fprintf_array_int16(oct_file, out_name, CVSD_FS, plc_state->hist+x0); fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name); char rest_hist_name[10]; - sprintf(rest_hist_name, "rest%d", plc_state->nbf); + snprintf(rest_hist_name, sizeof(rest_hist_name), "rest%d", plc_state->nbf); + rest_hist_name[sizeof(rest_hist_name) - 1] = 0; x0 = CVSD_LHIST + CVSD_FS; x1 = x0 + CVSD_OLAL + CVSD_RT - 1; octave_fprintf_array_int16(oct_file, rest_hist_name, CVSD_OLAL + CVSD_RT, plc_state->hist+x0); fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name); char new_hist_name[10]; - sprintf(new_hist_name, "hist%d", plc_state->nbf); + snprintf(new_hist_name, sizeof(new_hist_name), "hist%d", plc_state->nbf); + new_hist_name[sizeof(new_hist_name) - 1] = 0; octave_fprintf_array_int16(oct_file, new_hist_name, CVSD_LHIST, plc_state->hist); fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name); fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n "); char fig_name[1200]; - sprintf(fig_name, "../%s_octave_plc_%d_%s", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); + snprintf(fig_name, sizeof(fig_name), "../%s_octave_plc_%d_%s", + octave_base_name, plc_state->frame_count, + octave_frame_type2str(octave_frame_type)); + fig_name[sizeof(fig_name) - 1] = 0; fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name); } #endif diff --git a/src/classic/btstack_sbc_plc.c b/src/classic/btstack_sbc_plc.c index dec0b5a04..c6560d7f9 100644 --- a/src/classic/btstack_sbc_plc.c +++ b/src/classic/btstack_sbc_plc.c @@ -195,7 +195,10 @@ static void octave_fprintf_array_int16(FILE * oct_file, char * name, int data_le static FILE * open_octave_file(btstack_sbc_plc_state_t *plc_state, octave_frame_type_t frame_type){ char oct_file_name[1200]; octave_frame_type = frame_type; - sprintf(oct_file_name, "%s_octave_plc_%d_%s.m", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); + snprintf(oct_file_name, sizeof(oct_file_name), "%s_octave_plc_%d_%s.m", + octave_base_name, plc_state->frame_count, + octave_frame_type2str(octave_frame_type)); + oct_file_name[sizeof(oct_file_name) - 1] = 0; FILE * oct_file = fopen(oct_file_name, "wb"); if (oct_file == NULL){ @@ -209,7 +212,8 @@ static FILE * open_octave_file(btstack_sbc_plc_state_t *plc_state, octave_frame_ static void octave_fprintf_plot_history_frame(btstack_sbc_plc_state_t *plc_state, FILE * oct_file, int frame_nr){ char title[100]; char hist_name[10]; - sprintf(hist_name, "hist%d", plc_state->nbf); + snprintf(hist_name, sizeof(hist_name), "hist%d", plc_state->nbf); + hist_name[sizeof(hist_name) - 1] = 0; octave_fprintf_array_int16(oct_file, hist_name, SBC_LHIST, plc_state->hist); @@ -234,7 +238,9 @@ static void octave_fprintf_plot_history_frame(btstack_sbc_plc_state_t *plc_state fprintf(oct_file, "pattern_window_x = x + %d;\n", SBC_LHIST - SBC_M); fprintf(oct_file, "hf = figure();\n"); - sprintf(title, "PLC %s frame %d", octave_frame_type2str(octave_frame_type), frame_nr); + snprintf(title, sizeof(title), "PLC %s frame %d", + octave_frame_type2str(octave_frame_type), frame_nr); + title[sizeof(title) - 1] = 0; fprintf(oct_file, "hold on;\n"); fprintf(oct_file, "h1 = plot(%s); \n", hist_name); @@ -277,28 +283,34 @@ static void octave_fprintf_plot_history_frame(btstack_sbc_plc_state_t *plc_state static void octave_fprintf_plot_output(btstack_sbc_plc_state_t *plc_state, FILE * oct_file){ if (!oct_file) return; char out_name[10]; - sprintf(out_name, "out%d", plc_state->nbf); + snprintf(out_name, sizeof(out_name), "out%d", plc_state->nbf); + out_name[sizeof(out_name) - 1] = 0; int x0 = SBC_LHIST; int x1 = x0 + SBC_FS - 1; octave_fprintf_array_int16(oct_file, out_name, SBC_FS, plc_state->hist+x0); fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name); char rest_hist_name[10]; - sprintf(rest_hist_name, "rest%d", plc_state->nbf); + snprintf(rest_hist_name, sizeof(rest_hist_name), "rest%d", plc_state->nbf); + rest_hist_name[sizeof(rest_hist_name) - 1] = 0; x0 = SBC_LHIST + SBC_FS; x1 = x0 + SBC_OLAL + SBC_RT - 1; octave_fprintf_array_int16(oct_file, rest_hist_name, SBC_OLAL + SBC_RT, plc_state->hist+x0); fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name); char new_hist_name[10]; - sprintf(new_hist_name, "hist%d", plc_state->nbf); + snprintf(new_hist_name, sizeof(new_hist_name), "hist%d", plc_state->nbf); + new_hist_name[sizeof(new_hist_name) - 1] = 0; octave_fprintf_array_int16(oct_file, new_hist_name, SBC_LHIST, plc_state->hist); fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name); fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n "); char fig_name[1200]; - sprintf(fig_name, "../%s_octave_plc_%d_%s", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); + snprintf(fig_name, sizeof(fig_name), "../%s_octave_plc_%d_%s", + octave_base_name, plc_state->frame_count, + octave_frame_type2str(octave_frame_type)); + fig_name[sizeof(fig_name) - 1] = 0; fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name); } #endif diff --git a/src/classic/hfp_ag.c b/src/classic/hfp_ag.c index 8b04ca24f..9452cfc91 100644 --- a/src/classic/hfp_ag.c +++ b/src/classic/hfp_ag.c @@ -195,19 +195,24 @@ static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ char buffer[20]; - sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); + snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", + HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ char buffer[40]; - sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); + snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", + HFP_SUPPORTED_FEATURES, hfp_supported_features); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_send_ok(uint16_t cid){ char buffer[10]; - sprintf(buffer, "\r\nOK\r\n"); + snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } @@ -217,37 +222,49 @@ static int hfp_ag_send_ring(uint16_t cid){ static int hfp_ag_send_clip(uint16_t cid){ char buffer[50]; - sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); + snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, + hfp_gsm_clip_number(), hfp_gsm_clip_type()); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ char buffer[50]; - sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); + snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", + HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ char buffer[50]; - sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); + snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", + HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_send_call_waiting_notification(uint16_t cid){ char buffer[50]; - sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); + snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", + HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), + hfp_gsm_clip_type()); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_send_error(uint16_t cid){ char buffer[10]; - sprintf(buffer, "\r\nERROR\r\n"); + snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ char buffer[20]; - sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); + snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", + HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } @@ -261,10 +278,11 @@ static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int // store indicator static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ - sprintf((char *) buffer, "(\"%s\",(%d,%d)),", - hfp_ag_get_ag_indicators(hfp_connection)[i].name, - hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, - hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); + snprintf((char *)buffer, sizeof((char *)buffer), "(\"%s\",(%d,%d)),", + hfp_ag_get_ag_indicators(hfp_connection)[i].name, + hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, + hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); + ((char *)buffer)[sizeof((char *)buffer) - 1] = 0; } // structure: header [indicator [comma indicator]] footer @@ -440,7 +458,10 @@ static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ char buffer[20]; - sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); + snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", + HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, + indicator->status); + buffer[sizeof(buffer) - 1] = 0; return send_str_over_rfcomm(cid, buffer); } diff --git a/src/classic/hfp_hf.c b/src/classic/hfp_hf.c index 7ccd9825e..496576380 100644 --- a/src/classic/hfp_hf.c +++ b/src/classic/hfp_hf.c @@ -833,13 +833,18 @@ static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ char buffer[20]; switch (hfp_connection->hf_send_rrh_command){ case '?': - sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); + snprintf(buffer, sizeof(buffer), "AT%s?\r\n", + HFP_RESPONSE_AND_HOLD); + buffer[sizeof(buffer) - 1] = 0; send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); return; case '0': case '1': case '2': - sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); + snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", + HFP_RESPONSE_AND_HOLD, + hfp_connection->hf_send_rrh_command); + buffer[sizeof(buffer) - 1] = 0; send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); return; default: @@ -851,7 +856,9 @@ static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ if (hfp_connection->hf_send_cnum){ hfp_connection->hf_send_cnum = 0; char buffer[20]; - sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); + snprintf(buffer, sizeof(buffer), "AT%s\r\n", + HFP_SUBSCRIBER_NUMBER_INFORMATION); + buffer[sizeof(buffer) - 1] = 0; send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); return; } @@ -865,7 +872,11 @@ static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ hfp_connection->ok_pending = 1; hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); char buffer[30]; - sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); + snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r\n", + HFP_TRANSFER_HF_INDICATOR_STATUS, + hfp_indicators[i], + (unsigned int)hfp_indicators_value[i]); + buffer[sizeof(buffer) - 1] = 0; send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); } else { log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); diff --git a/src/classic/hsp_ag.c b/src/classic/hsp_ag.c index 659381d95..db3a1f48b 100644 --- a/src/classic/hsp_ag.c +++ b/src/classic/hsp_ag.c @@ -488,7 +488,9 @@ static void hsp_run(void){ int gain = ag_microphone_gain; ag_microphone_gain = -1; char buffer[12]; - sprintf(buffer, "\r\n%s=%d\r\n", HSP_MICROPHONE_GAIN, gain); + snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", + HSP_MICROPHONE_GAIN, gain); + buffer[sizeof(buffer) - 1] = 0; hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); break; } @@ -501,7 +503,9 @@ static void hsp_run(void){ int gain = ag_speaker_gain; ag_speaker_gain = -1; char buffer[12]; - sprintf(buffer, "\r\n%s=%d\r\n", HSP_SPEAKER_GAIN, gain); + snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", + HSP_SPEAKER_GAIN, gain); + buffer[sizeof(buffer) - 1] = 0; hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); break; } diff --git a/src/classic/hsp_hs.c b/src/classic/hsp_hs.c index 28df4d5ce..3ddce9de6 100644 --- a/src/classic/hsp_hs.c +++ b/src/classic/hsp_hs.c @@ -392,7 +392,9 @@ static void hsp_run_handle_state(void){ return; } char buffer[20]; - sprintf(buffer, "%s=%d\r", HSP_HS_MICROPHONE_GAIN, hs_microphone_gain); + snprintf(buffer, sizeof(buffer), "%s=%d\r", + HSP_HS_MICROPHONE_GAIN, hs_microphone_gain); + buffer[sizeof(buffer) - 1] = 0; hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); hs_microphone_gain = -1; break; @@ -404,7 +406,9 @@ static void hsp_run_handle_state(void){ return; } char buffer[20]; - sprintf(buffer, "%s=%d\r", HSP_HS_SPEAKER_GAIN, hs_speaker_gain); + snprintf(buffer, sizeof(buffer), "%s=%d\r", + HSP_HS_SPEAKER_GAIN, hs_speaker_gain); + buffer[sizeof(buffer) - 1] = 0; hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); hs_speaker_gain = -1; break;