From 8f6032a9f93505f4dca38c414d303e653acaa63d Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Fri, 6 Oct 2023 15:06:40 +0200 Subject: [PATCH] Improve all plain sprintf to snprintf --- src/decoder_util.c | 2 +- src/devices/ambientweather_wh31e.c | 8 ++++---- src/devices/ant_antplus.c | 4 ++-- src/devices/blyss.c | 11 ++++------- src/devices/burnhardbbq.c | 2 +- src/devices/dsc.c | 8 ++++---- src/devices/efth800.c | 2 +- src/devices/elro_db286a.c | 2 +- src/devices/emos_e6016.c | 2 +- src/devices/fineoffset.c | 4 ++-- src/devices/fineoffset_wh1050.c | 2 +- src/devices/fineoffset_wh1080.c | 2 +- src/devices/fineoffset_ws90.c | 4 ++-- src/devices/flex.c | 2 +- src/devices/flowis.c | 26 ++++++++++++-------------- src/devices/generic_motion.c | 14 +++++--------- src/devices/govee.c | 4 ++-- src/devices/hcs200.c | 4 ++-- src/devices/honeywell_cm921.c | 6 +++--- src/devices/interlogix.c | 12 ++++++------ src/devices/intertechno.c | 2 +- src/devices/neptune_r900.c | 2 +- src/devices/oregon_scientific.c | 2 +- src/devices/regency_fan.c | 10 +++++----- src/devices/schraeder.c | 18 +++++++++--------- src/devices/simplisafe.c | 30 +++++++++++++++--------------- src/devices/smoke_gs558.c | 7 +++---- src/devices/steelmate.c | 13 +++++++------ src/devices/tfa_marbella.c | 9 +++++---- src/devices/tpms_abarth124.c | 12 ++++++------ src/devices/tpms_ave.c | 7 +++---- src/devices/tpms_citroen.c | 16 ++++++++-------- src/devices/tpms_eezrv.c | 8 +++++--- src/devices/tpms_elantra2012.c | 23 ++++++++++------------- src/devices/tpms_ford.c | 18 ++++++++++-------- src/devices/tpms_hyundai_vdo.c | 7 +++---- src/devices/tpms_jansite.c | 12 ++++++------ src/devices/tpms_jansite_solar.c | 12 ++++++------ src/devices/tpms_kia.c | 19 +++++++++---------- src/devices/tpms_pmv107j.c | 2 +- src/devices/tpms_porsche.c | 2 +- src/devices/tpms_renault.c | 22 ++++++++++------------ src/devices/tpms_renault_0435r.c | 12 ++++++------ src/devices/tpms_toyota.c | 7 +++---- src/devices/tpms_truck.c | 2 +- src/devices/tpms_tyreguard400.c | 4 ++-- src/devices/ttx201.c | 2 +- src/devices/visonic_powercode.c | 10 ++++------ src/devices/x10_sec.c | 8 ++++---- src/mongoose.c | 2 +- src/samp_grab.c | 2 +- 51 files changed, 204 insertions(+), 219 deletions(-) diff --git a/src/decoder_util.c b/src/decoder_util.c index 5817ff333..fff89fa34 100644 --- a/src/decoder_util.c +++ b/src/decoder_util.c @@ -58,7 +58,7 @@ static char *bitrow_asprint_code(uint8_t const *bitrow, unsigned bit_len) // print at least one '0' if (bit_len == 0) { - sprintf(row_bytes, "0"); + snprintf(row_bytes, sizeof(row_bytes), "0"); } // a simple bitrow representation diff --git a/src/devices/ambientweather_wh31e.c b/src/devices/ambientweather_wh31e.c index 25c0e3ad3..119cdb946 100644 --- a/src/devices/ambientweather_wh31e.c +++ b/src/devices/ambientweather_wh31e.c @@ -216,7 +216,7 @@ static int ambientweather_whx_decode(r_device *decoder, bitbuffer_t *bitbuffer) float temp_c = (temp_raw - 400) * 0.1f; int humidity = b[4]; char extra[11]; - sprintf(extra, "%02x%02x%02x%02x%02x", b[6], b[7], b[8], b[9], b[10]); + snprintf(extra, sizeof(extra), "%02x%02x%02x%02x%02x", b[6], b[7], b[8], b[9], b[10]); /* clang-format off */ data_t *data = data_make( @@ -258,7 +258,7 @@ static int ambientweather_whx_decode(r_device *decoder, bitbuffer_t *bitbuffer) int seconds = ((b[8] & 0x70) >> 4) * 10 + (b[8] & 0x0F); char clock_str[23]; - sprintf(clock_str, "%04d-%02d-%02dT%02d:%02d:%02dZ", + snprintf(clock_str, sizeof(clock_str), "%04d-%02d-%02dT%02d:%02d:%02dZ", year, month, day, hours, minutes, seconds); /* clang-format off */ @@ -292,7 +292,7 @@ static int ambientweather_whx_decode(r_device *decoder, bitbuffer_t *bitbuffer) int battery_lvl = battery_v <= 9 ? 0 : ((battery_v - 9) / 6 * 100); // 0.9V-1.5V is 0-100 int rain_raw = (b[5] << 8) | b[6]; char extra[11]; - sprintf(extra, "%02x%02x%02x%02x%02x", b[9], b[10], b[11], b[12], b[13]); + snprintf(extra, sizeof(extra), "%02x%02x%02x%02x%02x", b[9], b[10], b[11], b[12], b[13]); if (battery_lvl > 100) battery_lvl = 100; @@ -334,7 +334,7 @@ static int ambientweather_whx_decode(r_device *decoder, bitbuffer_t *bitbuffer) int wgust = b[12]; int wdir = ((b[7] & 0x20) >> 5) | b[11]; char extra[7]; - sprintf(extra, "%02x %02x%01x", b[13], b[16], b[17] >> 4); + snprintf(extra, sizeof(extra), "%02x %02x%01x", b[13], b[16], b[17] >> 4); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/ant_antplus.c b/src/devices/ant_antplus.c index ede380420..ca4fcc7b0 100644 --- a/src/devices/ant_antplus.c +++ b/src/devices/ant_antplus.c @@ -82,7 +82,6 @@ static int ant_antplus_decode(r_device *decoder, bitbuffer_t *bitbuffer) uint8_t const preamble[] = {0xAA}; uint8_t b[17]; // aligned packet data for both preambles/offsets unsigned bit_offset; - char payload[8 * 3 + 1]; // payload is 8 hex pairs for ANT and ANT+ int antplus_flag = 0; // validate buffer: ANT messages are shorter than 150us, i.e. ~140 bits at 1Mbps @@ -115,7 +114,8 @@ static int ant_antplus_decode(r_device *decoder, bitbuffer_t *bitbuffer) uint8_t device_type = b[4]; uint8_t tx_type = b[5]; // display ANT and ANT+ payload in the same format used by ANT tools - sprintf(payload, "%02x %02x %02x %02x %02x %02x %02x %02x", b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14]); + char payload[8 * 3 + 1]; // payload is 8 hex pairs for ANT and ANT+ + snprintf(payload, sizeof(payload), "%02x %02x %02x %02x %02x %02x %02x %02x", b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14]); // display ANT or ANT+ depending on the network key used. if (0xc5a6 == net_key) diff --git a/src/devices/blyss.c b/src/devices/blyss.c index b75aad2a0..a29b4b136 100644 --- a/src/devices/blyss.c +++ b/src/devices/blyss.c @@ -23,15 +23,11 @@ packet gap is 6964 us static int blyss_callback(r_device *decoder, bitbuffer_t *bitbuffer) { - data_t *data; - uint8_t *b; - char id_str[16]; - for (int i = 0; i < bitbuffer->num_rows; ++i) { if (bitbuffer->bits_per_row[i] != 33) // last row is 32 continue; // DECODE_ABORT_LENGTH - b = bitbuffer->bb[i]; + uint8_t *b = bitbuffer->bb[i]; //This needs additional validation, but works on mine. Suspect each DC5-UK-WH uses different codes as the transmitter //is paired to the receivers to avoid being triggered by the neighbours transmitter ?!? @@ -40,10 +36,11 @@ static int blyss_callback(r_device *decoder, bitbuffer_t *bitbuffer) ((b[0] != 0xe7) || (b[1] != 0x37) || (b[2] != 0x7a) || (b[3] != 0x2c) || (b[4] != 0x80))) continue; // DECODE_ABORT_EARLY - sprintf(id_str, "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); + char id_str[16]; + snprintf(id_str, sizeof(id_str), "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Blyss-DC5ukwh", "id", "", DATA_STRING, id_str, NULL); diff --git a/src/devices/burnhardbbq.c b/src/devices/burnhardbbq.c index 3cd88bc42..222c35da4 100644 --- a/src/devices/burnhardbbq.c +++ b/src/devices/burnhardbbq.c @@ -71,7 +71,7 @@ static int burnhardbbq_decode(r_device *decoder, bitbuffer_t *bitbuffer) float temp_c = (temp_raw - 500) * 0.1f; char timer_str[6]; - sprintf(timer_str, "%02x:%02x", b[3], b[4] & 0x7f); + snprintf(timer_str, sizeof(timer_str), "%02x:%02x", b[3], b[4] & 0x7f); char const *meat; switch (b[5] >> 4) { diff --git a/src/devices/dsc.c b/src/devices/dsc.c index 23a69bcbe..5b6360a0c 100644 --- a/src/devices/dsc.c +++ b/src/devices/dsc.c @@ -113,8 +113,6 @@ static int dsc_callback(r_device *decoder, bitbuffer_t *bitbuffer) uint8_t status, crc; //int subtype; uint32_t esn; - char status_str[3]; - char esn_str[7]; int s_closed, s_event, s_tamper, s_battery_low; int s_xactivity, s_xtamper1, s_xtamper2, s_exception; @@ -208,8 +206,10 @@ static int dsc_callback(r_device *decoder, bitbuffer_t *bitbuffer) // 0x80 is always set and 0x04 has never been set. s_exception = ((status & 0x80) != 0x80) || ((status & 0x04) == 0x04); - sprintf(status_str, "%02x", status); - sprintf(esn_str, "%06x", esn); + char status_str[3]; + snprintf(status_str, sizeof(status_str), "%02x", status); + char esn_str[7]; + snprintf(esn_str, sizeof(esn_str), "%06x", esn); /* clang-format off */ data = data_make( diff --git a/src/devices/efth800.c b/src/devices/efth800.c index 76f1a0a1d..0cfe2d52d 100644 --- a/src/devices/efth800.c +++ b/src/devices/efth800.c @@ -64,7 +64,7 @@ static int eurochron_efth800_decode(r_device *decoder, bitbuffer_t *bitbuffer) int dcf77_mth = (b[6] & 0x0f); if (!crc8(b, 8, 0x31, 0x00)) { - sprintf(dcf77_str, "%4d-%02d-%02dT%02d:%02d:%02d", dcf77_year + 2000, dcf77_mth, dcf77_day, dcf77_hour, dcf77_min, dcf77_sec); + snprintf(dcf77_str, sizeof(dcf77_str), "%4d-%02d-%02dT%02d:%02d:%02d", dcf77_year + 2000, dcf77_mth, dcf77_day, dcf77_hour, dcf77_min, dcf77_sec); } } diff --git a/src/devices/elro_db286a.c b/src/devices/elro_db286a.c index af6d56d8e..c4ac3e371 100644 --- a/src/devices/elro_db286a.c +++ b/src/devices/elro_db286a.c @@ -36,7 +36,7 @@ static int elro_db286a_callback(r_device *decoder, bitbuffer_t *bitbuffer) // 32 bits, trailing bit is dropped char id_str[4 * 2 + 1]; - sprintf(id_str, "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); + snprintf(id_str, sizeof(id_str), "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/emos_e6016.c b/src/devices/emos_e6016.c index 681526cbe..f51b5ea67 100644 --- a/src/devices/emos_e6016.c +++ b/src/devices/emos_e6016.c @@ -103,7 +103,7 @@ static int emos_e6016_decode(r_device *decoder, bitbuffer_t *bitbuffer) float dir_deg = dir_raw * 22.5f; char dcf77_str[20]; // "2064-16-32T32:64:64" - sprintf(dcf77_str, "%4d-%02d-%02dT%02d:%02d:%02d", dcf77_year + 2000, dcf77_mth, dcf77_day, dcf77_hour, dcf77_min, dcf77_sec); + snprintf(dcf77_str, sizeof(dcf77_str), "%4d-%02d-%02dT%02d:%02d:%02d", dcf77_year + 2000, dcf77_mth, dcf77_day, dcf77_hour, dcf77_min, dcf77_sec); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/fineoffset.c b/src/devices/fineoffset.c index feabf8d64..f7aae57da 100644 --- a/src/devices/fineoffset.c +++ b/src/devices/fineoffset.c @@ -630,7 +630,7 @@ static int fineoffset_WH51_callback(r_device *decoder, bitbuffer_t *bitbuffer) // Decode data char id[7]; - sprintf(id, "%02x%02x%02x", b[1], b[2], b[3]); + snprintf(id, sizeof(id), "%02x%02x%02x", b[1], b[2], b[3]); int boost = (b[4] & 0xe0) >> 5; int battery_mv = (b[4] & 0x1f) * 100; float battery_level = (battery_mv - 700) / 900.0f; // assume 1.6V (100%) to 0.7V (0%) range @@ -788,7 +788,7 @@ static int alecto_ws1200v2_dcf_callback(r_device *decoder, bitbuffer_t *bitbuffe int time_m = b[8]; // (b[8] >> 4) * 10 + (b[8] & 0x0f); int time_s = b[9]; // (b[9] >> 4) * 10 + (b[9] & 0x0f); char clock_str[32]; - sprintf(clock_str, "%04x-%02x-%02xT%02x:%02x:%02x", + snprintf(clock_str, sizeof(clock_str), "%04x-%02x-%02xT%02x:%02x:%02x", date_y, date_m, date_d, time_h, time_m, time_s); /* clang-format off */ diff --git a/src/devices/fineoffset_wh1050.c b/src/devices/fineoffset_wh1050.c index 0e3979437..b4d528bfb 100644 --- a/src/devices/fineoffset_wh1050.c +++ b/src/devices/fineoffset_wh1050.c @@ -164,7 +164,7 @@ static int fineoffset_wh1050_decode(r_device *decoder, bitbuffer_t *bitbuffer, u int day = ((br[7] & 0xF0) >> 4) * 10 + (br[7] & 0x0F); char clock_str[23]; - sprintf(clock_str, "%04d-%02d-%02dT%02d:%02d:%02d", + snprintf(clock_str, sizeof(clock_str), "%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds); /* clang-format off */ diff --git a/src/devices/fineoffset_wh1080.c b/src/devices/fineoffset_wh1080.c index a8e49831d..a0f36b738 100644 --- a/src/devices/fineoffset_wh1080.c +++ b/src/devices/fineoffset_wh1080.c @@ -276,7 +276,7 @@ static int fineoffset_wh1080_callback(r_device *decoder, bitbuffer_t *bitbuffer, } else if (msg_type == 1) { char clock_str[23]; - sprintf(clock_str, "%04d-%02d-%02dT%02d:%02d:%02d", + snprintf(clock_str, sizeof(clock_str), "%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds); /* clang-format off */ diff --git a/src/devices/fineoffset_ws90.c b/src/devices/fineoffset_ws90.c index b5938d6c6..e50967d75 100644 --- a/src/devices/fineoffset_ws90.c +++ b/src/devices/fineoffset_ws90.c @@ -108,12 +108,12 @@ static int fineoffset_ws90_decode(r_device *decoder, bitbuffer_t *bitbuffer) int rain_raw = (b[19] << 8 ) | (b[20]); int supercap_V = (b[21] & 0x3f); int firmware = b[29]; - char extra[31]; if (battery_lvl > 100) // More then 100%? battery_lvl = 100; - sprintf(extra, "%02x%02x%02x%02x%02x------%02x%02x%02x%02x%02x%02x%02x", b[14], b[15], b[16], b[17], b[18], /* b[19,20] is the rain sensor, b[21] is supercap_V */ b[22], b[23], b[24], b[25], b[26], b[27], b[28]); + char extra[31]; + snprintf(extra, sizeof(extra), "%02x%02x%02x%02x%02x------%02x%02x%02x%02x%02x%02x%02x", b[14], b[15], b[16], b[17], b[18], /* b[19,20] is the rain sensor, b[21] is supercap_V */ b[22], b[23], b[24], b[25], b[26], b[27], b[28]); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/flex.c b/src/devices/flex.c index 692d1a5fd..a2c518bfd 100644 --- a/src/devices/flex.c +++ b/src/devices/flex.c @@ -319,7 +319,7 @@ static int flex_callback(r_device *decoder, bitbuffer_t *bitbuffer) // print at least one '0' if (row_bytes[0] == '\0') { - sprintf(row_bytes, "0"); + snprintf(row_bytes, sizeof(row_bytes), "0"); } // a simpler representation for csv output diff --git a/src/devices/flowis.c b/src/devices/flowis.c index 37323a30c..09047856c 100644 --- a/src/devices/flowis.c +++ b/src/devices/flowis.c @@ -53,8 +53,6 @@ static int flowis_decode(r_device *decoder, bitbuffer_t *bitbuffer) 0xd3, 0x91, 0xd3, 0x91 // sync word }; - data_t *data; - if (bitbuffer->num_rows != 1) { return DECODE_ABORT_EARLY; } @@ -96,26 +94,26 @@ static int flowis_decode(r_device *decoder, bitbuffer_t *bitbuffer) int id = b[5] << 24 | b[4] << 16 | b[3] << 8 | b[2]; int volume = b[13] << 16 | b[12] << 8 | b[11]; - char fts_str[20]; int fts_year = b[10] >> 2; int fts_mth = ((b[9]>>6) | (b[10]&3)<<2); int fts_day = (b[9]&0x3E) >> 1; int fts_hour = (b[8]>>4) | ((b[9]&1)<<4); int fts_min = ((b[8]&0xF)<<2) | ((b[7]&0xC0)>>6); int fts_sec = b[7]&0x3F; - sprintf(fts_str, "%4d-%02d-%02dT%02d:%02d:%02d", fts_year + 2000, fts_mth, fts_day, fts_hour, fts_min, fts_sec); + char fts_str[20]; + snprintf(fts_str, sizeof(fts_str), "%4d-%02d-%02dT%02d:%02d:%02d", fts_year + 2000, fts_mth, fts_day, fts_hour, fts_min, fts_sec); /* clang-format off */ - data = data_make( - "model", "", DATA_STRING, "Flowis", - "id", "Meter id", DATA_INT, id, - "type", "Type", DATA_INT, type, - "volume_m3", "Volume", DATA_FORMAT, "%.3f m3", DATA_DOUBLE, volume/1000.0, - "device_time", "Device time", DATA_STRING, fts_str, - "alarm", "Alarm", DATA_INT, b[15], - "backflow", "Backflow", DATA_INT, b[14], - "mic", "Integrity", DATA_STRING, "CRC", - NULL); + data_t *data = data_make( + "model", "", DATA_STRING, "Flowis", + "id", "Meter id", DATA_INT, id, + "type", "Type", DATA_INT, type, + "volume_m3", "Volume", DATA_FORMAT, "%.3f m3", DATA_DOUBLE, volume/1000.0, + "device_time", "Device time", DATA_STRING, fts_str, + "alarm", "Alarm", DATA_INT, b[15], + "backflow", "Backflow", DATA_INT, b[14], + "mic", "Integrity", DATA_STRING, "CRC", + NULL); /* clang-format on */ decoder_output_data(decoder, data); diff --git a/src/devices/generic_motion.c b/src/devices/generic_motion.c index d1ce8588e..0956066de 100644 --- a/src/devices/generic_motion.c +++ b/src/devices/generic_motion.c @@ -32,13 +32,8 @@ with a repeat gap of 4 pulse widths, i.e.: static int generic_motion_callback(r_device *decoder, bitbuffer_t *bitbuffer) { - data_t *data; - uint8_t *b; - int code; - char code_str[6]; - for (int i = 0; i < bitbuffer->num_rows; ++i) { - b = bitbuffer->bb[i]; + uint8_t *b = bitbuffer->bb[i]; // strictly validate package as there is no checksum if ((bitbuffer->bits_per_row[i] != 20) || ((b[1] == 0) && (b[2] == 0)) @@ -46,11 +41,12 @@ static int generic_motion_callback(r_device *decoder, bitbuffer_t *bitbuffer) || bitbuffer_count_repeats(bitbuffer, i, 0) < 3) continue; // DECODE_ABORT_EARLY - code = (b[0] << 12) | (b[1] << 4) | (b[2] >> 4); - sprintf(code_str, "%05x", code); + int code = (b[0] << 12) | (b[1] << 4) | (b[2] >> 4); + char code_str[6]; + snprintf(code_str, sizeof(code_str), "%05x", code); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Generic-Motion", "code", "", DATA_STRING, code_str, NULL); diff --git a/src/devices/govee.c b/src/devices/govee.c index a52f19dbe..7a42a0aa7 100644 --- a/src/devices/govee.c +++ b/src/devices/govee.c @@ -156,7 +156,7 @@ static int govee_decode(r_device *decoder, bitbuffer_t *bitbuffer) // dump raw input code char code_str[13]; - sprintf(code_str, "%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5]); + snprintf(code_str, sizeof(code_str), "%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5]); bitbuffer_invert(bitbuffer); @@ -339,7 +339,7 @@ static int govee_h5054_decode(r_device *decoder, bitbuffer_t *bitbuffer) uint8_t *b = bitbuffer->bb[r]; char code_str[13]; - sprintf(code_str, "%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5]); + snprintf(code_str, sizeof(code_str), "%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5]); uint16_t chk = crc16(b, 6, 0x1021, 0x1d0f); if (chk != 0) { diff --git a/src/devices/hcs200.c b/src/devices/hcs200.c index 22d338f1f..b7bfebbba 100644 --- a/src/devices/hcs200.c +++ b/src/devices/hcs200.c @@ -66,9 +66,9 @@ static int hcs200_callback(r_device *decoder, bitbuffer_t *bitbuffer) int repeat = (b[8] & 0x40) == 0x40; char encrypted_str[9]; - sprintf(encrypted_str, "%08X", encrypted); + snprintf(encrypted_str, sizeof(encrypted_str), "%08X", encrypted); char serial_str[9]; - sprintf(serial_str, "%07X", serial); + snprintf(serial_str, sizeof(serial_str), "%07X", serial); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/honeywell_cm921.c b/src/devices/honeywell_cm921.c index 11681d59d..fd21529ef 100644 --- a/src/devices/honeywell_cm921.c +++ b/src/devices/honeywell_cm921.c @@ -107,9 +107,9 @@ static data_t *decode_device_ids(const message_t *msg, data_t *data, int style) char buf[16] = {0}; if (style == 0) - decode_device_id(msg->device_id[i], buf, 16); + decode_device_id(msg->device_id[i], buf, sizeof(buf)); else { - sprintf(buf, "%02x%02x%02x", + snprintf(buf, sizeof(buf), "%02x%02x%02x", msg->device_id[i][0], msg->device_id[i][1], msg->device_id[i][2]); @@ -171,7 +171,7 @@ static data_t *honeywell_cm921_interpret_message(r_device *decoder, const messag uint8_t month = msg->payload[6]; uint8_t year[2] = { msg->payload[7], msg->payload[8] }; char time_str[256]; - sprintf(time_str, "%02d:%02d:%02d %02d-%02d-%04d", hour, minute, second, day, month, (year[0] << 8) | year[1]); + snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d %02d-%02d-%04d", hour, minute, second, day, month, (year[0] << 8) | year[1]); data = data_append(data, "datetime", "", DATA_STRING, time_str, NULL); break; } diff --git a/src/devices/interlogix.c b/src/devices/interlogix.c index 465a1240a..2d5ef54f9 100644 --- a/src/devices/interlogix.c +++ b/src/devices/interlogix.c @@ -103,10 +103,7 @@ static int interlogix_decode(r_device *decoder, bitbuffer_t *bitbuffer) data_t *data; unsigned int row = 0; - char device_type_id[2]; char const *device_type; - char device_serial[7]; - char raw_message[7]; int low_battery; char const *f1_latch_state; char const *f2_latch_state; @@ -162,7 +159,8 @@ static int interlogix_decode(r_device *decoder, bitbuffer_t *bitbuffer) return DECODE_FAIL_MIC; } - sprintf(device_type_id, "%01x", (reverse8(message[2]) >> 4)); + char device_type_id[2]; + snprintf(device_type_id, sizeof(device_type_id), "%01x", (reverse8(message[2]) >> 4)); switch ((reverse8(message[2]) >> 4)) { case 0xa: device_type = "contact"; break; @@ -174,9 +172,11 @@ static int interlogix_decode(r_device *decoder, bitbuffer_t *bitbuffer) default: device_type = "unknown"; break; } - sprintf(device_serial, "%02x%02x%02x", reverse8(message[2]), reverse8(message[1]), reverse8(message[0])); + char device_serial[7]; + snprintf(device_serial, sizeof(device_serial), "%02x%02x%02x", reverse8(message[2]), reverse8(message[1]), reverse8(message[0])); - sprintf(raw_message, "%02x%02x%02x", message[3], message[4], message[5]); + char raw_message[7]; + snprintf(raw_message, sizeof(raw_message), "%02x%02x%02x", message[3], message[4], message[5]); // keyfob logic. see protocol description addendum for protocol exceptions if ((reverse8(message[2]) >> 4) == 0xf) { diff --git a/src/devices/intertechno.c b/src/devices/intertechno.c index 81b0252fe..f3b9045f2 100644 --- a/src/devices/intertechno.c +++ b/src/devices/intertechno.c @@ -28,7 +28,7 @@ static int intertechno_callback(r_device *decoder, bitbuffer_t *bitbuffer) return DECODE_ABORT_EARLY; char id_str[11]; - sprintf(id_str, "%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4]); + snprintf(id_str, sizeof(id_str), "%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4]); int slave = b[7] & 0x0f; int master = (b[7] & 0xf0) >> 4; int command = b[6] & 0x07; diff --git a/src/devices/neptune_r900.c b/src/devices/neptune_r900.c index 353deb9b1..0da30450c 100644 --- a/src/devices/neptune_r900.c +++ b/src/devices/neptune_r900.c @@ -184,7 +184,7 @@ static int neptune_r900_decode(r_device *decoder, bitbuffer_t *bitbuffer) int leaknow = b[9]&0x03; // extra 24 bits ??? char extra[7]; - sprintf(extra,"%02x%02x%02x", b[10], b[11], b[12]); + snprintf(extra, sizeof(extra),"%02x%02x%02x", b[10], b[11], b[12]); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/oregon_scientific.c b/src/devices/oregon_scientific.c index efbb7ef7c..a4536fa93 100644 --- a/src/devices/oregon_scientific.c +++ b/src/devices/oregon_scientific.c @@ -437,7 +437,7 @@ static int oregon_scientific_v2_1_decode(r_device *decoder, bitbuffer_t *bitbuff int seconds = ((msg[4] & 0x0F) * 10) + ((msg[4] & 0xF0) >> 4); char clock_str[24]; - sprintf(clock_str, "%04d-%02d-%02dT%02d:%02d:%02d", + snprintf(clock_str, sizeof(clock_str), "%04d-%02d-%02dT%02d:%02d:%02d", year, month, day, hours, minutes, seconds); /* clang-format off */ diff --git a/src/devices/regency_fan.c b/src/devices/regency_fan.c index 2b0871582..31ab4e24f 100644 --- a/src/devices/regency_fan.c +++ b/src/devices/regency_fan.c @@ -119,23 +119,23 @@ static int regency_fan_decode(r_device *decoder, bitbuffer_t *bitbuffer) switch (command) { case 1: // 1 is the command to STOP - sprintf(value_string, "stop"); + snprintf(value_string, sizeof(value_string), "stop"); break; case 2: // 2 is the command to change fan speed - sprintf(value_string, "speed %d", value); + snprintf(value_string, sizeof(value_string), "speed %d", value); break; case 4: // 4 is the command to change the light intensity - sprintf(value_string, "%d %%", value); + snprintf(value_string, sizeof(value_string), "%d %%", value); break; case 5: // 5 is the command to set the light delay - sprintf(value_string, "%s", value == 0 ? "off" : "on"); + snprintf(value_string, sizeof(value_string), "%s", value == 0 ? "off" : "on"); break; case 6: // 6 is the command to change fan direction - sprintf(value_string, "%s", value == 0x07 ? "clockwise" : "counter-clockwise"); + snprintf(value_string, sizeof(value_string), "%s", value == 0x07 ? "clockwise" : "counter-clockwise"); break; default: diff --git a/src/devices/schraeder.c b/src/devices/schraeder.c index 183bc78a4..946a5efe7 100644 --- a/src/devices/schraeder.c +++ b/src/devices/schraeder.c @@ -34,12 +34,9 @@ Packet payload: 1 sync nibble and 8 bytes data, 17 nibbles: static int schraeder_decode(r_device *decoder, bitbuffer_t *bitbuffer) { - data_t *data; uint8_t b[8]; int serial_id; - char id_str[9]; int flags; - char flags_str[3]; int pressure; // mbar/hectopascal int temperature; // deg C @@ -60,11 +57,14 @@ static int schraeder_decode(r_device *decoder, bitbuffer_t *bitbuffer) flags = (b[0] & 0x0F) << 4 | b[1] >> 4; pressure = b[5] * 25; temperature = b[6] - 50; - sprintf(id_str, "%07X", serial_id); - sprintf(flags_str, "%02x", flags); + + char id_str[9]; + snprintf(id_str, sizeof(id_str), "%07X", serial_id); + char flags_str[3]; + snprintf(flags_str, sizeof(flags_str), "%02x", flags); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Schrader", "type", "", DATA_STRING, "TPMS", "flags", "", DATA_STRING, flags_str, @@ -133,8 +133,8 @@ static int schrader_EG53MA4_decode(r_device *decoder, bitbuffer_t *bitbuffer) flags = ((unsigned)b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]; pressure = b[7] * 25; temperature = b[8]; - sprintf(id_str, "%06X", serial_id); - sprintf(flags_str, "%08x", flags); + snprintf(id_str, sizeof(id_str), "%06X", serial_id); + snprintf(flags_str, sizeof(flags_str), "%08x", flags); /* clang-format off */ data = data_make( @@ -267,7 +267,7 @@ static int schrader_SMD3MA4_decode(r_device *decoder, bitbuffer_t *bitbuffer) } char id_str[9]; - sprintf(id_str, "%06X", serial_id); + snprintf(id_str, sizeof(id_str), "%06X", serial_id); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/simplisafe.c b/src/devices/simplisafe.c index ff8d68d61..edacf22b8 100644 --- a/src/devices/simplisafe.c +++ b/src/devices/simplisafe.c @@ -55,8 +55,6 @@ static int ss_sensor_parser(r_device *decoder, bitbuffer_t *bitbuffer, int row) { data_t *data; uint8_t *b = bitbuffer->bb[row]; - char id[6]; - char extradata[30] = ""; // each row needs to have exactly 92 bits if (bitbuffer->bits_per_row[row] != 92) @@ -68,14 +66,16 @@ static int ss_sensor_parser(r_device *decoder, bitbuffer_t *bitbuffer, int row) if (((seq + state) & 0xff) != csum) return DECODE_FAIL_MIC; + char id[6]; ss_get_id(id, b); + char extradata[30] = ""; if (state == 1) { - strcpy(extradata,"Contact Open"); + strncpy(extradata, "Contact Open", sizeof(extradata) - 1); } else if (state == 2) { - strcpy(extradata,"Contact Closed"); + strncpy(extradata, "Contact Closed", sizeof(extradata) - 1); } else if (state == 3) { - strcpy(extradata,"Alarm Off"); + strncpy(extradata, "Alarm Off", sizeof(extradata) - 1); } /* clang-format off */ @@ -99,8 +99,6 @@ static int ss_pinentry_parser(r_device *decoder, bitbuffer_t *bitbuffer, int row { data_t *data; uint8_t *b = bitbuffer->bb[row]; - char id[6]; - char extradata[30]; // In a keypad message the pin is encoded in bytes 10 and 11 with the the digits each using 4 bits // However the bits are low order to high order int digits[5]; @@ -112,9 +110,11 @@ static int ss_pinentry_parser(r_device *decoder, bitbuffer_t *bitbuffer, int row digits[2] = (pinb & 0xf); digits[3] = ((pinb & 0xf0) >> 4); + char id[6]; ss_get_id(id, b); - sprintf(extradata, "Disarm Pin: %x%x%x%x", digits[0], digits[1], digits[2], digits[3]); + char extradata[30]; + snprintf(extradata, sizeof(extradata), "Disarm Pin: %x%x%x%x", digits[0], digits[1], digits[2], digits[3]); /* clang-format off */ data = data_make( @@ -136,23 +136,23 @@ static int ss_keypad_commands(r_device *decoder, bitbuffer_t *bitbuffer, int row { data_t *data; uint8_t *b = bitbuffer->bb[row]; - char id[6]; char extradata[30]; // = "Arming: "; if (b[10] == 0x6a) { - strcpy(extradata, "Arm System - Away"); + strncpy(extradata, "Arm System - Away", sizeof(extradata) - 1); } else if (b[10] == 0xca) { - strcpy(extradata, "Arm System - Home"); + strncpy(extradata, "Arm System - Home", sizeof(extradata) - 1); } else if (b[10] == 0x3a) { - strcpy(extradata, "Arm System - Canceled"); + strncpy(extradata, "Arm System - Canceled", sizeof(extradata) - 1); } else if (b[10] == 0x2a) { - strcpy(extradata, "Keypad Panic Button"); + strncpy(extradata, "Keypad Panic Button", sizeof(extradata) - 1); } else if (b[10] == 0x86) { - strcpy(extradata, "Keypad Menu Button"); + strncpy(extradata, "Keypad Menu Button", sizeof(extradata) - 1); } else { - sprintf(extradata, "Unknown Keypad: %02x", b[10]); + snprintf(extradata, sizeof(extradata), "Unknown Keypad: %02x", b[10]); } + char id[6]; ss_get_id(id, b); /* clang-format off */ diff --git a/src/devices/smoke_gs558.c b/src/devices/smoke_gs558.c index 41315a9b6..e4d56388a 100644 --- a/src/devices/smoke_gs558.c +++ b/src/devices/smoke_gs558.c @@ -49,13 +49,11 @@ Also you always need to learn from the same primary. static int smoke_gs558_callback(r_device *decoder, bitbuffer_t *bitbuffer) { - data_t *data; uint8_t *b; int r; int learn = 0; int unit; // max 30 int id; - char code_str[7]; if (bitbuffer->num_rows < 3) return DECODE_ABORT_EARLY; // truncated transmission @@ -101,10 +99,11 @@ static int smoke_gs558_callback(r_device *decoder, bitbuffer_t *bitbuffer) if (id == 0 || id == 0x7fff) return DECODE_FAIL_SANITY; // reject min/max to reduce false positives - sprintf(code_str, "%02x%02x%02x", b[2], b[1], b[0]); + char code_str[7]; + snprintf(code_str, sizeof(code_str), "%02x%02x%02x", b[2], b[1], b[0]); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Smoke-GS558", "id" , "", DATA_INT, id, "unit", "", DATA_INT, unit, diff --git a/src/devices/steelmate.c b/src/devices/steelmate.c index 42e64cf47..bafc44ee5 100644 --- a/src/devices/steelmate.c +++ b/src/devices/steelmate.c @@ -39,7 +39,6 @@ static int steelmate_callback(r_device *decoder, bitbuffer_t *bitbuffer) for (int row = 0; row < bitbuffer->num_rows; row++) { //Payload is inverted Manchester encoded, and reversed MSB/LSB order - char sensor_idhex[7]; uint8_t *b = bitbuffer->bb[row]; //Length must be 72 bits to be considered a valid packet @@ -51,7 +50,7 @@ static int steelmate_callback(r_device *decoder, bitbuffer_t *bitbuffer) continue; // DECODE_ABORT_EARLY //Preamble - uint8_t preAmble = ~reverse8(b[2]); + uint8_t preamble = ~reverse8(b[2]); //Sensor ID uint8_t id1 = ~reverse8(b[3]); @@ -68,14 +67,16 @@ static int steelmate_callback(r_device *decoder, bitbuffer_t *bitbuffer) //Checksum is a sum of all the other values uint8_t payload_checksum = ~reverse8(b[8]); - uint8_t calculated_checksum = preAmble + id1 + id2 + p1 + tempFahrenheit + tmpbattery_mV; + uint8_t calculated_checksum = preamble + id1 + id2 + p1 + tempFahrenheit + tmpbattery_mV; if (payload_checksum != calculated_checksum) continue; // DECODE_FAIL_MIC - int sensor_id = (id1 << 8) | id2; - sprintf(sensor_idhex, "0x%04x", sensor_id); + int sensor_id = (id1 << 8) | id2; float pressure_psi = p1 * 0.5f; - int battery_mV = tmpbattery_mV * 2; + int battery_mV = tmpbattery_mV * 2; + + char sensor_idhex[7]; + snprintf(sensor_idhex, sizeof(sensor_idhex), "0x%04x", sensor_id); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/tfa_marbella.c b/src/devices/tfa_marbella.c index 90c1724b6..0c07c4075 100644 --- a/src/devices/tfa_marbella.c +++ b/src/devices/tfa_marbella.c @@ -43,8 +43,7 @@ L - lsfr, byte reflected reverse galois with 0x31 key and generator static int tfa_marbella_callback(r_device *decoder, bitbuffer_t *bitbuffer) { unsigned bitpos = 0; - uint8_t msg[11], ic; - char serialnr_str[6 * 2 + 1]; + uint8_t msg[11]; uint8_t const preamble_pattern[] = {0xaa, 0x2d, 0xd4}; @@ -60,7 +59,7 @@ static int tfa_marbella_callback(r_device *decoder, bitbuffer_t *bitbuffer) return DECODE_FAIL_SANITY; // Rev-Galois with gen 0x31 and key 0x31 - ic = lfsr_digest8_reflect(&msg[3], 7, 0x31, 0x31); + uint8_t ic = lfsr_digest8_reflect(&msg[3], 7, 0x31, 0x31); if (ic != msg[10]) { return DECODE_FAIL_MIC; } @@ -71,7 +70,9 @@ static int tfa_marbella_callback(r_device *decoder, bitbuffer_t *bitbuffer) float temp_c = (temp_raw - 400) * 0.1f; int counter = (msg[6] & 0xF) >> 1; int serialnr = msg[3] << 16 | msg[4] << 8 | msg[5]; - sprintf(serialnr_str, "%06x", serialnr); + + char serialnr_str[6 * 2 + 1]; + snprintf(serialnr_str, sizeof(serialnr_str), "%06x", serialnr); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/tpms_abarth124.c b/src/devices/tpms_abarth124.c index 72dfb623e..26c1129ce 100644 --- a/src/devices/tpms_abarth124.c +++ b/src/devices/tpms_abarth124.c @@ -38,11 +38,8 @@ Data layout (nibbles): static int tpms_abarth124_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; - char id_str[4 * 2 + 1]; - char flags[1 * 2 + 1]; int pressure; int temperature; int status; @@ -64,15 +61,18 @@ static int tpms_abarth124_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsi return 0; // DECODE_FAIL_MIC; } - sprintf(flags, "%02x", b[4]); pressure = b[5]; temperature = b[6]; status = b[7]; checksum = b[8]; - sprintf(id_str, "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); + + char flags[1 * 2 + 1]; + snprintf(flags, sizeof(flags), "%02x", b[4]); + char id_str[4 * 2 + 1]; + snprintf(id_str, sizeof(id_str), "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Abarth-124Spider", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_ave.c b/src/devices/tpms_ave.c index e7fe0d7fd..bf4bd423a 100644 --- a/src/devices/tpms_ave.c +++ b/src/devices/tpms_ave.c @@ -32,11 +32,9 @@ Packet nibbles: static int tpms_ave_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[9 + 1]; int mode; int pressure_raw; double pressure; @@ -89,10 +87,11 @@ static int tpms_ave_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned r } pressure = ((double)pressure_raw - offset) * ratio; - sprintf(id_str, "%08x", id); + char id_str[9 + 1]; + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "Model", DATA_STRING, "AVE", "type", "Type", DATA_STRING, "TPMS", "id", "Id", DATA_STRING, id_str, diff --git a/src/devices/tpms_citroen.c b/src/devices/tpms_citroen.c index 12767bb13..173db4a6a 100644 --- a/src/devices/tpms_citroen.c +++ b/src/devices/tpms_citroen.c @@ -31,13 +31,10 @@ Packet nibbles: static int tpms_citroen_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; int state; - char state_str[3]; unsigned id; - char id_str[9]; int flags; int repeat; int pressure; @@ -63,18 +60,21 @@ static int tpms_citroen_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsign return DECODE_FAIL_MIC; // bad checksum } - state = b[0]; // not covered by CRC - sprintf(state_str, "%02x", state); - id = (unsigned)b[1] << 24 | b[2] << 16 | b[3] << 8 | b[4]; - sprintf(id_str, "%08x", id); + state = b[0]; // not covered by CRC + id = (unsigned)b[1] << 24 | b[2] << 16 | b[3] << 8 | b[4]; flags = b[5] >> 4; repeat = b[5] & 0x0f; pressure = b[6]; temperature = b[7]; maybe_battery = b[8]; + char state_str[3]; + snprintf(state_str, sizeof(state_str), "%02x", state); + char id_str[9]; + snprintf(id_str, sizeof(id_str), "%08x", id); + /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Citroen", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_eezrv.c b/src/devices/tpms_eezrv.c index 645120a55..8e86f1895 100644 --- a/src/devices/tpms_eezrv.c +++ b/src/devices/tpms_eezrv.c @@ -71,13 +71,15 @@ static int tpms_eezrv_decode(r_device *decoder, bitbuffer_t *bitbuffer) decoder_log(decoder, 2, __func__, "Checksum fail"); return DECODE_FAIL_MIC; } - char id_str[7]; - sprintf(id_str, "%02x%02x%02x", b[0], b[1], b[2]); float pressure_kPa = b[3] * 2.5; int temperature_C = b[4] - 50; int flags = b[5]; + + char id_str[7]; + snprintf(id_str, sizeof(id_str), "%02x%02x%02x", b[0], b[1], b[2]); char flags_str[3]; - sprintf(flags_str, "%x", flags); + snprintf(flags_str, sizeof(flags_str), "%x", flags); + /* clang-format off */ data_t *data = data_make( "model", "", DATA_STRING, "EezTire-E618", diff --git a/src/devices/tpms_elantra2012.c b/src/devices/tpms_elantra2012.c index efc50c2bc..f7682942f 100644 --- a/src/devices/tpms_elantra2012.c +++ b/src/devices/tpms_elantra2012.c @@ -51,13 +51,10 @@ Preamble is 111 0001 0101 0101 (0x7155). static int tpms_elantra2012_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; uint32_t id; - char id_str[9]; int flags; - char flags_str[3]; int pressure_kpa; int temperature_c; int triggered, battery_low, storage; @@ -73,21 +70,21 @@ static int tpms_elantra2012_decode(r_device *decoder, bitbuffer_t *bitbuffer, un return DECODE_FAIL_MIC; } - id = ((uint32_t)b[2] << 24) | (b[3] << 16) | (b[4] << 8) | (b[5]); - sprintf(id_str, "%08x", id); - - flags = b[6]; - sprintf(flags_str, "%x", flags); - + id = ((uint32_t)b[2] << 24) | (b[3] << 16) | (b[4] << 8) | (b[5]); + flags = b[6]; pressure_kpa = b[0] + 60; temperature_c = b[1] - 50; + storage = (b[6] & 0x04) >> 2; + battery_low = (b[6] & 0x02) >> 1; + triggered = (b[6] & 0x01) >> 0; - storage = (b[6] & 0x04) >> 2; - battery_low = (b[6] & 0x02) >> 1; - triggered = (b[6] & 0x01) >> 0; + char id_str[9]; + snprintf(id_str, sizeof(id_str), "%08x", id); + char flags_str[3]; + snprintf(flags_str, sizeof(flags_str), "%x", flags); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Elantra2012", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_ford.c b/src/devices/tpms_ford.c index aeb992899..5f2e4476a 100644 --- a/src/devices/tpms_ford.c +++ b/src/devices/tpms_ford.c @@ -55,13 +55,10 @@ Packet nibbles: static int tpms_ford_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[9]; int code; - char code_str[7], unknown_str[3], unknown_3_str[2]; float pressure_psi; int temperature_c, temperature_valid; int psibits; @@ -83,11 +80,9 @@ static int tpms_ford_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned } id = (unsigned)b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]; - sprintf(id_str, "%08x", id); /* Extract and log code to aid in debugging. */ code = b[4] << 16 | b[5] << 8 | b[6]; - sprintf(code_str, "%06x", code); /* * Formula is a combination of regression and plausible, observed @@ -153,14 +148,21 @@ static int tpms_ford_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned * so that leaves 0x80 and 0x10, which are expected to be 0. */ unknown |= (b[6] & 0x90); - sprintf(unknown_str, "%02x", unknown); /* Low-order 2 bits are variously 01, 10. */ unknown_3 = b[6] & 0x3; - sprintf(unknown_3_str, "%01x", unknown_3); + + char id_str[9]; + snprintf(id_str, sizeof(id_str), "%08x", id); + char code_str[7]; + snprintf(code_str, sizeof(code_str), "%06x", code); + char unknown_str[3]; + snprintf(unknown_str, sizeof(unknown_str), "%02x", unknown); + char unknown_3_str[2]; + snprintf(unknown_3_str, sizeof(unknown_3_str), "%01x", unknown_3); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Ford", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_hyundai_vdo.c b/src/devices/tpms_hyundai_vdo.c index ebfb9d713..7dd7672bd 100644 --- a/src/devices/tpms_hyundai_vdo.c +++ b/src/devices/tpms_hyundai_vdo.c @@ -42,12 +42,10 @@ Packet nibbles: static int tpms_hyundai_vdo_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; int state; unsigned id; - char id_str[9 + 1]; int flags; int repeat; int pressure; @@ -80,10 +78,11 @@ static int tpms_hyundai_vdo_decode(r_device *decoder, bitbuffer_t *bitbuffer, un temperature = b[7]; maybe_battery = b[8]; - sprintf(id_str, "%08x", id); + char id_str[9 + 1]; + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Hyundai-VDO", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_jansite.c b/src/devices/tpms_jansite.c index d723ea1aa..b131aeb22 100644 --- a/src/devices/tpms_jansite.c +++ b/src/devices/tpms_jansite.c @@ -31,15 +31,12 @@ Data layout (nibbles): static int tpms_jansite_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[7 + 1]; int flags; int pressure; int temperature; - char code_str[7 * 2 + 1]; bitbuffer_manchester_decode(bitbuffer, row, bitpos, &packet_bits, 56); @@ -56,11 +53,14 @@ static int tpms_jansite_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsign pressure = b[4]; temperature = b[5]; //crc = b[6]; - sprintf(id_str, "%07x", id); - sprintf(code_str, "%02x%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6]); // figure out the checksum + + char id_str[7 + 1]; + snprintf(id_str, sizeof(id_str), "%07x", id); + char code_str[7 * 2 + 1]; + snprintf(code_str, sizeof(code_str), "%02x%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6]); // figure out the checksum /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Jansite", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_jansite_solar.c b/src/devices/tpms_jansite_solar.c index c7d5393df..4dc5bba91 100644 --- a/src/devices/tpms_jansite_solar.c +++ b/src/devices/tpms_jansite_solar.c @@ -43,15 +43,12 @@ TODO: identify battery bits static int tpms_jansite_solar_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[7 + 1]; int flags; int pressure; int temperature; - char code_str[9 * 2 + 1]; bitbuffer_manchester_decode(bitbuffer, row, bitpos, &packet_bits, 88); bitbuffer_invert(&packet_bits); @@ -77,11 +74,14 @@ static int tpms_jansite_solar_decode(r_device *decoder, bitbuffer_t *bitbuffer, flags = b[5]; temperature = b[6]; pressure = b[7]; - sprintf(id_str, "%06x", id); - sprintf(code_str, "%02x%02x%02x%02x%02x%02x%02x%02x%02x", b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10]); + + char id_str[7 + 1]; + snprintf(id_str, sizeof(id_str), "%06x", id); + char code_str[9 * 2 + 1]; + snprintf(code_str, sizeof(code_str), "%02x%02x%02x%02x%02x%02x%02x%02x%02x", b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10]); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Jansite-Solar", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_kia.c b/src/devices/tpms_kia.c index f184e238f..d52449274 100644 --- a/src/devices/tpms_kia.c +++ b/src/devices/tpms_kia.c @@ -43,20 +43,15 @@ NOTE: You may need to use the "-s 1000000" option of rtl_433 in order to get a c static int tpms_kia_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[9 + 1]; - char unknown1_str[2 + 1]; - char unknown2_str[3 + 1]; uint8_t unknown1; uint8_t unknown2; uint8_t pressure; uint8_t temperature; uint8_t crc; - char raw[9 * 2 + 1]; // 9 bytes in hex notation unsigned int start_pos; const unsigned int preamble_length = 16; @@ -80,16 +75,20 @@ static int tpms_kia_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned r return DECODE_FAIL_MIC; } - sprintf(id_str, "%08x", id); - sprintf(unknown1_str, "%02x", unknown1); - sprintf(unknown2_str, "%03x", unknown2); - sprintf(raw, "%02x%02x%02x%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]); + char id_str[9 + 1]; + snprintf(id_str, sizeof(id_str), "%08x", id); + char unknown1_str[2 + 1]; + snprintf(unknown1_str, sizeof(unknown1_str), "%02x", unknown1); + char unknown2_str[3 + 1]; + snprintf(unknown2_str, sizeof(unknown2_str), "%03x", unknown2); + char raw[9 * 2 + 1]; // 9 bytes in hex notation + snprintf(raw, sizeof(raw), "%02x%02x%02x%02x%02x%02x%02x%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]); float pressure_float = pressure / 5.0; float temperature_float = temperature - 50.0; /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Kia", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_pmv107j.c b/src/devices/tpms_pmv107j.c index 014192406..54122afe4 100644 --- a/src/devices/tpms_pmv107j.c +++ b/src/devices/tpms_pmv107j.c @@ -65,7 +65,7 @@ static int tpms_pmv107j_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsign } char id_str[9]; - sprintf(id_str, "%08x", id); + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/tpms_porsche.c b/src/devices/tpms_porsche.c index 643e9743f..a44ee2cb0 100644 --- a/src/devices/tpms_porsche.c +++ b/src/devices/tpms_porsche.c @@ -65,7 +65,7 @@ static int tpms_porsche_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsign int temperature_c = temperature - 40; char id_str[4 * 2 + 1]; - sprintf(id_str, "%08x", id); + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/tpms_renault.c b/src/devices/tpms_renault.c index e226d7d5e..4fe86327e 100644 --- a/src/devices/tpms_renault.c +++ b/src/devices/tpms_renault.c @@ -28,16 +28,12 @@ Packet nibbles: static int tpms_renault_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; bitbuffer_t packet_bits = {0}; uint8_t *b; int flags; - char flags_str[3]; unsigned id; - char id_str[7]; int pressure_raw, temp_c, unknown; double pressure_kpa; - char code_str[5]; bitbuffer_manchester_decode(bitbuffer, row, bitpos, &packet_bits, 160); // require 72 data bits @@ -51,20 +47,22 @@ static int tpms_renault_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsign return 0; } - flags = b[0] >> 2; - sprintf(flags_str, "%02x", flags); - - id = b[5] << 16 | b[4] << 8 | b[3]; // little-endian - sprintf(id_str, "%06x", id); - + flags = b[0] >> 2; + id = b[5] << 16 | b[4] << 8 | b[3]; // little-endian pressure_raw = (b[0] & 0x03) << 8 | b[1]; pressure_kpa = pressure_raw * 0.75; temp_c = b[2] - 30; unknown = b[7] << 8 | b[6]; // little-endian, fixed 0xffff? - sprintf(code_str, "%04x", unknown); + + char flags_str[3]; + snprintf(flags_str, sizeof(flags_str), "%02x", flags); + char id_str[7]; + snprintf(id_str, sizeof(id_str), "%06x", id); + char code_str[5]; + snprintf(code_str, sizeof(code_str), "%04x", unknown); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Renault", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_renault_0435r.c b/src/devices/tpms_renault_0435r.c index 23fb59b08..809fc5e01 100644 --- a/src/devices/tpms_renault_0435r.c +++ b/src/devices/tpms_renault_0435r.c @@ -102,17 +102,17 @@ static int tpms_renault_0435r_decode(r_device *decoder, bitbuffer_t *bitbuffer, // observed always 0xc0 - FIXME: find possible combinations and reject message with impossible combination // to avoid confusion with other FSK manchester 9-byte sensors with 8bit xor checksum. - char id_str[7]; - sprintf(id_str, "%02x%02x%02x", b[0], b[1], b[2]); - - char flags_str[3]; - sprintf(flags_str, "%02x", flags); - int pressure_raw = b[4]; double pressure_kpa = pressure_raw / 0.75; int temp_c = (int)b[5] - 50; int rad_acc = (int)b[6] * 5; + char id_str[7]; + snprintf(id_str, sizeof(id_str), "%02x%02x%02x", b[0], b[1], b[2]); + + char flags_str[3]; + snprintf(flags_str, sizeof(flags_str), "%02x", flags); + /* clang-format off */ data_t *data = data_make( "model", "", DATA_STRING, "Renault-0435R", diff --git a/src/devices/tpms_toyota.c b/src/devices/tpms_toyota.c index 92aa3f6a8..d0234d01a 100644 --- a/src/devices/tpms_toyota.c +++ b/src/devices/tpms_toyota.c @@ -30,12 +30,10 @@ The pressure seems to be 1/4 PSI offset by -7 PSI (i.e. 28 raw = 0 PSI). static int tpms_toyota_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) { - data_t *data; unsigned int start_pos; bitbuffer_t packet_bits = {0}; uint8_t *b; unsigned id; - char id_str[9]; unsigned status, pressure1, pressure2, temp; int crc; @@ -62,10 +60,11 @@ static int tpms_toyota_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigne return 0; } - sprintf(id_str, "%08x", id); + char id_str[9]; + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "", DATA_STRING, "Toyota", "type", "", DATA_STRING, "TPMS", "id", "", DATA_STRING, id_str, diff --git a/src/devices/tpms_truck.c b/src/devices/tpms_truck.c index 7fb47bfd1..e74f69b24 100644 --- a/src/devices/tpms_truck.c +++ b/src/devices/tpms_truck.c @@ -74,7 +74,7 @@ static int tpms_truck_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned int temperature = b[7]; char id_str[4 * 2 + 1]; - sprintf(id_str, "%08x", id); + snprintf(id_str, sizeof(id_str), "%08x", id); /* clang-format off */ data_t *data = data_make( diff --git a/src/devices/tpms_tyreguard400.c b/src/devices/tpms_tyreguard400.c index 6261bd478..472ca130c 100644 --- a/src/devices/tpms_tyreguard400.c +++ b/src/devices/tpms_tyreguard400.c @@ -95,9 +95,9 @@ static int tpms_tyreguard400_decode(r_device *decoder, bitbuffer_t *bitbuffer, u //int add1024 = (flags & 0x40) >> 6; // bytes = 0b0X000000 char id_str[8]; - sprintf(id_str, "%07x", (uint32_t)(((b[3] & 0xf)<<24)) | (b[4]<<16) | (b[5]<<8) | b[6]); // 28 bits ID + snprintf(id_str, sizeof(id_str), "%07x", (uint32_t)(((b[3] & 0xf)<<24)) | (b[4]<<16) | (b[5]<<8) | b[6]); // 28 bits ID char flags_str[3]; - sprintf(flags_str, "%02x", flags); + snprintf(flags_str, sizeof(flags_str), "%02x", flags); //id = (b[4] << 8) int pressure_kpa = b[7] | ((flags & 0x70) << 4); diff --git a/src/devices/ttx201.c b/src/devices/ttx201.c index 2ae48645f..e669b0c67 100644 --- a/src/devices/ttx201.c +++ b/src/devices/ttx201.c @@ -187,7 +187,7 @@ static int ttx201_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row int minute = b[4] & 0x3f; int second = (b[5] & 0x7e) >> 1; char clock_str[25]; - sprintf(clock_str, "%04d-%02d-%02dT%02d:%02d:%02d %s", year + 2000, month, day, hour, minute, second, cest ? "CEST" : "CET"); + snprintf(clock_str, sizeof(clock_str), "%04d-%02d-%02dT%02d:%02d:%02d %s", year + 2000, month, day, hour, minute, second, cest ? "CEST" : "CET"); /* clang-format off */ data = data_make( diff --git a/src/devices/visonic_powercode.c b/src/devices/visonic_powercode.c index a9433b8d7..bc20b7297 100644 --- a/src/devices/visonic_powercode.c +++ b/src/devices/visonic_powercode.c @@ -49,8 +49,6 @@ Protocol cribbed from: static int visonic_powercode_decode(r_device *decoder, bitbuffer_t *bitbuffer) { uint8_t msg[32]; - data_t *data; - char id[7]; uint8_t lrc; // 37 bits expected, 6 packet repetitions, accept 4 @@ -81,13 +79,13 @@ static int visonic_powercode_decode(r_device *decoder, bitbuffer_t *bitbuffer) decoder_logf(decoder, 2, __func__, "data byte is %02x", msg[3]); // format device id - sprintf(id, "%02x%02x%02x", msg[0], msg[1], msg[2]); + char id_str[7]; + snprintf(id_str, sizeof(id_str), "%02x%02x%02x", msg[0], msg[1], msg[2]); - // populate data byte fields /* clang-format off */ - data = data_make( + data_t *data = data_make( "model", "Model", DATA_STRING, "Visonic-Powercode", - "id", "ID", DATA_STRING, id, + "id", "ID", DATA_STRING, id_str, "tamper", "Tamper", DATA_INT, ((0x80 & msg[3]) == 0x80) ? 1 : 0, "alarm", "Alarm", DATA_INT, ((0x40 & msg[3]) == 0x40) ? 1 : 0, "battery_ok", "Battery", DATA_INT, ((0x20 & msg[3]) == 0x20) ? 0 : 1, diff --git a/src/devices/x10_sec.c b/src/devices/x10_sec.c index 7697e9788..9ed7e9bf3 100644 --- a/src/devices/x10_sec.c +++ b/src/devices/x10_sec.c @@ -62,8 +62,6 @@ static int x10_sec_callback(r_device *decoder, bitbuffer_t *bitbuffer) data_t *data; uint8_t *b; /* bits of a row */ char const *event_str = "UNKNOWN"; /* human-readable event */ - char x10_id_str[12] = ""; /* string showing hex value */ - char x10_code_str[5] = ""; /* string showing hex value */ int battery_low = 0; /* battery indicator */ int delay = 0; /* delay setting */ uint8_t tamper = 0; /* tamper alarm indicator */ @@ -162,8 +160,10 @@ static int x10_sec_callback(r_device *decoder, bitbuffer_t *bitbuffer) } /* get x10_id_str, x10_code_str ready for output */ - sprintf(x10_id_str, "%02x%02x", b[0], b[4]); - sprintf(x10_code_str, "%02x", b[2]); + char x10_id_str[12]; + snprintf(x10_id_str, sizeof(x10_id_str), "%02x%02x", b[0], b[4]); + char x10_code_str[5]; + snprintf(x10_code_str, sizeof(x10_code_str), "%02x", b[2]); /* debug output */ decoder_logf_bitbuffer(decoder, 1, __func__, bitbuffer, "id=%02x%02x code=%02x event_str=%s", b[0], b[4], b[2], event_str); diff --git a/src/mongoose.c b/src/mongoose.c index 55bd01e1f..049539e8b 100644 --- a/src/mongoose.c +++ b/src/mongoose.c @@ -5800,7 +5800,7 @@ int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info, if (port != 0) { char port_str[20]; - int port_str_len = sprintf(port_str, ":%u", port); + int port_str_len = snprintf(port_str, sizeof(port_str), ":%u", port); mbuf_append(&out, port_str, port_str_len); } diff --git a/src/samp_grab.c b/src/samp_grab.c index 0f4b7baeb..d73831666 100644 --- a/src/samp_grab.c +++ b/src/samp_grab.c @@ -101,7 +101,7 @@ void samp_grab_write(samp_grab_t *g, unsigned grab_len, unsigned grab_end) double freq_mhz = *g->frequency / 1000000.0; double rate_khz = *g->samp_rate / 1000.0; while (1) { - sprintf(f_name, "g%03u_%gM_%gk.%s", g->sg_counter, freq_mhz, rate_khz, format); + snprintf(f_name, sizeof(f_name), "g%03u_%gM_%gk.%s", g->sg_counter, freq_mhz, rate_khz, format); g->sg_counter++; if (access(f_name, F_OK) == -1) { break;