From f11d104a367fa6976c8b76f82ebdc49cecd84bcf Mon Sep 17 00:00:00 2001 From: Rey Tucker Date: Fri, 1 Jun 2018 16:49:24 -0400 Subject: [PATCH] host: bladeRF-cli: fix strncat warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: host/utilities/bladeRF-cli/src/cmd/rx.c:137:13: error: ‘strncat’ output may be truncated copying 31 bytes from a string of length 31 [-Werror=stringop-truncation] by using fixed-sized buffers --- host/utilities/bladeRF-cli/src/cmd/rx.c | 36 ++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/host/utilities/bladeRF-cli/src/cmd/rx.c b/host/utilities/bladeRF-cli/src/cmd/rx.c index 73698781..e0d2385b 100644 --- a/host/utilities/bladeRF-cli/src/cmd/rx.c +++ b/host/utilities/bladeRF-cli/src/cmd/rx.c @@ -100,6 +100,11 @@ static int rx_write_csv_sc16q11(struct rxtx_data *rx, int16_t *samples, size_t n_samples) { + const size_t MAXLEN = 128; /* max line length (with newline and null) */ + + char *line = NULL; + char *tmp = NULL; + size_t i, j, nchans; int status = 0; @@ -120,7 +125,21 @@ static int rx_write_csv_sc16q11(struct rxtx_data *rx, MUTEX_UNLOCK(&rx->data_mgmt.lock); if (status != 0) { - return status; + goto out; + } + + line = calloc(MAXLEN, 1); + if (NULL == line) { + status = errno; + set_last_error(&rx->last_error, ETYPE_ERRNO, status); + goto out; + } + + tmp = calloc(MAXLEN, 1); + if (NULL == tmp) { + status = errno; + set_last_error(&rx->last_error, ETYPE_ERRNO, status); + goto out; } MUTEX_LOCK(&rx->file_mgmt.file_lock); @@ -128,16 +147,16 @@ static int rx_write_csv_sc16q11(struct rxtx_data *rx, // Output 2 columns for each enabled channel // (2 cols for BLADERF_RX_X1, 4 cols for BLADERF_RX_X2, etc) for (i = 0; i < 2 * n_samples; i += 2 * nchans) { - char line[32] = { 0 }; + memset(line, 0, MAXLEN); + memset(tmp, 0, MAXLEN); for (j = 0; j < 2 * nchans; j += 2) { - char tmp[32]; - snprintf(tmp, sizeof(tmp), "%s%d, %d", (j > 0) ? ", " : "", + snprintf(tmp, MAXLEN, "%s%d, %d", (j > 0) ? ", " : "", samples[i + j], samples[i + j + 1]); - strncat(line, tmp, sizeof(line) - 1); + strncat(line, tmp, MAXLEN - 1); } - strncat(line, EOL, sizeof(line) - 1); + strncat(line, EOL, MAXLEN - 1); if (fputs(line, rx->file_mgmt.file) < 0) { set_last_error(&rx->last_error, ETYPE_ERRNO, errno); @@ -147,6 +166,11 @@ static int rx_write_csv_sc16q11(struct rxtx_data *rx, } MUTEX_UNLOCK(&rx->file_mgmt.file_lock); + +out: + free(line); + free(tmp); + return status; }