Skip to content

Commit

Permalink
Improve all plain sprintf to snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Oct 6, 2023
1 parent 46afffc commit 8f6032a
Show file tree
Hide file tree
Showing 51 changed files with 204 additions and 219 deletions.
2 changes: 1 addition & 1 deletion src/decoder_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/devices/ambientweather_wh31e.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/devices/ant_antplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions src/devices/blyss.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?!?
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/devices/burnhardbbq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/devices/dsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/devices/efth800.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/devices/elro_db286a.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/devices/emos_e6016.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/devices/fineoffset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/devices/fineoffset_wh1050.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/devices/fineoffset_wh1080.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/devices/fineoffset_ws90.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/devices/flex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions src/devices/flowis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 5 additions & 9 deletions src/devices/generic_motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,21 @@ 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))
|| ((b[1] == 0xff) && (b[2] == 0xff))
|| 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);
Expand Down
4 changes: 2 additions & 2 deletions src/devices/govee.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/devices/hcs200.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/devices/honeywell_cm921.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/devices/interlogix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/devices/intertechno.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/devices/neptune_r900.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 8f6032a

Please sign in to comment.