From 7565025358b772a934cde8aec79a3c847c143874 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Fri, 6 Oct 2023 16:17:55 +0200 Subject: [PATCH] Improve some strcpy strncpy to snprintf --- src/abuf.c | 2 +- src/data_tag.c | 8 +++++--- src/devices/simplisafe.c | 20 +++++++++++--------- src/output_influx.c | 3 +-- src/output_mqtt.c | 3 +-- src/sdr.c | 9 ++++----- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/abuf.c b/src/abuf.c index da594882e..8b3bc691f 100644 --- a/src/abuf.c +++ b/src/abuf.c @@ -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; } diff --git a/src/data_tag.c b/src/data_tag.c index 07732090e..798d211ea 100644 --- a/src/data_tag.c +++ b/src/data_tag.c @@ -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); } } @@ -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; @@ -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); diff --git a/src/devices/simplisafe.c b/src/devices/simplisafe.c index edacf22b8..17306c010 100644 --- a/src/devices/simplisafe.c +++ b/src/devices/simplisafe.c @@ -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), ""); } /* clang-format off */ @@ -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]); } diff --git a/src/output_influx.c b/src/output_influx.c index d6198b31c..e0782aff6 100644 --- a/src/output_influx.c +++ b/src/output_influx.c @@ -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; diff --git a/src/output_mqtt.c b/src/output_mqtt.c index 9468c2314..4d60f332e 100644 --- a/src/output_mqtt.c +++ b/src/output_mqtt.c @@ -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, ':')) diff --git a/src/sdr.c b/src/sdr.c index dbf980951..c09ba45c3 100644 --- a/src/sdr.c +++ b/src/sdr.c @@ -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); @@ -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;