Skip to content

Commit

Permalink
Improve some strcpy strncpy to snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Oct 6, 2023
1 parent 8f6032a commit 7565025
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/abuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void abuf_cat(abuf_t *buf, char const *str)
{
size_t len = strlen(str);
if (buf->left >= len + 1) {
strcpy(buf->tail, str);
memcpy(buf->tail, str, len + 1);
buf->tail += len;
buf->left -= len;
}
Expand Down
8 changes: 5 additions & 3 deletions src/data_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ char const filter_nmea[] = "$GPGGA,";
static void gpsd_client_line(gpsd_client_t *ctx, char *line)
{
if (!ctx->filter_str || strncmp(line, ctx->filter_str, strlen(ctx->filter_str)) == 0) {
strncpy(ctx->msg, line, sizeof(ctx->msg) - 1);
snprintf(ctx->msg, sizeof(ctx->msg), "%s", line);
}
}

Expand Down Expand Up @@ -263,7 +263,6 @@ static data_t *append_filtered_json(data_t *data, char const *json, char const *
}

// check all tokens
char buf[1024] = {0};
for (int i = 1; i < toks - 1; ++i) {
jsmntok_t *k = tok + i;
jsmntok_t *v = tok + i + 1;
Expand All @@ -276,7 +275,10 @@ static data_t *append_filtered_json(data_t *data, char const *json, char const *
char const *key = find_list_strncmp(includes, json + k->start, k->end - k->start);
if (key) {
// append json tag
strncpy(buf, json + v->start, v->end - v->start);
char buf[1024];
int len = MIN(v->end - v->start, (int)sizeof(buf) - 1);
memcpy(buf, json + v->start, len);
buf[len] = '\0';
data = data_append(data,
key, "", DATA_STRING, buf,
NULL);
Expand Down
20 changes: 11 additions & 9 deletions src/devices/simplisafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ static int ss_sensor_parser(r_device *decoder, bitbuffer_t *bitbuffer, int row)
char id[6];
ss_get_id(id, b);

char extradata[30] = "";
char extradata[30];
if (state == 1) {
strncpy(extradata, "Contact Open", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Contact Open");
} else if (state == 2) {
strncpy(extradata, "Contact Closed", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Contact Closed");
} else if (state == 3) {
strncpy(extradata, "Alarm Off", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Alarm Off");
} else {
snprintf(extradata, sizeof(extradata), "");

Check warning on line 80 in src/devices/simplisafe.c

View workflow job for this annotation

GitHub Actions / Build with CMake

zero-length gnu_printf format string [-Wformat-zero-length]

Check warning on line 80 in src/devices/simplisafe.c

View workflow job for this annotation

GitHub Actions / Build with CMake

zero-length gnu_printf format string [-Wformat-zero-length]
}

/* clang-format off */
Expand Down Expand Up @@ -139,15 +141,15 @@ static int ss_keypad_commands(r_device *decoder, bitbuffer_t *bitbuffer, int row
char extradata[30]; // = "Arming: ";

if (b[10] == 0x6a) {
strncpy(extradata, "Arm System - Away", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Arm System - Away");
} else if (b[10] == 0xca) {
strncpy(extradata, "Arm System - Home", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Arm System - Home");
} else if (b[10] == 0x3a) {
strncpy(extradata, "Arm System - Canceled", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Arm System - Canceled");
} else if (b[10] == 0x2a) {
strncpy(extradata, "Keypad Panic Button", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Keypad Panic Button");
} else if (b[10] == 0x86) {
strncpy(extradata, "Keypad Menu Button", sizeof(extradata) - 1);
snprintf(extradata, sizeof(extradata), "Keypad Menu Button");
} else {
snprintf(extradata, sizeof(extradata), "Unknown Keypad: %02x", b[10]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/output_influx.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ static void influx_client_event(struct mg_connection *nc, int ev, void *ev_data)

static influx_client_t *influx_client_init(influx_client_t *ctx, char const *url, char const *token)
{
strncpy(ctx->url, url, sizeof(ctx->url));
ctx->url[sizeof(ctx->url) - 1] = '\0';
snprintf(ctx->url, sizeof(ctx->url), "%s", url);
snprintf(ctx->extra_headers, sizeof (ctx->extra_headers), "Authorization: Token %s\r\n", token);

return ctx;
Expand Down
3 changes: 1 addition & 2 deletions src/output_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ static mqtt_client_t *mqtt_client_init(struct mg_mgr *mgr, tls_opts_t *tls_opts,
//ctx->opts.keepalive = 60;
//ctx->timeout = 10000L;
//ctx->cleansession = 1;
strncpy(ctx->client_id, client_id, sizeof(ctx->client_id));
ctx->client_id[sizeof(ctx->client_id) - 1] = '\0';
snprintf(ctx->client_id, sizeof(ctx->client_id), "%s", client_id);

// if the host is an IPv6 address it needs quoting
if (strchr(host, ':'))
Expand Down
9 changes: 4 additions & 5 deletions src/sdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ static int rtltcp_open(sdr_dev_t **out_dev, char const *dev_query, int verbose)

char *param = arg_param(dev_query); // strip scheme
hostport[0] = '\0';
if (param)
strncpy(hostport, param, sizeof(hostport) - 1);
hostport[sizeof(hostport) - 1] = '\0';
if (param) {
snprintf(hostport, sizeof(hostport), "%s", param);
}
hostport_param(hostport, &host, &port);

print_logf(LOG_CRITICAL, "SDR", "rtl_tcp input from %s port %s", host, port);
Expand Down Expand Up @@ -720,8 +720,7 @@ static int soapysdr_gain_str_set(SoapySDRDevice *dev, char const *gain_str, int

if (strchr(gain_str, '=')) {
char gain_cpy[GAIN_STR_MAX_SIZE];
strncpy(gain_cpy, gain_str, GAIN_STR_MAX_SIZE);
gain_cpy[GAIN_STR_MAX_SIZE - 1] = '\0';
snprintf(gain_cpy, sizeof(gain_cpy), "%s", gain_str);
char *gain_p = gain_cpy;
// Set each gain individually (more control)
char *name;
Expand Down

0 comments on commit 7565025

Please sign in to comment.